Richard Bucker

parallel execution - flowstaller

Posted at — Aug 27, 2014

The orchestration tool I’m working on got a shot today.  Go has some interesting properties. For example there is something called a Zero value than can be applied to structures as well as first class attributes.an empty string is the zero value for a stringa 0 (zero) is the zero value for an integerFalse is a zero value for a boolean A zero value for a struct is when all of the member attributes have their zero value.When decoding or Unmarshaling it’s impossible to know whether the string you decoded yielded an object instance or a zero value.type SomeStructType struct {    MyValue string}In order to Unmarshal something into this structure I need a wrapper structure:type SomeStructWrapper struct {    Something SomeStructType json:"</span><span style="font-family: 'Courier New', Courier, monospace;">SomeStructType</span><span style="font-family: 'Courier New', Courier, monospace;">"}Unmarshaling a string like:{ “SomeStructType”:{“MyValue”:“Hello World”}}Only works when you’re Unmarshaling into a wrapper instance.s := &SomeStructWrapper{}err := json.Unmarshal(buffer, s)But unless you know for certain that the types and buffer are really meant to match then… you need to check. Since you’ve already allocated the target object instance and it’s already the zero value. There is no way to know whether or not the Unmarshal was successful.  The error return value may not be helpful.I found that if I changed the wrapper to:type SomeStructWrapper struct {    Something []SomeStructType json:"</span><span style="font-family: 'Courier New', Courier, monospace;">SomeStructType</span><span style="font-family: 'Courier New', Courier, monospace;">"}Notice that Something is now a slice/array. And if I change the input document syntax a little:{ “SomeStructType”:[{“MyValue”:“Hello World”}]}Now I can check the length of the Something attribute once the Unmarshal returns.  If the len()=0 or with reflection of something==nil then I know that it failed (arrays are pointers; which is a symantic difference with other types).PS: I added a minmax tag so I could identify the number of items in the array.PS: and when there is multiple items in the array/slice I run them in different goroutines.