Make all properties lower case or upper case using reflect? - go

I am receiving unknown json from client and I parse to interface like
var f interface{}
err := json.Unmarshal(b, &f)
How to make all keys in f to be lower keys ?
I have to save this f to mongo and I need to make some queries but I want to avoid mistake if somebody send uppercase same json.

Here's one way to do it:
var v any
err := json.Unmarshal(b, &v)
v = lower(v)
where lower is:
func lower(v any) any {
switch v := v.(type) {
case []any:
lv := make([]any, len(v))
for i := range v {
lv[i] = lower(v[i])
}
return lv
case map[string]any:
lv := make(map[string]any, len(v))
for mk, mv := range v {
lv[strings.ToLower(mk)] = mv
}
return lv
default:
return v
}
}
The lower function calls itself recursively to handle key conversion in nested JSON objects and arrays.
playground
If you know that you are working with an object without nesting (the object fields do not contain arrays or other objects), then you can inline the map case from the lower function above:
var v map[string]any
err := json.Unmarshal(b, &v)
lv := make(map[string]any, len(v))
for mk, mv := range v {
lv[strings.ToLower(mk)] = mv
}
v = lv

It will be map[string]interface{} so go over it and simply convert keys to lowercase.
var f map[string]interface{}
...
converted := make(map[string]interface{}, len(f))
for k, v := range f {
converted[strings.ToLower(k)] = v
}

Related

Is it possible to infer type parameters from what return values are assigned to?

Suppose I wrote two functions like this:
func ToInterfaceSlice[T any](s []T) []interface{} {
res := make([]interface{}, len(s))
for i, v := range s {
res[i] = v
}
return res
}
func FromInterfaceSlice[T any](s []interface{}) (res []T, err error) {
res = make([]T, len(s))
for i, v := range s {
vt, ok := v.(T)
if !ok {
return nil, fmt.Errorf("%v (type=%T) doesn't fit the target type %T", v, v, res)
}
res[i] = vt
}
return
}
When I parse type from the input parameters, I can simply use
var m = []int{1, 2, 3}
fmt.Println(ToInterfaceSlice(m))
The compiler knows the T is int.
However when I try passing type from the return variables
var m []int
m, _ = FromInterfaceSlice([]interface{}{1, 2, 3})
fmt.Println(m)
The compiler gives error:
.\scratch.go:29:27: cannot infer T
I must explicitly pass the type in the function call:
var m []int
m, _ = FromInterfaceSlice[int]([]interface{}{1, 2, 3})
fmt.Println(m)
Is there anything hard to infer type parameters from return type when the receiver vars are not interface? Or just not implemented, even not to implement on purpose?
Update #1 after the comment
I do know a, b := GenericFunc() cannot refer the type of returned value. Currently Go does have "it depends" case whether requires the explicit instantiation or not from the user input.
type Set[T comparable] map[T]struct{}
func NewSet[T comparable](eles ...T) Set[T] {
s := make(Set[T])
for _, ele := range eles {
s[ele] = struct{}{}
}
return s
}
It's okay to use both t := NewSet(1, 2, 3) and t := NewSet[string](), but not var t NewSet[float64] = NewSet() now because of this
The current rules for type inference are explicit. How the return values are used is not taken into account:
Type inference is based on
a type parameter list
a substitution map M initialized with the known type arguments, if any
a (possibly empty) list of ordinary function arguments (in case of a function call only)
As of Go 1.18 might simply rewrite your function to accept an argument of the required type; this has also the benefit of not hiding allocations inside the function body:
func FromInterfaceSlice[T any](s []interface{}, dst []T) error {
if len(s) != len(dst) {
return errors.New("lengths don't match")
}
for i, v := range s {
vt, ok := v.(T)
if !ok {
return nil, fmt.Errorf("%v (type=%T) doesn't fit the target type %T", v, v, res)
}
dst[i] = vt
}
return nil
}
And pass in a destination slice with the required length:
func main() {
src := []interface{}{1, 2, 3}
m := make([]int, len(src))
_ = FromInterfaceSlice(src, m)
fmt.Println(m)
}
If you can't or don't want to determine the slice's length beforehand, you are left with explicit instantiation:
var m []int
m, _ = FromInterfaceSlice[int]([]interface{}{1, 2, 3})
// ^^^ explicit type argument
Also the type parameters are still not inferrable with := shorthand declaration:
// what is m???
m, err := FromInterfaceSlice([]interface{}{1, 2, 3})

