Skip to content

Commit 27f0c11

Browse files
author
Tao Wen
committed
support empty struct
1 parent 6c91989 commit 27f0c11

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

gen/main.go

+7
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,12 @@ func genMap(mapType *ast.MapType) {
204204
}
205205

206206
func genStruct(structType *ast.StructType) {
207+
oldLines := lines
207208
_l(" more := iter.ReadObjectHead()")
208209
_l(" for more {")
209210
_l(" field := iter.ReadObjectField()")
210211
_l(" switch {")
212+
isEmptyStruct := true
211213
for _, field := range structType.Fields.List {
212214
fieldName := field.Names[0].Name
213215
encodedFieldName := fieldName
@@ -230,10 +232,15 @@ func genStruct(structType *ast.StructType) {
230232
if isNotExported {
231233
continue
232234
}
235+
isEmptyStruct = false
233236
ptr := fmt.Sprintf("&(*out).%s", fieldName)
234237
_f(" case field == `%s`:", encodedFieldName)
235238
genDecodeStmt(field.Type, ptr)
236239
}
240+
if isEmptyStruct {
241+
lines = oldLines
242+
return
243+
}
237244
_l(" default:")
238245
_l(" iter.Skip()")
239246
_l(" }")

value_tests/EmptyStruct.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package value_tests
2+
3+
//go:generate go run github.com/json-iterator/tinygo/gen
4+
type EmptyStruct struct {
5+
}

value_tests/EmptyStruct_json.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package value_tests
2+
3+
import jsoniter "github.com/json-iterator/tinygo"
4+
5+
func EmptyStruct_json_unmarshal(iter *jsoniter.Iterator, out *EmptyStruct) {
6+
}
7+
type EmptyStruct_json struct {
8+
}
9+
func (json EmptyStruct_json) Type() interface{} {
10+
var val EmptyStruct
11+
return &val
12+
}
13+
func (json EmptyStruct_json) Unmarshal(iter *jsoniter.Iterator, val interface{}) {
14+
EmptyStruct_json_unmarshal(iter, val.(*EmptyStruct))
15+
}

0 commit comments

Comments
 (0)