gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/yaml/decode_test.go (about)

     1  package yaml
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/sy_183/go-common/assert"
     6  	"os"
     7  	"testing"
     8  )
     9  
    10  type DecodeTestBase struct {
    11  	String  string  `yaml:"string"`
    12  	Int     int     `yaml:"int"`
    13  	Uint    uint    `yaml:"uint"`
    14  	Float64 float64 `yaml:"float64"`
    15  
    16  	Strings []string  `yaml:"strings"`
    17  	Ints    []int     `yaml:"ints"`
    18  	Uints   []uint    `yaml:"uints"`
    19  	Floats  []float64 `yaml:"floats"`
    20  
    21  	StringMap  map[string]string   `yaml:"stringMap"`
    22  	StringsMap map[string][]string `yaml:"stringsMap"`
    23  	JsonMap    map[string]any      `yaml:"jsonMap"`
    24  	GeneralMap map[any]any         `yaml:"generalMap"`
    25  
    26  	Any  any  `yaml:"any"`
    27  	Node Node `yaml:"node"`
    28  }
    29  
    30  type DecodeTestMapBase map[string]any
    31  
    32  func (b *DecodeTestMapBase) Set(key string, value any) {
    33  	if *b == nil {
    34  		*b = make(DecodeTestMapBase)
    35  	}
    36  	(*b)[key] = value
    37  }
    38  
    39  func (b *DecodeTestMapBase) UnmarshalYAMLEntry(key *Node, value *Node) (ok bool, err error) {
    40  	if key.Kind == ScalarNode {
    41  		switch key.Value {
    42  		case "string":
    43  			var v string
    44  			assert.MustSuccess(value.Decode(&v))
    45  			b.Set(key.Value, v)
    46  		case "int":
    47  			var v int
    48  			assert.MustSuccess(value.Decode(&v))
    49  			b.Set(key.Value, v)
    50  		case "uint":
    51  			var v uint
    52  			assert.MustSuccess(value.Decode(&v))
    53  			b.Set(key.Value, v)
    54  		case "float64":
    55  			var v float64
    56  			assert.MustSuccess(value.Decode(&v))
    57  			b.Set(key.Value, v)
    58  		case "strings":
    59  			var v []string
    60  			assert.MustSuccess(value.Decode(&v))
    61  			b.Set(key.Value, v)
    62  		case "ints":
    63  			var v []int
    64  			assert.MustSuccess(value.Decode(&v))
    65  			b.Set(key.Value, v)
    66  		case "uints":
    67  			var v []uint
    68  			assert.MustSuccess(value.Decode(&v))
    69  			b.Set(key.Value, v)
    70  		case "floats":
    71  			var v []float64
    72  			assert.MustSuccess(value.Decode(&v))
    73  			b.Set(key.Value, v)
    74  		case "stringMap":
    75  			var v map[string]string
    76  			assert.MustSuccess(value.Decode(&v))
    77  			b.Set(key.Value, v)
    78  		case "stringsMap":
    79  			var v map[string][]string
    80  			assert.MustSuccess(value.Decode(&v))
    81  			b.Set(key.Value, v)
    82  		case "jsonMap":
    83  			var v map[string]any
    84  			assert.MustSuccess(value.Decode(&v))
    85  			b.Set(key.Value, v)
    86  		case "generalMap":
    87  			var v map[any]any
    88  			assert.MustSuccess(value.Decode(&v))
    89  			b.Set(key.Value, v)
    90  		case "any":
    91  			var v any
    92  			assert.MustSuccess(value.Decode(&v))
    93  			b.Set(key.Value, v)
    94  		case "node":
    95  			var v Node
    96  			assert.MustSuccess(value.Decode(&v))
    97  			b.Set(key.Value, v)
    98  		default:
    99  			return false, nil
   100  		}
   101  		return true, err
   102  	}
   103  	return false, nil
   104  }
   105  
   106  type DecodeTestStructMapBase struct {
   107  	String  string         `yaml:"string"`
   108  	Int     int            `yaml:"int"`
   109  	Uint    uint           `yaml:"uint"`
   110  	Float64 float64        `yaml:"float64"`
   111  	Other   map[string]any `yaml:"-"`
   112  }
   113  
   114  func (b *DecodeTestStructMapBase) Set(key string, value any) {
   115  	if b.Other == nil {
   116  		b.Other = make(DecodeTestMapBase)
   117  	}
   118  	b.Other[key] = value
   119  }
   120  
   121  func (b *DecodeTestStructMapBase) UnmarshalYAMLEntry(key *Node, value *Node) (ok bool, err error) {
   122  	if key.Kind == ScalarNode {
   123  		switch key.Value {
   124  		case "strings":
   125  			var v []string
   126  			assert.MustSuccess(value.Decode(&v))
   127  			b.Set(key.Value, v)
   128  		case "ints":
   129  			var v []int
   130  			assert.MustSuccess(value.Decode(&v))
   131  			b.Set(key.Value, v)
   132  		case "uints":
   133  			var v []uint
   134  			assert.MustSuccess(value.Decode(&v))
   135  			b.Set(key.Value, v)
   136  		case "floats":
   137  			var v []float64
   138  			assert.MustSuccess(value.Decode(&v))
   139  			b.Set(key.Value, v)
   140  		case "stringMap":
   141  			var v map[string]string
   142  			assert.MustSuccess(value.Decode(&v))
   143  			b.Set(key.Value, v)
   144  		case "stringsMap":
   145  			var v map[string][]string
   146  			assert.MustSuccess(value.Decode(&v))
   147  			b.Set(key.Value, v)
   148  		case "jsonMap":
   149  			var v map[string]any
   150  			assert.MustSuccess(value.Decode(&v))
   151  			b.Set(key.Value, v)
   152  		case "generalMap":
   153  			var v map[any]any
   154  			assert.MustSuccess(value.Decode(&v))
   155  			b.Set(key.Value, v)
   156  		case "any":
   157  			var v any
   158  			assert.MustSuccess(value.Decode(&v))
   159  			b.Set(key.Value, v)
   160  		case "node":
   161  			var v Node
   162  			assert.MustSuccess(value.Decode(&v))
   163  			b.Set(key.Value, v)
   164  		default:
   165  			return false, nil
   166  		}
   167  		return true, err
   168  	}
   169  	return false, nil
   170  }
   171  
   172  type DecodeTest struct {
   173  	Base          DecodeTestBase          `yaml:"base"`
   174  	BaseP         *DecodeTestBase         `yaml:"baseP"`
   175  	MapBase       DecodeTestMapBase       `yaml:"mapBase"`
   176  	MapBaseP      *DecodeTestMapBase      `yaml:"mapBaseP"`
   177  	StructMapBase DecodeTestStructMapBase `yaml:"structMapBase"`
   178  }
   179  
   180  func TestDecode(t *testing.T) {
   181  	s := &DecodeTest{}
   182  	y := assert.Must(os.ReadFile("decode_test.yaml"))
   183  	assert.MustSuccess(Unmarshal(y, &s))
   184  	node := Node{}
   185  	assert.MustSuccess(Unmarshal(y, &node))
   186  	fmt.Println(s)
   187  	os.Stdout.Write(assert.Must(Marshal(&node)))
   188  }