func structToMap()

in cmd/resource/utils.go [674:708]


func structToMap(item interface{}) map[string]interface{} {
	res := map[string]interface{}{}
	if item == nil {
		return res
	}
	v := reflect.TypeOf(item)
	reflectValue := reflect.ValueOf(item)
	reflectValue = reflect.Indirect(reflectValue)

	if v.Kind() == reflect.Ptr {
		v = v.Elem()
	}
	for i := 0; i < v.NumField(); i++ {
		tag := v.Field(i).Tag.Get("json")
		if reflectValue.Field(i).CanInterface() {
			field := reflectValue.Field(i).Interface()
			keyName := tag
			if tag != "" && tag != "-" {
				if index := strings.Index(tag, ","); index != -1 {
					if strings.Index(tag[index+1:], "omitempty") != -1 && IsZero(field) {
						continue
					}
					keyName = v.Field(i).Name
				}
				switch v.Field(i).Type.Kind() {
				case reflect.Struct:
					res[keyName] = structToMap(field)
				default:
					res[keyName] = stringify(field)
				}
			}
		}
	}
	return res
}