gitee.com/zhongguo168a/gocodes@v0.0.0-20230609140523-e1828349603f/datax/reflectx/ref-desc-decl-field.go (about)

     1  package reflectx
     2  
     3  import (
     4  	"gitee.com/zhongguo168a/gocodes/datax/schemax"
     5  )
     6  
     7  type FieldRef struct {
     8  }
     9  
    10  func (f *FieldRef) RefMapGet(target IRefObject, field string, key string) (val interface{}, isNil bool) {
    11  	obj := target.(*schemax.Field)
    12  	switch field {
    13  	case "Tags":
    14  		ival, ok := obj.Tags[key]
    15  		return ival, ok
    16  	}
    17  	return nil, true
    18  }
    19  
    20  func (f *FieldRef) RefMapSet(target IRefObject, field string, key string, val interface{}) {
    21  	obj := target.(*schemax.Field)
    22  	switch field {
    23  	case "Tags":
    24  		if obj.Tags == nil {
    25  			obj.Tags = map[string]string{}
    26  		}
    27  		obj.Tags[key] = val.(string)
    28  	}
    29  }
    30  
    31  func (f *FieldRef) RefMapKeys(target IRefObject, field string) (r []string) {
    32  	obj := target.(*schemax.Field)
    33  	switch field {
    34  	case "Tags":
    35  		for key := range obj.Tags {
    36  			r = append(r, key)
    37  		}
    38  	}
    39  	return
    40  }
    41  
    42  func (f *FieldRef) RefMapNew(field string) (r interface{}) {
    43  	switch field {
    44  	case "Tags":
    45  		r = make(map[string]string)
    46  	}
    47  	return
    48  }
    49  
    50  func (f *FieldRef) RefSliceGet(target IRefObject, field string, index int) (val interface{}, isNil bool) {
    51  	obj := target.(*schemax.Field)
    52  	switch field {
    53  	case "Comments":
    54  		val := obj.Comments[index]
    55  		return val, false
    56  	}
    57  	return nil, true
    58  }
    59  
    60  func (f *FieldRef) RefSliceSet(target IRefObject, field string, index int, val interface{}) {
    61  	obj := target.(*schemax.Field)
    62  	switch field {
    63  	case "Comments":
    64  		obj.Comments[index] = val.(string)
    65  	}
    66  }
    67  
    68  func (f *FieldRef) RefSliceNew(field string, len int, cap int) (r interface{}) {
    69  	switch field {
    70  	case "Comments":
    71  		r = make([]string, len, cap)
    72  	}
    73  	return
    74  }
    75  
    76  func (f *FieldRef) RefSliceLength(target IRefObject, field string) (r int) {
    77  	obj := target.(*schemax.Field)
    78  	switch field {
    79  	case "Comments":
    80  		r = len(obj.Comments)
    81  	}
    82  	return
    83  }
    84  
    85  func (f *FieldRef) RefHas(field string) bool {
    86  	switch field {
    87  	case "Name":
    88  		return true
    89  	case "Type":
    90  		return true
    91  	case "Value":
    92  		return true
    93  	}
    94  	return false
    95  }
    96  
    97  func (f *FieldRef) RefGet(target IRefObject, field string) (interface{}, bool) {
    98  	obj := target.(*schemax.Field)
    99  	switch field {
   100  	case "Name":
   101  		return obj.Name, false
   102  	case "Type":
   103  		val := obj.Type
   104  		return val, val == nil
   105  	case "Value":
   106  		return obj.Value, false
   107  	case "Tags":
   108  		return obj.Tags, obj.Tags == nil
   109  	}
   110  	return nil, true
   111  }
   112  
   113  func (f *FieldRef) RefSet(target IRefObject, field string, val interface{}) {
   114  	obj := target.(*schemax.Field)
   115  	switch field {
   116  	case "Name":
   117  		obj.Name = val.(string)
   118  	case "Type":
   119  		obj.Type = val.(schemax.IType)
   120  	case "Value":
   121  		obj.Value = val.(string)
   122  	}
   123  	return
   124  }
   125  
   126  func (f *FieldRef) RefNew() IRefObject {
   127  	return &schemax.Field{}
   128  }
   129  
   130  func (f *FieldRef) RefType() string {
   131  	return "field"
   132  }