github.com/sandwich-go/boost@v1.3.29/xmap/internal/template2/equal_template.go (about)

     1  package template2
     2  
     3  import "sort"
     4  
     5  type EqualConfig struct {
     6  	Key, Value string
     7  }
     8  
     9  func GetEqualTPLArgs() interface{} {
    10  	var equalConfigs = make([]EqualConfig, 0)
    11  	var types = []string{
    12  		"int", "int8", "int16", "int32", "int64",
    13  		"uint", "uint8", "uint16", "uint32", "uint64",
    14  		"float32", "float64",
    15  		"string",
    16  	}
    17  	for _, tk := range types {
    18  		for _, tv := range types {
    19  			equalConfigs = append(equalConfigs, EqualConfig{
    20  				Key:   tk,
    21  				Value: tv,
    22  			})
    23  		}
    24  	}
    25  	sort.Slice(equalConfigs, func(i, j int) bool {
    26  		if equalConfigs[i].Key == equalConfigs[j].Key {
    27  			return equalConfigs[i].Value < equalConfigs[j].Value
    28  		}
    29  		return equalConfigs[i].Key < equalConfigs[j].Key
    30  	})
    31  	return map[string]interface{}{"EqualConfigs": equalConfigs}
    32  }
    33  
    34  const EqualTestTPL = `// Code generated by tools. DO NOT EDIT.
    35  package xmap
    36  
    37  import (
    38  	. "github.com/smartystreets/goconvey/convey"
    39  	"strconv"
    40  	"testing"
    41  )
    42  
    43  func TestEqualMap(t *testing.T) {
    44  {{- range $equalConfig := .EqualConfigs }}
    45  	{{- $camelCaseKey := $equalConfig.Key | CamelCase }}
    46  	{{- $camelCaseValue := $equalConfig.Value | CamelCase | trimSuffix "{}" }}
    47  	{{- $equalMapName := print "Equal" $camelCaseKey $camelCaseValue "Map" }}
    48  	Convey("{{ $equalMapName }}", t, func() {
    49  		So({{ $equalMapName }}(nil, nil), ShouldBeTrue)
    50  		var a, b = make(map[{{ $equalConfig.Key }}]{{ $equalConfig.Value }}), make(map[{{ $equalConfig.Key }}]{{ $equalConfig.Value }})
    51  		So({{ $equalMapName }}(a, nil), ShouldBeFalse)
    52  		So({{ $equalMapName }}(nil, a), ShouldBeFalse)
    53  		So({{ $equalMapName }}(a, b), ShouldBeTrue)
    54  
    55  		var key {{ $equalConfig.Key }}
    56  		var value {{ $equalConfig.Value }}
    57  		for i := 0; i {{ "<" | Unescaped }} 100; i++ {
    58  			{{- if eq $equalConfig.Key "string" }}
    59  			key = strconv.FormatInt(int64(i), 10)
    60  			{{- else if eq $equalConfig.Key "int" }}
    61  			key = i
    62  			{{- else }}
    63  			key = {{ $equalConfig.Key }}(i)
    64  			{{- end }}
    65  			{{- if eq $equalConfig.Value "string" }}
    66  			value = strconv.FormatInt(int64(i), 10)
    67  			{{- else if eq $equalConfig.Value "int" }}
    68  			value = i
    69  			{{- else }}
    70  			value = {{ $equalConfig.Value }}(i)
    71  			{{- end }}
    72  			a[key] = value
    73  			b[key] = value
    74  		}
    75  		So({{ $equalMapName }}(a, b), ShouldBeTrue)
    76  		delete(a, key)
    77  		So({{ $equalMapName }}(a, b), ShouldBeFalse)
    78  	})
    79  {{ end }}}
    80  `
    81  
    82  const EqualTPL = `// Code generated by tools. DO NOT EDIT.
    83  package xmap
    84  
    85  {{- range $equalConfig := .EqualConfigs }}
    86  {{- $camelCaseKey := $equalConfig.Key | CamelCase }}
    87  {{- $camelCaseValue := $equalConfig.Value | CamelCase | trimSuffix "{}" }}
    88  {{- $equalMapName := print "Equal" $camelCaseKey $camelCaseValue "Map" }}
    89  func {{ $equalMapName }}(a, b map[{{ $equalConfig.Key }}]{{ $equalConfig.Value }}) bool {
    90  	if a == nil && b == nil {
    91  		return true
    92  	}
    93  	if (a != nil && b == nil) || (b != nil && a == nil) || (len(a) != len(b)) {
    94  		return false
    95  	}
    96  	for k, v := range a {
    97  		v1, ok := b[k]
    98  		if !ok || v != v1 {
    99  			return false
   100  		}
   101  	}
   102  	return true
   103  }
   104  {{ end }}
   105  `