Skip to content

Commit b761d1a

Browse files
committed
Don't parse struct field type if it's omitted from JSON
1 parent b2bdccc commit b761d1a

File tree

2 files changed

+37
-36
lines changed

2 files changed

+37
-36
lines changed

internal/parser/regex.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type JsonTag struct {
1919
Value string
2020
IsString bool
2121
Omitempty bool
22+
Inline bool
2223
}
2324

2425
func GetJsonTag(structTags string) (JsonTag, bool) {
@@ -41,6 +42,7 @@ func GetJsonTag(structTags string) (JsonTag, bool) {
4142
Value: submatches[1] + submatches[2],
4243
IsString: strings.Contains(submatches[2], ",string"),
4344
Omitempty: strings.Contains(submatches[2], ",omitempty"),
45+
Inline: strings.Contains(submatches[2], ",inline"),
4446
}
4547

4648
return jsonTag, true

internal/parser/struct.go

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package parser
33
import (
44
"fmt"
55
"go/types"
6-
"strings"
76

87
"github.com/webrpc/webrpc/schema"
98
)
@@ -21,7 +20,12 @@ func (p *Parser) ParseStruct(typeName string, structTyp *types.Struct) (*schema.
2120
}
2221
structTags := structTyp.Tag(i)
2322

24-
if structField.Embedded() || strings.Contains(structTags, `json:",inline"`) {
23+
jsonTag, _ := GetJsonTag(structTags)
24+
if jsonTag.Name == "-" { // struct field ignored by `json:"-"` struct tag
25+
continue
26+
}
27+
28+
if structField.Embedded() || jsonTag.Inline {
2529
varType, err := p.ParseNamedType("", structField.Type())
2630
if err != nil {
2731
return nil, fmt.Errorf("parsing var %v: %w", structField.Name(), err)
@@ -35,7 +39,7 @@ func (p *Parser) ParseStruct(typeName string, structTyp *types.Struct) (*schema.
3539
continue
3640
}
3741

38-
field, err := p.parseStructField(typeName+"Field", structField, structTags)
42+
field, err := p.parseStructField(typeName+"Field", structField, jsonTag)
3943
if err != nil {
4044
return nil, fmt.Errorf("parsing struct field %v: %w", i, err)
4145
}
@@ -58,7 +62,7 @@ func (p *Parser) ParseStruct(typeName string, structTyp *types.Struct) (*schema.
5862

5963
// parses single Go struct field
6064
// if the field is embedded, ie. `json:",inline"`, parse recursively
61-
func (p *Parser) parseStructField(structTypeName string, field *types.Var, structTags string) (*schema.TypeField, error) {
65+
func (p *Parser) parseStructField(structTypeName string, field *types.Var, jsonTag JsonTag) (*schema.TypeField, error) {
6266
fieldName := field.Name()
6367
fieldType := field.Type()
6468

@@ -68,48 +72,43 @@ func (p *Parser) parseStructField(structTypeName string, field *types.Var, struc
6872

6973
goFieldImport := p.GoTypeImport(fieldType)
7074

71-
jsonTag, ok := GetJsonTag(structTags)
72-
if ok {
75+
if jsonTag.Name != "" {
7376
if jsonTag.Name == "-" { // struct field ignored by `json:"-"` struct tag
7477
return nil, nil
7578
}
79+
jsonFieldName = jsonTag.Name
80+
}
7681

77-
if jsonTag.Name != "" {
78-
jsonFieldName = jsonTag.Name
79-
}
80-
82+
if jsonTag.Omitempty {
8183
optional = jsonTag.Omitempty
82-
if optional {
83-
goFieldType = "*" + goFieldType
84-
}
85-
86-
if jsonTag.IsString { // struct field forced to be string by `json:",string"`
84+
goFieldType = "*" + goFieldType
85+
}
8786

88-
structField := &schema.TypeField{
89-
Name: jsonFieldName,
90-
Type: &schema.VarType{
91-
Expr: "string",
92-
Type: schema.T_String,
93-
},
94-
TypeExtra: schema.TypeExtra{
95-
Meta: []schema.TypeFieldMeta{
96-
{"go.field.name": fieldName},
97-
{"go.field.type": goFieldType},
98-
},
99-
Optional: optional,
87+
if jsonTag.IsString { // struct field forced to be string by `json:",string"`
88+
structField := &schema.TypeField{
89+
Name: jsonFieldName,
90+
Type: &schema.VarType{
91+
Expr: "string",
92+
Type: schema.T_String,
93+
},
94+
TypeExtra: schema.TypeExtra{
95+
Meta: []schema.TypeFieldMeta{
96+
{"go.field.name": fieldName},
97+
{"go.field.type": goFieldType},
10098
},
101-
}
102-
if goFieldImport != "" {
103-
structField.TypeExtra.Meta = append(structField.TypeExtra.Meta,
104-
schema.TypeFieldMeta{"go.type.import": goFieldImport},
105-
)
106-
}
99+
Optional: optional,
100+
},
101+
}
102+
if goFieldImport != "" {
107103
structField.TypeExtra.Meta = append(structField.TypeExtra.Meta,
108-
schema.TypeFieldMeta{"go.tag.json": jsonTag.Value},
104+
schema.TypeFieldMeta{"go.type.import": goFieldImport},
109105
)
110-
111-
return structField, nil
112106
}
107+
structField.TypeExtra.Meta = append(structField.TypeExtra.Meta,
108+
schema.TypeFieldMeta{"go.tag.json": jsonTag.Value},
109+
)
110+
111+
return structField, nil
113112
}
114113

115114
if _, ok := field.Type().Underlying().(*types.Pointer); ok {

0 commit comments

Comments
 (0)