This question already has answers here:
How do I pass multiple parameters into a function in PowerShell?
(15 answers)
Closed 3 years ago.
I am trying to simplify a script I wrote by creating some functions, however, I can't seem to get them to work the way I want.
An example function will accept 2 or more parameters but only return 1 value. When I do this, it is returning every value, which in this case are all the parameters (2 in this case) which are passed to the function.
I understand from some research that Powershell returns more than is explicitly called on, which is a little confusing, so I have tried some suggestions about assigning those other values to $null but to no avail.
My function and its result when run looks like the following:
function postReq($path, $payload) {
Write-Host $path
}
postReq($url, $params)
> [path value (is correct)] [$payload (shouldn't be included here)]
Your syntax in how you are calling a function is not correct semantically. You write:
postReq($url, $params)
But that is not the correct syntax in PowerShell. (It's valid syntactically, but not semantically.) In PowerShell, functions are called without ( and ) and without ,, as in:
postReq $url $params
When you use ( ), you are passing a single argument to your function.
Solution
You're not invoking the function how you think you are. PowerShell functions are called without parentheses and the argument delimiter is whitespace, not a comma. Your function call should look like this:
postReq $url $params
What is wrong with using traditional C#-like syntax?
Calling it like you are above as postReq($url, $params) has two undesired consequences here:
The parentheses indicate a sub-expression, the code in the parentheses will run first before the outer code and be treated as a single argument. If you are familiar with solving algebraic equations, the order of operations is the same as in PEMDAS - parentheses first.
Whitespace (), and NOT commas (,) are the argument delimiter to Powershell functions. However, the commas do mean something in Powershell syntax - they signify a collection. [1, 2, 3, 4] is functionally the same as 1, 2, 3, 4 in Powershell. In your case above, you are rolling both parameters into a single array argument of [$url, $params], which the stream-writing cmdlets will do an array join with a , as the delimiter in the rendered string.
But what about object instance and static methods?
This can be confusing to some because object instance and class (RE: static) methods ARE called with the traditional C#-like syntax, where you DO need the parentheses to indicate parameter values, and commas are the delimiter. For example:
([DateTime]::Now).ToString()
returns the current local time, and runs the ToString() method on the returned DateTime object. If you used one of its overloads as shown below, you would separate each argument with a , and regardless of whether you need to pass in arguments or not, you still must specify the outer parentheses:
OverloadDefinitions
-------------------
string ToString()
string ToString(string format)
string ToString(System.IFormatProvider provider)
string ToString(string format, System.IFormatProvider provider)
string IFormattable.ToString(string format, System.IFormatProvider formatProvider)
string IConvertible.ToString(System.IFormatProvider provider)
If you were to omit the parentheses on the empty parameter overload above, you would get the preceding output showing the overload definitions, unlike the behavior when calling functions with no argument.
It's a little odd, but I remember that in any programming language, functions and methods are similar, but distinct, and it's no different in Powershell other than functions and methods are less alike than they are in other languages. I find a good rule of thumb for this is:
Invoke methods with C# syntax and functions with shell syntax.
This question already has answers here:
MongoDB: How to query over a json string?
(1 answer)
Using stored JavaScript functions in the Aggregation pipeline, MapReduce or runCommand
(1 answer)
Closed 3 years ago.
In the context of metabase:
I am applying the following one-liner query to extract a particular field from a collection of mongodb documents:
[{"$project":{"myName":"$field1.field2" }},
{"$match":{"_id":{"$eq":"blah"}}} ]
myName is a string field that is in fact a serialized JSON object.
How can I adapt the pipeline above, perhaps using JSON.parse(), to pull out fields nested further down the JSON hierarchy?
Part II: How would I add a final step to extract some of those deeply nested fields? The situation is further complicated because the document structure is not consistent between documents...
Thanks!
This question already has answers here:
JSON and dealing with unexported fields
(2 answers)
Printing Empty Json as a result [duplicate]
(1 answer)
(un)marshalling json golang not working
(3 answers)
json.Marshal(struct) returns "{}"
(3 answers)
Closed 10 months ago.
I'm trying to marshal to JSON a struct Foo that has a Values map[string]CellValue property where CellValue is another struct. For some reason, the resultant JSON does not contain the data held in the CellValue struct even though all the keys in the Values map are present.
Here's a simple playground repro of the issue.
I'm new to Go, can anyone spot the problem here?
The fields of CellValue are unexported (start with a lowercase character). Per the documentation (emphasis mine), "Each exported struct field becomes a member of the object" - meaning unexported values are ignored when marshaling or unmarshaling.
This question already has answers here:
What is the minimum valid JSON?
(8 answers)
Closed 6 years ago.
Looking at json spec, I'm not sure whether a value is permitted to stand alone, or is only permitted as part of object or array structure.
This is valid JSON:
[123]
But is this valid JSON:
123
Per ECMA-404, The JSON Data Interchange Standard (pdf), which is linked from the JSON.org page you linked:
A JSON text is a sequence of tokens formed from Unicode code points that conforms to the JSON value grammar.
And:
A JSON value can be an object, array, number, string, true, false, or null.
As such, the value 123 is valid JSON, representing an integer.
A value is permitted to stand alone.
The rule has been changed a few years ago.
This question already has answers here:
I read in the EcmaScript specification that certain methods are "generic". What does this mean?
(2 answers)
Closed 7 years ago.
What is the meaning of this? This is regarding the Array.prototype.concat method, but it's stated throughout the spec, again and again, for a series of methods.
The concat function is intentionally generic; it does not require that
its this value be an Array object. Therefore it can be transferred to
other kinds of objects for use as a method. Whether the concat
function can be applied successfully to a host object is
implementation-dependent.
Can you explain this in simple wording, perhaps providing an example as well?
It means that you can apply the method to an array-like object even if it's not an Array. These include NodeLists and the arguments object.
I sometimes use this feature to turn the arguments object into an array: [].slice.call(arguments) returns the argument list as an Array object, giving me native access to all the array methods.
You can read about this on MDN.