How to create type conditionals for functions that accept interface{} in go? - go

I'm writing a function that is meant to accept either strings or slices in go. However, when I type my parameters as interface{} I can't perform actions upon those variables even when inside a conditional which checks the type.
Can the compiler deduce that my local variable must be of type Slice once inside my if block? How can I accomplish a for loop over the Slice after I know for certain it is a Slice?
func createFields(keys interface{}, values interface{}) ([]map[string]interface{}, error) {
fields := make([]map[string]interface{}, 1, 1)
if reflect.TypeOf(keys).Kind() == reflect.Slice && reflect.TypeOf(values).Kind() == reflect.Slice {
if len(keys.([]interface{})) != len(values.([]interface{})) {
return fields, errors.New("The number of keys and values must match")
}
// How can I loop over this slice inside the if block?
for i, key := range keys.([]interface{}) {
item := map[string]string{
"fieldID": keys[i], // ERROR: invalid operation: keys[i] (type interface {} does not support indexing)
"fieldValue": values[i],
}
fields.append(item)// ERROR: fields.append undefined (type []map[string]interface {} has no field or method append)
}
return fields, _
}
if reflect.TypeOf(keys).Kind() == reflect.String && reflect.Typeof(values).Kind() == reflect.String {
item := map[string]string{
"fieldID": keys,
"fieldValue": values,
}
fields.append(item)
return fields, _
}
return fields, errors.New("Parameter types did not match")
}

Use type assertions like
keySlice := keys.([]interface{})
valSlice := values.([]interface{})
and work with those from that point onwards. You can even eliminate the use of reflect, like:
keySlice, keysIsSlice := keys.([]interface{})
valSlice, valuesIsSlice := values.([]interface{})
if (keysIsSlice && valuesIsSlice) {
// work with keySlice, valSlice
return
}
keyString, keysIsString := keys.(string)
valString, valuesIsString := values.(string)
if (keysIsString && valuesIsString) {
// work with keyString, valString
return
}
return errors.New("types don't match")
Or you can structure the whole thing as type switches:
switch k := keys.(type) {
case []interface{}:
switch v := values.(type) {
case []interface{}:
// work with k and v as slices
default:
// mismatch error
}
case string:
switch v := values.(type) {
case string:
// work with k and v as strings
default:
// mismatch error
}
default:
// unknown types error
}

Related

Return default value for generic type

How do you return nil for a generic type T?
func (list *mylist[T]) pop() T {
if list.first != nil {
data := list.first.data
list.first = list.first.next
return data
}
return nil
}
func (list *mylist[T]) getfirst() T {
if list.first != nil {
return list.first.data
}
return nil
}
I get the following compilation error:
cannot use nil as T value in return statement
You can't return nil for any type. If int is used as the type argument for T for example, returning nil makes no sense. nil is also not a valid value for structs.
What you may do–and what makes sense–is return the zero value for the type argument used for T. For example the zero value is nil for pointers, slices, it's the empty string for string and 0 for integer and floating point numbers.
How to return the zero value? Simply declare a variable of type T, and return it:
func getZero[T any]() T {
var result T
return result
}
Testing it:
i := getZero[int]()
fmt.Printf("%T %v\n", i, i)
s := getZero[string]()
fmt.Printf("%T %q\n", s, s)
p := getZero[image.Point]()
fmt.Printf("%T %v\n", p, p)
f := getZero[*float64]()
fmt.Printf("%T %v\n", f, f)
Which outputs (try it on the Go Playground):
int 0
string ""
image.Point (0,0)
*float64 <nil>
The *new(T) idiom
This has been suggested as the preferred option in golang-nuts. It is probably less readable but easier to find and replace if/when some zero-value builtin gets added to the language.
It also allows one-line assignments.
The new built-in allocates storage for a variable of any type and returns a pointer to it, so dereferencing *new(T) effectively yields the zero value for T. You can use a type parameter as the argument:
func Zero[T any]() T {
return *new(T)
}
In case T is comparable, this comes in handy to check if some variable is a zero value:
func IsZero[T comparable](v T) bool {
return v == *new(T)
}
var of type T
Straightforward and easier to read, though it always requires one line more:
func Zero[T any]() T {
var zero T
return zero
}
Named return types
If you don't want to explicitly declare a variable you can use named returns. Not everyone is fond of this syntax, though this might come in handy when your function body is more complex than this contrived example, or if you need to manipulate the value in a defer statement:
func Zero[T any]() (ret T) {
return
}
func main() {
fmt.Println(Zero[int]()) // 0
fmt.Println(Zero[map[string]int]()) // map[]
fmt.Println(Zero[chan chan uint64]()) // <nil>
}
It's not a chance that the syntax for named returns closely resembles that of var declarations.
Using your example:
func (list *mylist[T]) pop() (data T) {
if list.first != nil {
data = list.first.data
list.first = list.first.next
}
return
}
Return nil for non-nillable types
If you actually want to do this, as stated in your question, you can return *T explicitly.
This can be done when the type param T is constrained to something that excludes pointer types. In that case, you can declare the return type as *T and now you can return nil, which is the zero value of pointer types.
// constraint includes only non-pointer types
func getNilFor[T constraints.Integer]() *T {
return nil
}
func main() {
fmt.Println(reflect.TypeOf(getNilFor[int]())) // *int
fmt.Println(reflect.TypeOf(getNilFor[uint64]())) // *uint64
}
Let me state this again: this works best when T is NOT constrained to anything that admits pointer types, otherwise what you get is a pointer-to-pointer type:
// pay attention to this
func zero[T any]() *T {
return nil
}
func main() {
fmt.Println(reflect.TypeOf(zero[int]())) // *int, good
fmt.Println(reflect.TypeOf(zero[*int]())) // **int, maybe not what you want...
}
You can init a empty variable.
if l == 0 {
var empty T
return empty, errors.New("empty Stack")
}

