iterate through map[string]interface{} recursively using reflect package - go

i just trying learn to use reflection, i wanna go througe each field in the map[string]interface{} this why i using
json.Unmarshal to interface{}.
func i2s(data interface{}, out interface{}) error {
fmt.Println(reflect.TypeOf(data).Kind())
switch reflect.TypeOf(data).Kind() {
case reflect.Map, reflect.Array, reflect.Ptr, reflect.Slice:
i2s(reflect.TypeOf(data).Elem(), out)
case reflect.Struct:
for i := 0; i < reflect.ValueOf(data).NumField(); i++ {
f := reflect.TypeOf(data).Field(i)
fmt.Println(f.Name, f.Type.Name())
if f.Tag != "" {
fmt.Println(f.Tag)
}
}
}
return nil
}
type Simple struct {
ID int
Username string
Active bool
}
func main() {
test := &Simple{
ID: 42,
Username: "dcandu",
Active: true,
}
jsonRaw, _ := json.Marshal(test)
var tmpData interface{}
json.Unmarshal(jsonRaw, &tmpData)
result := new(Simple)
i2s(tmpData, result)
}
When i run this code i get the infinite loop.
Please tell me where i'm doing wrong.
Thank you.

Related

Generically modify struct fields using reflection in golang

Below is a method which uses reflect package to modify fields of a struct ,this works for a specific struct type
func modify(obj Car) interface{} {
ty := reflect.TypeOf(obj)
for i := 0; i < ty.NumField(); i++ {
rval := reflect.Indirect(reflect.ValueOf(&obj))
field := rval.Field(i)
fieldType := field.Kind()
switch fieldType {
case reflect.String:
field.SetString("")
case reflect.Int:
field.SetInt(0)
case reflect.Ptr:
field.Set(reflect.ValueOf(nil))
}
}
return obj
}
modifying the signature to
func modify(obj interface{}) interface{} {
results in
panic: reflect: call of reflect.Value.Field on interface Value
at line
field := rval.Field(i)
https://go.dev/play/p/pGfKtIg5RUp
It works with the signature
func modify(obj Car) interface{} {
https://go.dev/play/p/31Oh6WLmlGP
Why is the compile time type modifying the behaviour ?
The goal here is to mask certain fields based on struct tags .It could wrap an endpoint and the input and output to the method being wrapped could be struct or pointer so in above case both calls should work
modify(car)
modify(&car)
This is how it works for both value and pointer types
func modify(obj interface{}) interface{} {
rv := reflect.ValueOf(obj)
trv := reflect.TypeOf(obj)
value := reflect.New(rv.Type())
if rv.Kind() == reflect.Pointer {
rv = reflect.ValueOf(obj).Elem()
trv = reflect.TypeOf(obj).Elem()
value = reflect.New(rv.Type())
}
for i := 0; i < rv.NumField(); i++ {
field := rv.Field(i)
fieldType := field.Kind()
v := value.Elem().Field(i)
tag, _ := trv.Field(i).Tag.Lookup("es")
if len(tag) != 0 {
switch fieldType {
case reflect.String:
v.SetString(tag)
case reflect.Int:
v.SetInt(0)
case reflect.Ptr:
v.Set(reflect.ValueOf(nil))
}
} else {
v.Set(field)
}
}
return value
}
https://go.dev/play/p/C1pqw_UbPcG

Nested string fields can not be updated using reflection in an arbitrary Go struct

I'm trying to update all string fields in a struct and its subfields using reflection in golang for an arbitrary struct as follows:
package main
import (
"fmt"
"reflect"
"strings"
)
func main() {
type Inner struct {
In1 string
In2 []string
}
type Type struct {
Name string
Names []string
NewSt Inner
}
a := Type{
Name: " [ (Amir[ ",
Names: nil,
NewSt: Inner{
In1: " [in1",
In2: []string{" [in2( "},
},
}
trims(&a)
fmt.Printf("%#v\n", a)
}
func trim(str string) string {
return strings.TrimSpace(strings.Trim(str, "[](){}, "))
}
func trims(ps interface{}) {
v := reflect.ValueOf(ps).Elem() // Elem() dereferences pointer
for i := 0; i < v.NumField(); i++ {
fv := v.Field(i)
switch fv.Kind() {
case reflect.String:
fv.SetString(trim(fv.String()))
case reflect.Struct:
in := fv.Interface()
trims(&in)
}
}
}
But I get panic: reflect: call of reflect.Value.Elem on struct Value error.
How can I fix it or is there any better way that I can do such thing??
Thanks.
func trims(ps interface{}) {
v := reflect.ValueOf(ps)
if v.Kind() == reflect.Ptr {
v = v.Elem() // Elem() dereferences pointer
}
if v.Kind() != reflect.Struct {
panic("not struct")
}
for i := 0; i < v.NumField(); i++ {
fv := v.Field(i)
switch fv.Kind() {
case reflect.String:
fv.SetString(trim(fv.String()))
case reflect.Struct:
// use Addr() to get an addressable
// value of the field
in := fv.Addr().Interface()
// do not use &in, that evaluates
// to *interface{}, that's almost
// NEVER what you want
trims(in)
case reflect.Slice:
if fv.Type().Elem().Kind() == reflect.String {
for i := 0; i < fv.Len(); i++ {
fv.Index(i).SetString(trim(fv.Index(i).String()))
}
}
}
}
}
https://go.dev/play/p/JkJTJzTckNA

Obtain structure info

The program is:
package main
import (
"fmt"
"reflect"
)
type Request struct {
Method string
Resource string //path
Protocol string
}
type s struct {
ID int
Title string
Request Request
Price float64
Interface interface{}
Exists bool
Many []string
}
func main() {
s := s{}
iterateStruct(s)
}
func iterateStruct(s interface{}) {
e := reflect.ValueOf(s)
for i := 0; i < e.NumField(); i++ {
varName := e.Type().Field(i).Name
varKind := e.Field(i).Kind()
fmt.Println(e.Type().Field(i).Name)
if varKind == reflect.Struct {
//iterateStruct( <what should be here?>)
}
varType := e.Type().Field(i).Type
varValue := e.Field(i).Interface()
fmt.Printf("%v %v %v %v\n", varName, varKind, varType, varValue)
}
}
Using recursion I'd like to get the same information for Request, that is a structure part of a structure.
What would I need to pass as a parameter? I tried various ways but I have to reckon it's a lot of trial and error for me.
Try this:
if varKind == reflect.Struct {
iterateStruct(e.Field(i).Interface())
}
e.Field(i) returns the Value for the struct field. Interface{} will return the underlying value, so you can call iterateStruct using that.
Here's an example that handles fields with pointers to structs, interfaces containing struct value, etc.. As a bonus, this example indents nested structs.
func iterate(v reflect.Value, indent string) {
v = reflect.Indirect(v)
if v.Kind() != reflect.Struct {
return
}
indent += " "
for i := 0; i < v.NumField(); i++ {
varName := v.Type().Field(i).Name
varKind := v.Field(i).Kind()
varType := v.Type().Field(i).Type
varValue := v.Field(i).Interface()
fmt.Printf("%s%v %v %v %v\n", indent, varName, varKind, varType, varValue)
iterate(v.Field(i), indent)
}
}
Call it like this:
iterate(reflect.ValueOf(s), "")
https://go.dev/play/p/y1CzbKAUvD_w

How to use multiple expressions in single case statement?

In the below code:
package main
import (
"fmt"
"reflect"
)
type Model1 struct {
ID string
}
type Model2 struct {
ID string
}
func main() {
ch1 := make(chan Model1)
close(ch1)
checkIfChannelClosed(ch1)
ch2 := make(chan Model2)
close(ch2)
checkIfChannelClosed(ch2)
}
func checkIfChannelClosed(ch interface{}) bool {
if reflect.TypeOf(ch).Kind() != reflect.Chan {
fmt.Println("only channels can be closed")
return false
}
ok := true
if ch == nil {
return false
}
switch v := ch.(type) {
case chan Model1:
select {
case _, ok = <-v: // Line 26
default:
}
case chan Model2:
select {
case _, ok = <-v:
default:
}
default:
fmt.Println("Invalid case")
}
if ok {
fmt.Println("channel is open")
} else {
fmt.Println("channel is closed")
}
return ok
}
GoLang compiler does not allow to write multiple expressions in case statement(as shown below). Goal is to avoid redundant code for select:
switch v := ch.(type) {
case chan Model1, chan Model2:
select {
case _, ok = <-v:
default:
}
default:
fmt.Println("Invalid case")
}
How to use multiple expressions with case statement?
I read this in "The Go programming language" chapter 7.13:
In this style, the emphasis is on the concrete types that satisfy the interface, not on the interface’s methods (if indeed it has any),and there is no hiding of information.
So, i think x.(Type) return a concrete type,if you use a multicase in a swith x:=x.(Type), what happend in the follow code?
switch v := ch.(type) {
case chan Model1,int:
//do something
}
just use the reflect.value to do this:
func checkIfChannelClosed(ch interface{}) bool {
v := reflect.ValueOf(ch)
if v.Kind() != reflect.Chan {
fmt.Println("only channels can be closed")
return false
}
_, ok := v.TryRecv()
if ok{
fmt.Println("recv value from channel..")
}else{
fmt.Println("channel is closed or receive cannot finish without blocking")
}
return ok
}

go: var declared but not used error - how to work around it?

In this function I get "s declared and not used" which I don't understand - do I need to somehow tag it as 'really I used it' or something?
func getString(data map[string]interface{}, name string) (string, error) {
s := data[name]
if reflect.TypeOf(s).Kind() != reflect.String {
return s.(string), nil
}
return "", &apiError{1, "it's not a string"}
}
Oddly, I don't get the error from this function:
func getInt(data map[string]interface{}, name string) (int, error) {
t := data[name]
if reflect.TypeOf(t).Kind() == reflect.Int {
return t.(int), nil
}
return 0, &apiError{1, "it's not an int"}
}
Also, any thoughts on the right way to factor these into a single function would be welcomed!
Your error comes from (declaring and not) using the same identifier elsewhere because this compiles and runs fine on golang.org:
package main
import "reflect"
func main() {
m := make(map[string]interface{})
m["foo"] = "25"
getString(m, "foo")
}
func getString(data map[string]interface{}, name string) (string, error) {
s := data[name]
if reflect.TypeOf(s).Kind() != reflect.String {
return s.(string), nil
}
return "", nil
}
Your code looks correct, error isn't reproducible.
Sure you can refactor these into a single function, but you may not like it depending of tastes.
type VType int
const (
VInteger VType = iota
VString
VUnknown
)
func getValue(data map[string]interface{}, name string) (VType, int, string) {
switch v := data[name].(type) {
case int:
return VInteger, v, ""
case string:
return VString, 0, v
default:
return VUnknown, 0, ""
}
}
func main() {
m := make(map[string]interface{})
m["foo"] = "25"
switch t, i, s := getValue(m, "foo"); t {
case VInteger:
fmt.Println("int ", i) //do something with int
case VString:
fmt.Println("string ", s) //do something with string
case VUnknown:
err := &apiError{1, "it's not an int"} //do something with err
}
}

Resources