mardi 2 mai 2023

How to access the embedded struct inside the SliceOfPointers field of a Struct

I want to add functionality to take the first element when the data is []*struct.

func getEncFields(t reflect.Type, list map[string]int) {
    for t.Kind() == reflect.Ptr {
        t = t.Elem()
    }
    if t.Kind() == reflect.Struct {
        for i := 0; i < t.NumField(); i++ {
            field := t.Field(i)
            tag := field.Tag.Get("bson")
            if containsTag(tag, "encr") {
                list[getFieldName(field, tag)]++
            }
            getEncFields(field.Type, list)
        }
    }

In this code I need to add functionality when the data is []*Struct. Here is the struct type to be passed in this function.

type Customer struct {
    Name     string    `json:"name" bson:"name"`
    Acnumber int64     `json:"acnumber" bson:"acnumber,encr"`
    Number   int64     `json:"number" bson:"number,encr"`
    Address  []Address `json:"address" bson:"address"`
}
type Address struct {
    Mail string `json:"mail" bson:"mail,encr"`
}

Thank you for your support





Aucun commentaire:

Enregistrer un commentaire