github.com/googollee/clic@v0.1.5/structtags/struct_parser.go (about)

     1  package structtags
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"strings"
     7  )
     8  
     9  type Field struct {
    10  	Name          []string
    11  	DefaultString string
    12  	Description   string
    13  	Parser        ParseFieldFunc
    14  	Value         reflect.Value
    15  }
    16  
    17  func (f Field) MarshalText() ([]byte, error) {
    18  	return fmt.Appendf(nil, "%v", f.Value.Interface()), nil
    19  }
    20  
    21  func (f Field) UnmarshalText(buf []byte) error {
    22  	return f.Parser(f.Value, string(buf))
    23  }
    24  
    25  func ParseStruct(v reflect.Value, name []string) ([]Field, error) {
    26  	if v.Kind() == reflect.Pointer {
    27  		v = v.Elem()
    28  	}
    29  
    30  	if v.Kind() != reflect.Struct {
    31  		return nil, fmt.Errorf("can't parse a %s type (names: %v)", v.Kind(), name)
    32  	}
    33  
    34  	var ret []Field
    35  
    36  	t := v.Type()
    37  	vfields := reflect.VisibleFields(t)
    38  
    39  	for _, vfield := range vfields {
    40  		if vfield.Anonymous {
    41  			continue
    42  		}
    43  
    44  		f := getFieldTag(vfield, name)
    45  
    46  		vfieldValue := v.FieldByIndex(vfield.Index)
    47  		vfieldType := vfieldValue.Type()
    48  
    49  		if vfieldValue.Kind() == reflect.Pointer {
    50  			vfieldType = vfieldType.Elem()
    51  
    52  			if vfieldValue.IsNil() {
    53  				vfieldValue.Set(reflect.New(vfieldType))
    54  			}
    55  
    56  			vfieldValue = vfieldValue.Elem()
    57  		}
    58  
    59  		if parser := getParseFieldFunc(vfieldType); parser != nil {
    60  			f.Parser = parser
    61  			f.Value = vfieldValue
    62  			if f.DefaultString != "" {
    63  				if err := f.Parser(f.Value, f.DefaultString); err != nil {
    64  					return nil, fmt.Errorf("can't parse default value %q for field %v: %w", f.DefaultString, f.Name, err)
    65  				}
    66  			}
    67  			ret = append(ret, f)
    68  			continue
    69  		}
    70  
    71  		fields, err := ParseStruct(vfieldValue, f.Name)
    72  		if err != nil {
    73  			return nil, err
    74  		}
    75  		ret = append(ret, fields...)
    76  	}
    77  
    78  	return ret, nil
    79  }
    80  
    81  func getFieldTag(sfield reflect.StructField, name []string) (ret Field) {
    82  	tagStr := sfield.Tag.Get("clic")
    83  	tagArray := strings.SplitN(tagStr, ",", 3)
    84  	name = name[:]
    85  
    86  	switch len(tagArray) {
    87  	case 3:
    88  		ret.Name = append(name, tagArray[0])
    89  		ret.DefaultString = tagArray[1]
    90  		ret.Description = tagArray[2]
    91  		return
    92  	case 2:
    93  		ret.Name = append(name, tagArray[0])
    94  		ret.DefaultString = tagArray[1]
    95  		return
    96  	case 1:
    97  		if tagStr != "" {
    98  			ret.Name = append(name, tagArray[0])
    99  			return
   100  		}
   101  	}
   102  
   103  	if sfield.Name != "" {
   104  		ret.Name = append(name, sfield.Name)
   105  	}
   106  
   107  	return
   108  }