github.com/shuguocloud/go-zero@v1.3.0/core/mapping/fieldoptions.go (about)

     1  package mapping
     2  
     3  import "fmt"
     4  
     5  const notSymbol = '!'
     6  
     7  type (
     8  	// use context and OptionalDep option to determine the value of Optional
     9  	// nothing to do with context.Context
    10  	fieldOptionsWithContext struct {
    11  		FromString bool
    12  		Optional   bool
    13  		Options    []string
    14  		Default    string
    15  		Range      *numberRange
    16  	}
    17  
    18  	fieldOptions struct {
    19  		fieldOptionsWithContext
    20  		OptionalDep string
    21  	}
    22  
    23  	numberRange struct {
    24  		left         float64
    25  		leftInclude  bool
    26  		right        float64
    27  		rightInclude bool
    28  	}
    29  )
    30  
    31  func (o *fieldOptionsWithContext) fromString() bool {
    32  	return o != nil && o.FromString
    33  }
    34  
    35  func (o *fieldOptionsWithContext) getDefault() (string, bool) {
    36  	if o == nil {
    37  		return "", false
    38  	}
    39  
    40  	return o.Default, len(o.Default) > 0
    41  }
    42  
    43  func (o *fieldOptionsWithContext) optional() bool {
    44  	return o != nil && o.Optional
    45  }
    46  
    47  func (o *fieldOptionsWithContext) options() []string {
    48  	if o == nil {
    49  		return nil
    50  	}
    51  
    52  	return o.Options
    53  }
    54  
    55  func (o *fieldOptions) optionalDep() string {
    56  	if o == nil {
    57  		return ""
    58  	}
    59  
    60  	return o.OptionalDep
    61  }
    62  
    63  func (o *fieldOptions) toOptionsWithContext(key string, m Valuer, fullName string) (
    64  	*fieldOptionsWithContext, error) {
    65  	var optional bool
    66  	if o.optional() {
    67  		dep := o.optionalDep()
    68  		if len(dep) == 0 {
    69  			optional = true
    70  		} else if dep[0] == notSymbol {
    71  			dep = dep[1:]
    72  			if len(dep) == 0 {
    73  				return nil, fmt.Errorf("wrong optional value for %q in %q", key, fullName)
    74  			}
    75  
    76  			_, baseOn := m.Value(dep)
    77  			_, selfOn := m.Value(key)
    78  			if baseOn == selfOn {
    79  				return nil, fmt.Errorf("set value for either %q or %q in %q", dep, key, fullName)
    80  			}
    81  
    82  			optional = baseOn
    83  		} else {
    84  			_, baseOn := m.Value(dep)
    85  			_, selfOn := m.Value(key)
    86  			if baseOn != selfOn {
    87  				return nil, fmt.Errorf("values for %q and %q should be both provided or both not in %q",
    88  					dep, key, fullName)
    89  			}
    90  
    91  			optional = !baseOn
    92  		}
    93  	}
    94  
    95  	if o.fieldOptionsWithContext.Optional == optional {
    96  		return &o.fieldOptionsWithContext, nil
    97  	}
    98  
    99  	return &fieldOptionsWithContext{
   100  		FromString: o.FromString,
   101  		Optional:   optional,
   102  		Options:    o.Options,
   103  		Default:    o.Default,
   104  	}, nil
   105  }