Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.

Commit 7c98b09

Browse files
committed
update tests and example of enum type without value.
1 parent 0347833 commit 7c98b09

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

README.md

+5-7
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,11 @@ type isOption interface {
9494
type Option0 struct {
9595
Data uint32
9696
}
97-
type Option1 struct {
98-
Data uint64
99-
}
97+
type Option1 struct {} // Empty enum variant
10098
type Option2 bool
10199
// Variants should implement isOption
102100
func (*Option0) isOption() {}
103-
func (*Option1) isOption() {}
101+
func (Option1) isOption() {}
104102
func (Option2) isOption() {}
105103

106104
// MyStruct contains the enum type Option
@@ -110,8 +108,8 @@ type MyStruct struct {
110108
List2D [][]isOption `lcs:"enum:dummy"` // support multi-dim slices
111109
}
112110

113-
// EnumTypes implement lcs.EnumTypeUser. It returns the ingredients used for
114-
// all enum types.
111+
// EnumTypes implement lcs.EnumTypeUser. It returns all the ingredients that can be
112+
// used for all enum fields in the receiver struct type.
115113
func (*Option) EnumTypes() []EnumVariant {
116114
return []EnumVariant{
117115
{
@@ -122,7 +120,7 @@ func (*Option) EnumTypes() []EnumVariant {
122120
{
123121
Name: "dummy",
124122
Value: 1,
125-
Template: (*Option1)(nil),
123+
Template: Option1{},
126124
},
127125
{
128126
Name: "dummy",

codec_test.go

+19-16
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ func TestBasicStruct(t *testing.T) {
199199
}
200200

201201
runTest(t, []*testCase{
202+
{
203+
v: struct{}{},
204+
b: nil,
205+
name: "empty struct",
206+
},
202207
{
203208
v: MyStruct{
204209
Boolean: true,
@@ -338,9 +343,7 @@ func TestMap(t *testing.T) {
338343
type Option0 struct {
339344
Data uint32
340345
}
341-
type Option1 struct {
342-
Data uint64
343-
}
346+
type Option1 struct{}
344347
type Option2 bool
345348
type isOption interface {
346349
isOption()
@@ -350,7 +353,7 @@ type Option struct {
350353
}
351354

352355
func (*Option0) isOption() {}
353-
func (*Option1) isOption() {}
356+
func (Option1) isOption() {}
354357
func (Option2) isOption() {}
355358
func (*Option) EnumTypes() []EnumVariant {
356359
return []EnumVariant{
@@ -362,7 +365,7 @@ func (*Option) EnumTypes() []EnumVariant {
362365
{
363366
Name: "option",
364367
Value: 1,
365-
Template: (*Option1)(nil),
368+
Template: Option1{},
366369
},
367370
{
368371
Name: "option",
@@ -379,28 +382,28 @@ func TestEnum(t *testing.T) {
379382
Option: &Option0{5},
380383
},
381384
b: hexMustDecode("0000 0000 0500 0000"),
382-
name: "ptr to struct with ptr interface 0",
385+
name: "ptr to struct with ptr enum variant",
383386
},
384387
{
385388
v: &Option{
386-
Option: &Option1{6},
389+
Option: Option1{},
387390
},
388-
b: hexMustDecode("0100 0000 0600 0000 0000 0000"),
389-
name: "ptr to struct with ptr interface 1",
391+
b: hexMustDecode("0100 0000"),
392+
name: "ptr to struct with non-ptr empty variant",
390393
},
391394
{
392395
v: &Option{
393396
Option: Option2(true),
394397
},
395398
b: hexMustDecode("0200 0000 01"),
396-
name: "ptr to struct with real value as interface",
399+
name: "ptr to struct with real value as enum variant",
397400
},
398401
{
399402
v: Option{
400-
Option: &Option1{6},
403+
Option: Option1{},
401404
},
402-
b: hexMustDecode("0100 0000 0600 0000 0000 0000"),
403-
name: "struct with enum with real struct interface",
405+
b: hexMustDecode("0100 0000"),
406+
name: "non-ptr struct with variant",
404407
},
405408
})
406409
}
@@ -419,7 +422,7 @@ func (*Wrapper) EnumTypes() []EnumVariant {
419422
{
420423
Name: "option",
421424
Value: 1,
422-
Template: (*Option1)(nil),
425+
Template: Option1{},
423426
},
424427
{
425428
Name: "option",
@@ -435,11 +438,11 @@ func TestEnumSlice(t *testing.T) {
435438
v: &Wrapper{
436439
Option: []isOption{
437440
&Option0{5},
438-
&Option1{6},
441+
Option1{},
439442
Option2(true),
440443
},
441444
},
442-
b: hexMustDecode("03000000 00000000 05000000 01000000 0600000000000000 02000000 01"),
445+
b: hexMustDecode("03000000 00000000 05000000 01000000 02000000 01"),
443446
name: "enum slice",
444447
},
445448
})

0 commit comments

Comments
 (0)