Print the key/value types of a Golang map

I am trying to print the type of a map, eg: map[int]string
func handleMap(m reflect.Value) string {
keys := m.MapKeys()
n := len(keys)
keyType := reflect.ValueOf(keys).Type().Elem().String()
valType := m.Type().Elem().String()
return fmt.Sprintf("map[%s]%s>", keyType, valType)
}
so if I do:
log.Println(handleMap(make(map[int]string)))
I want to get "map[int]string"
but I can't figure out the right calls to make.
func handleMap(m interface{}) string {
return fmt.Sprintf("%T", m)
}
Try not to use reflect. But if you must use reflect:
A reflect.Value value has a Type() function, which returns a reflect.Type value.
If that type's Kind() is reflect.Map, that reflect.Value is a value of type map[T1]T2 for some types T1 and T2, where T1 is the key type and T2 is the element type.
Therefore, when using reflect, we can pull apart the pieces like this:
func show(m reflect.Value) {
t := m.Type()
if t.Kind() != reflect.Map {
panic("not a map")
}
kt := t.Key()
et := t.Elem()
fmt.Printf("m = map from %s to %s\n", kt, et)
}
See a more complete example on the Go Playground. (Note that both maps are actually nil, so there are no keys and values to enumerate.)

Walking through go struct fields with reflect doesn't match case map[string]interface{}

I have unusual task:
1. parse json message to Go struct
2. verify that all fields in JSON are within specific limits:
- string fields length no longer fixed constant
- maps contain no more than fixed number elements
- if values of map keys are nested structs verify for above 2 rules
To do this I use reflect, then iterating over elements,
and doing type checking:
- if int or float - nothing to do - no verification
- if string - verify length (and return if failed)
- if map verify map length (and return if failed), then iterate over map values and recursively check if their fields violate string/map rules
- default (I assume that this is struct nested JSON structure): convert it to interface slice and do recursive call.
Problem:
In JSON, I would have different map value types like:
- map[string]MyStruct1
- map[string]MyStruct2
- etc.
So when I'm doing type checking I write:
case map[string]interface{}
But in my program this case is never matched and goes to case default,
causing some error.
Any possible way to match type with case - map[string]interface{} ????
Here is my code for reference:
http://play.golang.org/p/IVXHLBRuPK
func validate(vals []interface{}) bool {
result := true
for _, elem := range vals {
switch v := elem.(type) {
case int, float64:
fmt.Println("Got int or float: ", v)
case string:
fmt.Println("Got string", v)
if len(elem.(string)) > 5 {
fmt.Println("String rule Violation!")
result = false
break
fmt.Println("After Break")
}
case map[string]interface{}:
fmt.Println("Got map", v)
if len(elem.(map[string]interface{})) > 1 || !validate(elem.([]interface{})) {
fmt.Println("Map length rule Violation!")
result = false
break
}
default:
fmt.Println("Got struct:", v)
// Convert to interface list all other structures no string/int/float/map:
new_v := reflect.ValueOf(elem)
new_values := make([]interface{}, new_v.NumField())
for j := 0; j < new_v.NumField(); j++ {
new_values[j] = new_v.Field(j).Interface()
}
// Recursively call for validate nested structs
if !validate(new_values) {
result = false
break
}
}
}
fmt.Println("After Break 2")
return result
}
func main() {
// Test truct:
x := C{1, B{"abc", A{10, 0.1, map[string]Host{"1,2,3,4": Host{"1.2.3.4"}}}}}
// Conversion:
v := reflect.ValueOf(x)
values := make([]interface{}, v.NumField())
for i := 0; i < v.NumField(); i++ {
values[i] = v.Field(i).Interface()
}
// Validate function verification
fmt.Println(validate(values))
}
In this example I can't ever reach case: map[string]interface{}
Big kudos on helpful suggestions!
The problem is case map[string]interface{} won't match map[string]Host so it will get parsed as a struct, which it isn't.
You will either have to check new_v.Kind() and handle maps via reflection or add a special case for map[string]Host.

