github.com/hedzr/evendeep@v0.4.8/flags/flags_lazyinit.go (about)

     1  package flags
     2  
     3  import (
     4  	"github.com/hedzr/evendeep/flags/cms"
     5  
     6  	"sync"
     7  )
     8  
     9  // lazyInitFieldTagsFlags initialize something.
    10  func lazyInitFieldTagsFlags() {
    11  	onceFieldTagsEquip.Do(func() {
    12  		// add := func(s string) { mKnownFieldTagFlags[fieldTagFlag.Parse(s)] = struct{}{} }
    13  
    14  		conflictsAdd("byordinal", "byname")
    15  
    16  		conflictsAdd("noomit", "omitempty", "omitnil", "omitzero")
    17  		conflictsAdd("noomittgt", "omitemptytgt", "omitniltgt", "omitzerotgt")
    18  
    19  		conflictsAdd("slicecopy", "slicecopyappend", "slicemerge")
    20  		conflictsAdd("mapcopy", "mapmerge")
    21  
    22  		// conflictsAdd("clearinvalid")
    23  		// conflictsAdd("cleareq")
    24  		// conflictsAdd("keepneq")
    25  
    26  		conflictsAdd("std", "-", "must")
    27  
    28  		mKnownStrategyGroup = []cms.CopyMergeStrategies{
    29  			{cms.ByOrdinal, cms.ByName},
    30  			{cms.NoOmit, cms.OmitIfEmpty, cms.OmitIfNil, cms.OmitIfZero},
    31  			{cms.NoOmitTarget, cms.OmitIfTargetEmpty, cms.OmitIfTargetNil, cms.OmitIfTargetZero},
    32  			{cms.SliceCopy, cms.SliceCopyAppend, cms.SliceMerge},
    33  			{cms.MapCopy, cms.MapMerge},
    34  			// {cms.ClearIfInvalid},
    35  			// {cms.ClearIfEq},
    36  			// {cms.KeepIfNotEq},
    37  			{cms.Default, cms.Ignore, cms.Must},
    38  			// {cms.Flat},
    39  		}
    40  	})
    41  }
    42  
    43  func conflictsAdd(ss ...string) {
    44  	// ss := strings.Split(s, ",")
    45  	if mKnownFieldTagFlagsConflict == nil {
    46  		mKnownFieldTagFlagsConflict = make(map[cms.CopyMergeStrategy]map[cms.CopyMergeStrategy]struct{})
    47  	}
    48  	if mKnownFieldTagFlagsConflictLeaders == nil {
    49  		mKnownFieldTagFlagsConflictLeaders = make(map[cms.CopyMergeStrategy]struct{})
    50  	}
    51  	for i, fr := range ss {
    52  		ftf := cms.Default.Parse(fr)
    53  		if i == 0 {
    54  			mKnownFieldTagFlagsConflictLeaders[ftf] = struct{}{}
    55  		}
    56  		if v, ok := mKnownFieldTagFlagsConflict[ftf]; !ok || (ok && v == nil) {
    57  			mKnownFieldTagFlagsConflict[ftf] = make(map[cms.CopyMergeStrategy]struct{})
    58  		}
    59  		for _, to := range ss {
    60  			if to != fr {
    61  				mKnownFieldTagFlagsConflict[ftf][cms.Default.Parse(to)] = struct{}{}
    62  			}
    63  		}
    64  	}
    65  }
    66  
    67  var onceFieldTagsEquip sync.Once //nolint:gochecknoglobals //i know that
    68  
    69  // var mKnownFieldTagFlags map[fieldTagFlag]struct{}
    70  
    71  var mKnownFieldTagFlagsConflict map[cms.CopyMergeStrategy]map[cms.CopyMergeStrategy]struct{} //nolint:lll,gochecknoglobals //i know that
    72  var mKnownFieldTagFlagsConflictLeaders map[cms.CopyMergeStrategy]struct{}                    //nolint:lll,gochecknoglobals //i know that
    73  var mKnownStrategyGroup []cms.CopyMergeStrategies                                            //nolint:lll,unused,gochecknoglobals //i know that
    74  // the toggleable radio groups