Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang get struct's field name by JSON tag

Tags:

go

I have a struct:

type Human struct {
    Head  string  `json:"a1"`
    Body  string  `json:"a2"`
    Leg   string  `json:"a3"`
}

How can I get the struct's field name by providing JSON tag name? Probably something like this:

fmt.Println(getFieldName("a1")) // "Head"
fmt.Println(getFieldName("a2")) // "Body"
fmt.Println(getFieldName("a99")) // ""

func getFieldName(tag string) (fieldname string) {
    /* ... */
}

How should I implement the getFieldName function? I read online, it seems I need to use the reflect package, hmmm... any helping hand? :)

like image 508
Boo Yan Jiong Avatar asked Apr 27 '19 09:04

Boo Yan Jiong


2 Answers

You can use the reflect package to loop over a struct's fields and match against their tag values.

func getFieldName(tag, key string, s interface{}) (fieldname string) {
    rt := reflect.TypeOf(s)
    if rt.Kind() != reflect.Struct {
        panic("bad type")
    }
    for i := 0; i < rt.NumField(); i++ {
        f := rt.Field(i)
        v := strings.Split(f.Tag.Get(key), ",")[0] // use split to ignore tag "options" like omitempty, etc.
        if v == tag {
            return f.Name
        }
    }
    return ""
}

https://play.golang.com/p/2zCC7pZKJTz


Alternatively, as pointed out by @icza, you can build up a map and then use that for quicker lookups.

//                         Human            json       a1     Head
var fieldsByTag = make(map[reflect.Type]map[string]map[string]string)

func buildFieldsByTagMap(key string, s interface{}) {
    rt := reflect.TypeOf(s)
    if rt.Kind() != reflect.Struct {
        panic("bad type")
    }

    if fieldsByTag[rt] == nil {
        fieldsByTag[rt] = make(map[string]map[string]string)
    }
    if fieldsByTag[rt][key] == nil {
        fieldsByTag[rt][key] = make(map[string]string)
    }

    for i := 0; i < rt.NumField(); i++ {
        f := rt.Field(i)
        v := strings.Split(f.Tag.Get(key), ",")[0] // use split to ignore tag "options"
        if v == "" || v == "-" {
            continue
        }
        fieldsByTag[rt][key][v] = f.Name
    }
}

https://play.golang.com/p/qlt_mWsXGju

like image 198
mkopriva Avatar answered Nov 05 '22 11:11

mkopriva


I worked over other solutions and developed my get boolean field value from json tag value. You can change it according to your scenario easily.

func GetBooleanFieldValueByJsonTag(jsonTagValue string, s interface{}) bool {
    rt := reflect.TypeOf(s)
    if rt.Kind() != reflect.Struct {
        return false
    }
    for i := 0; i < rt.NumField(); i++ {
        f := rt.Field(i)
        v := strings.Split(f.Tag.Get("json"), ",")[0] // use split to ignore tag "options" like omitempty, etc.
        if v == jsonTagValue {
            r := reflect.ValueOf(s)
            field := reflect.Indirect(r).FieldByName(f.Name)
            return field.Bool()
        }
    }
    return false
}
like image 1
Mustafa Akçakaya Avatar answered Nov 05 '22 13:11

Mustafa Akçakaya