How to access the embedded struct inside the Slice of Pointers field of a Struct

huangapple go评论87阅读模式
英文:

How to access the embedded struct inside the Slice of Pointers field of a Struct

问题

我想要添加一个功能,当数据是[]*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)
		}
	}
}

在这段代码中,当数据是[]*Struct时,我需要添加功能。以下是要传递给此函数的结构类型。

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"`
}

谢谢您的支持。

英文:

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 &lt; t.NumField(); i++ {
			field := t.Field(i)
			tag := field.Tag.Get(&quot;bson&quot;)
			if containsTag(tag, &quot;encr&quot;) {
				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:&quot;name&quot; bson:&quot;name&quot;`
	Acnumber int64     `json:&quot;acnumber&quot; bson:&quot;acnumber,encr&quot;`
	Number   int64     `json:&quot;number&quot; bson:&quot;number,encr&quot;`
	Address  []*Address `json:&quot;address&quot; bson:&quot;address&quot;`
}
type Address struct {
	Mail string `json:&quot;mail&quot; bson:&quot;mail,encr&quot;`
}

Thank you for your support

答案1

得分: 3

通过指针,可以像遍历数组、切片和映射一样深入查看。

func getEncFields(t reflect.Type, list map[string]int) {
    for t.Kind() == reflect.Ptr || t.Kind() == reflect.Slice || t.Kind() == reflect.Array || t.Kind() == reflect.Map {
        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)
        }
    }
}

你可以在这里查看代码示例:https://go.dev/play/p/1msFGC89NX-

英文:

Drill down through arrays, slices and maps as you do for pointers.

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

https://go.dev/play/p/1msFGC89NX-

huangapple
  • 本文由 发表于 2023年5月2日 23:25:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76156434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定