How to get zero value of a field type

I have a struct containing many fields - I've figured out how to extract the field name, value, and tag information using reflection. What I also want to do is to determine if the value of a field is different from the field's default value.
Currently, I have this (works, but a bit smelly):
...
qsMap := make(map[string]interface{})
var defaultTime time.Time
var defaultString string
...
// get the field name and value
fieldName := s.Type().Field(i).Tag.Get("bson")
fieldValue := valueField.Interface()
// use reflection to determine the TYPE of the field and apply the proper formatting
switch fieldValue.(type) {
case time.Time:
if fieldValue != defaultTime {
qsMap[fieldName] = fieldValue
}
case string:
if fieldValue != defaultString {
qsMap[fieldName] = fieldValue
}
...
}
Seems to me that there should be a way to avoid the type switch in this case - what I'm trying to do is build up a map of field/values that have a value different from their default zero value, something like:
// doesn't work -- i.e., if fieldValue of type string would be compared against "", etc.
if fieldValue != reflect.Zero(reflect.Type(fieldValue)) {
qsMap[fieldName] = fieldValue
}
Is there an elegant way to accomplish this?
Thanks!
For types that support the equality operation, you can just compare interface{} variables holding the zero value and field value. Something like this:
v.Interface() == reflect.Zero(v.Type()).Interface()
For functions, maps and slices though, this comparison will fail, so we still need to include some special casing. Further more, while arrays and structs are comparable, the comparison will fail if they contain non-comparable types. So you probably need something like:
func isZero(v reflect.Value) bool {
switch v.Kind() {
case reflect.Func, reflect.Map, reflect.Slice:
return v.IsNil()
case reflect.Array:
z := true
for i := 0; i < v.Len(); i++ {
z = z && isZero(v.Index(i))
}
return z
case reflect.Struct:
z := true
for i := 0; i < v.NumField(); i++ {
z = z && isZero(v.Field(i))
}
return z
}
// Compare other types directly:
z := reflect.Zero(v.Type())
return v.Interface() == z.Interface()
}
I couldn't post a comment, but the accepted answer panics if you provide a struct with any unexported fields. The trick I've found is to check if the field can be set - essentially ignoring any unexported fields.
func isZero(v reflect.Value) bool {
switch v.Kind() {
case reflect.Func, reflect.Map, reflect.Slice:
return v.IsNil()
case reflect.Array:
z := true
for i := 0; i < v.Len(); i++ {
z = z && isZero(v.Index(i))
}
return z
case reflect.Struct:
z := true
for i := 0; i < v.NumField(); i++ {
if v.Field(i).CanSet() {
z = z && isZero(v.Field(i))
}
}
return z
case reflect.Ptr:
return isZero(reflect.Indirect(v))
}
// Compare other types directly:
z := reflect.Zero(v.Type())
result := v.Interface() == z.Interface()
return result
}
You can switch on the Kind() of the Value and use the appropriate accessor (many fewer kinds than types). Something like:
switch valueField.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if valueField.Int() == 0 {...}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if valueField.Uint() == 0 {...}
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
if valueField.IsNil() {...}
//add more cases for Float, Bool, String, etc (and anything else listed http://golang.org/pkg/reflect/#Kind )
}
You could also get a zeroed instance of a value by using reflect.Zero(valueField.Type()) but it is not safe to compare that with valueField since some types (such as slices and maps) do not support equality and would panic.
Simply use
reflect.Value.IsZero()
which is introduced in Go 1.13, released 2019-09-03 (the issue lasted 5 years).
(Missed it too, so I first landed here.)

How do I type-assert that a value is a pointer (to a string)?

I'm trying to create a method that will return the length of a generic type. If we have a string, we call len(string), or if its an array of interface{} type, we call len() on that as well. This works well, however, it doesnt work in you pass in a pointer to a string (I'm assuming I'd have the same problem with arrays and slices as well). So how can I check if I have a pointer, and dereference it?
func (s *Set) Len(i interface{}) int {
if str, ok := i.(string); ok {
return len(str)
}
if array, ok := i.([]interface{}); ok {
return len(array)
}
if m, ok := i.(map[interface{}]interface{}); ok {
return len(m)
}
return 0
}
You can do the same thing as for the other types:
if str, ok := i.(*string); ok {
return len(*str)
}
At this point you may want to use a type switch instead of the more verbose ifs:
switch x := i.(type) {
case string:
return len(x)
case *string:
return len(*x)
…
}

Resources