How to return a slice of an specific type depending on the variable i send to the function in Go

I have a function, that takes an empty interface (any type, which im looking for 2 in specific) and then returning a slice of the selected type.
func testingInterface(temp interface{}) (interface{}, interface{}) {
var doc interface{}
array := make([]interface{}, 3)
switch x := temp.(type) {
case int:
doc = x
tempArray := make([]string, 3)
for i, v := range tempArray {
array[i] = string(v)
}
fmt.Printf("Int to string %T, %T ", doc, tempArray)
case string:
doc = x
tempArray := make([]int, 3)
for i, v := range tempArray {
array[i] = int(v)
}
fmt.Printf("String to int %T, %T ", doc, tempArray)
}
return array, doc
}
So what happens, it is that the doc variable indeed changes the type of it, but the slice when i return it, it stays as []interface{}
When i test an element individual, it changes the type but the whole array it is still an []interface{}
The tempArray in the question has the slice you want. Return it instead of copying the values to the []interface{} that you don't want.
Use this code:
func testingInterface(x interface{}) (interface{}, interface{}) {
var result interface{}
switch x.(type) {
case int:
result = make([]int, 3)
case string:
result = make([]string, 3)
}
return result, x
}

golang - how to get element from the interface{} type of slice?

I want to write a function that can convert slice([]int, []string, []bool, []int64, []float64) to string.
[]string{a,b,c} -> a,b,c
[]int{1,2,3} -> 1,2,3
There is my code:
func sliceToString(itr interface{}) string {
switch itr.(type) {
case []string:
return strings.Join(itr.([]string), ",")
case []int:
s := []string{}
for _, v := range itr.([]int) {
s = append(s, fmt.Sprintf("%v", v))
}
return strings.Join(s, ",")
case []int64:
s := []string{}
for _, v := range itr.([]int64) {
s = append(s, fmt.Sprintf("%v", v))
}
return strings.Join(s, ",")
case []float64:
s := []string{}
for _, v := range itr.([]float64) {
s = append(s, fmt.Sprintf("%v", v))
}
return strings.Join(s, ",")
case []bool:
s := []string{}
for _, v := range itr.([]bool) {
s = append(s, fmt.Sprintf("%v", v))
}
return strings.Join(s, ",")
}
return ""
}
But it's a little complicated, if i can convert interface{}(type is slice) to []interface{} or get element , it's getting more simple.
func sliceToString(itr interface{}) string {
s := []string{}
// convert interface{} to []interface{} or get elements
// els := ...
for _,v:= range els{
s = append(s, fmt.Sprintf("%v", v))
}
return s
}
You can't do that, because a slice of int, string or anything can't be directly casted to a slice of interfaces. (see that question for more explanation on this).
To do the conversion, you need to cast each item of the slice as an interface{} separately. And you can't access the items without casting to a slice first, but for that you need to know the slice's type (so we're back to square one).
One way to shorten your syntax is to take in a slice of interfaces as argument, and let the caller do the conversion (because the caller knows the slice's type). Here is an example : https://play.golang.org/p/6yLYk1OM25
package main
import (
"fmt"
"strings"
)
func main() {
mySlice := []int{1, 2, 3}
interfaceSlice := make([]interface{}, len(mySlice))
for index := range mySlice {
interfaceSlice[index] = mySlice[index]
}
fmt.Println(sliceToString(interfaceSlice))
}
func sliceToString(values []interface{}) string {
s := make([]string, len(values)) // Pre-allocate the right size
for index := range values {
s[index] = fmt.Sprintf("%v", values[index])
}
return strings.Join(s, ",")
}
This will work with any slice as mySlice, but on the way you lose a lot of convenience for the caller.

Accessing a nested value in a Go map

I have a random JSON (I will not know the schema ahead of time) that I am marshaling into an map[string]interface{}.
I also have a string representing the field value I would like to return, something like "SomeRootKey.NestValue.AnotherNestValue"
I want to be able to return that value. Is there an simple way to access that value without doing some recursive tricks?
Without recursion? yes, using a loop, but no there's no magical way to do that.
func getKey(m interface{}, key string) (string, bool) {
L:
for _, k := range strings.Split(key, ".") {
var v interface{}
switch m := m.(type) {
case map[string]interface{}:
v = m[k]
case []interface{}:
idx, err := strconv.Atoi(k)
if err != nil || idx > len(m) {
break L
}
v = m[idx]
default:
break L
}
switch v := v.(type) {
case map[string]interface{}:
m = v
case []interface{}:
m = v
case string:
return v, true
default:
break L
}
}
return "", false
}
Using json like:
{
"SomeRootKey": {
"NestValue": {"AnotherNestValue": "object value"},
"Array": [{"AnotherNestValue": "array value"}]
}
}
You can use:
fmt.Println(getKey(m, "SomeRootKey.NestValue.AnotherNestValue"))
fmt.Println(getKey(m, "SomeRootKey.Array.0.AnotherNestValue"))
playground

How to read an slice of like []interface{} in Go?

I have something like this:
a := []interface{}{}
b := []interface{}{}
type S struct {
text string
}
s := S{"string"}
t := S{"string"}
a = append(a, s)
b = append(b, t)
a := append(a, b)
a
And now I want to read elements of a, or elements of elements.. but how?
What you want is called a type assertion. http://golang.org/ref/spec#Type_assertions
The simple example on that page is:
var x interface{} = 7 // x has dynamic type int and value 7
i := x.(int) // i has type int and value 7`
The other thing to note is that a type assertion returns a value called ok that is true if the assertion is successful. Here's a simple code example for your case:
a := []interface{}{}
b := []interface{}{}
type S struct {
text string
}
s := S{"string"}
t := S{"string"}
a = append(a, s)
b = append(b, t)
a = append(a, b)
assertedS,ok := a[0].(S)
if !ok { // If this is, in fact, not a value of type S, something is wrong
// error handling
}
fmt.Println(assertedS) // Should show you the same thing as printing s
assertedB,ok := a[1].([]interface{})
if !ok {
//...
}
assertedT,ok := assertedB[0].(S)
if !ok {
//...
}
fmt.Println(assertedT) // Should show you the same thing as printing t
If you don't know ahead of time which list element is what, you can iterate through it and use the "type switch". http://golang.org/ref/spec#Switch_statements
switch x.(type) {
// cases
}
Which allows you to perform conditional behavior based on what type the stored interface{} really is.
For instance, you might use
func ExtractSlice(a []interface{}) {
for _,x := range a {
switch i := x.(type) {
case S:
fmt.Println(i)
case []interface{}:
ExtractSlice(i) // Recursively unpacks b once it's found within a
}
}
}
Do you mean this?
a := []interface{}{}
b := []interface{}{}
type S struct {
text string
}
s := S{"string"}
t := S{"string"}
a = append(a, s)
b = append(b, t)
a = append(a, b)
for _, v := range a {
switch v.(type) {
case S:
fmt.Println("S", v)
default:
fmt.Println("Slice", v)
}
}
This code example may help:
package main
import "fmt"
func main() {
a := []interface{}{}
b := []interface{}{}
type S struct {
text string
}
s := S{"string s"}
t := S{"string t"}
a = append(a, s)
b = append(b, t)
a = append(a, b)
for _, v := range a {
fmt.Println(v)
}
}
but be aware that you've defined a and b as slices of interfaces. This means, that when you do a = append(a, b) you're putting the b slice after the existing a string in the a slice, and therefore when you range over a you get:
{string s} //interface of string
[{string t}] //slice of interface of string

Resources