github.com/lingyao2333/mo-zero@v1.4.1/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  		Inherit    bool
    12  		FromString bool
    13  		Optional   bool
    14  		Options    []string
    15  		Default    string
    16  		Range      *numberRange
    17  	}
    18  
    19  	fieldOptions struct {
    20  		fieldOptionsWithContext
    21  		OptionalDep string
    22  	}
    23  
    24  	numberRange struct {
    25  		left         float64
    26  		leftInclude  bool
    27  		right        float64
    28  		rightInclude bool
    29  	}
    30  )
    31  
    32  func (o *fieldOptionsWithContext) fromString() bool {
    33  	return o != nil && o.FromString
    34  }
    35  
    36  func (o *fieldOptionsWithContext) getDefault() (string, bool) {
    37  	if o == nil {
    38  		return "", false
    39  	}
    40  
    41  	return o.Default, len(o.Default) > 0
    42  }
    43  
    44  func (o *fieldOptionsWithContext) inherit() bool {
    45  	return o != nil && o.Inherit
    46  }
    47  
    48  func (o *fieldOptionsWithContext) optional() bool {
    49  	return o != nil && o.Optional
    50  }
    51  
    52  func (o *fieldOptionsWithContext) options() []string {
    53  	if o == nil {
    54  		return nil
    55  	}
    56  
    57  	return o.Options
    58  }
    59  
    60  func (o *fieldOptions) optionalDep() string {
    61  	if o == nil {
    62  		return ""
    63  	}
    64  
    65  	return o.OptionalDep
    66  }
    67  
    68  func (o *fieldOptions) toOptionsWithContext(key string, m Valuer, fullName string) (
    69  	*fieldOptionsWithContext, error) {
    70  	var optional bool
    71  	if o.optional() {
    72  		dep := o.optionalDep()
    73  		if len(dep) == 0 {
    74  			optional = true
    75  		} else if dep[0] == notSymbol {
    76  			dep = dep[1:]
    77  			if len(dep) == 0 {
    78  				return nil, fmt.Errorf("wrong optional value for %q in %q", key, fullName)
    79  			}
    80  
    81  			_, baseOn := m.Value(dep)
    82  			_, selfOn := m.Value(key)
    83  			if baseOn == selfOn {
    84  				return nil, fmt.Errorf("set value for either %q or %q in %q", dep, key, fullName)
    85  			}
    86  
    87  			optional = baseOn
    88  		} else {
    89  			_, baseOn := m.Value(dep)
    90  			_, selfOn := m.Value(key)
    91  			if baseOn != selfOn {
    92  				return nil, fmt.Errorf("values for %q and %q should be both provided or both not in %q",
    93  					dep, key, fullName)
    94  			}
    95  
    96  			optional = !baseOn
    97  		}
    98  	}
    99  
   100  	if o.fieldOptionsWithContext.Optional == optional {
   101  		return &o.fieldOptionsWithContext, nil
   102  	}
   103  
   104  	return &fieldOptionsWithContext{
   105  		FromString: o.FromString,
   106  		Optional:   optional,
   107  		Options:    o.Options,
   108  		Default:    o.Default,
   109  	}, nil
   110  }