Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for inline struct tag option #533

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,64 @@ func TestAnonymousFields(t *testing.T) {
return S{}
},
want: `{}`,
}, {
// Structs with an inline tag option should be embedded.
label: "InlinedStruct",
makeInput: func() interface{} {
type (
S2 struct{ X int }
S struct {
S2 `json:",inline"`
}
)
return S{S2: S2{X: 1}}
},
want: `{"X":1}`,
}, {
// Pointers to structs with an inline tag option should be embedded.
label: "InlinedStructPointer",
makeInput: func() interface{} {
type (
S2 struct{ X int }
S struct {
*S2 `json:",inline"`
}
)
return S{S2: &S2{X: 1}}
},
want: `{"X":1}`,
}, {
// Non-anymous generic struct with an inline tag option should
// be embedded.
label: "InlinedStructGeneric",
makeInput: func() interface{} {
type (
S1 struct {
X int
}
S2 struct {
Y int
}
S[T any] struct {
Embedded T `json:",inline"`
S2
}
)
return S[S1]{Embedded: S1{X: 1}, S2: S2{Y: 2}}
},
want: `{"X":1,"Y":2}`,
}, {
// Non-struct with an inline tag option should be serialized
label: "InlinedStructPointer",
makeInput: func() interface{} {
type (
S struct {
X int `json:",inline"`
}
)
return S{X: 1}
},
want: `{"X":1}`,
}}

for i, tt := range tests {
Expand Down
3 changes: 3 additions & 0 deletions internal/encoder/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,9 @@ func isEmbeddedStruct(field *StructFieldCode) bool {
if !field.isAnonymous {
return false
}
if field.tag.IsInline {
return true
}
t := field.typ
if t.Kind() == reflect.Ptr {
t = t.Elem()
Expand Down
2 changes: 1 addition & 1 deletion internal/encoder/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ func (c *Compiler) structFieldCode(structCode *StructCode, tag *runtime.StructTa
key: tag.Key,
tag: tag,
offset: field.Offset,
isAnonymous: field.Anonymous && !tag.IsTaggedKey && toElemType(fieldType).Kind() == reflect.Struct,
isAnonymous: (field.Anonymous || tag.IsInline) && !tag.IsTaggedKey && toElemType(fieldType).Kind() == reflect.Struct,
isTaggedKey: tag.IsTaggedKey,
isNilableType: c.isNilableType(fieldType),
isNilCheck: true,
Expand Down
3 changes: 3 additions & 0 deletions internal/runtime/struct_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func IsIgnoredStructField(field reflect.StructField) bool {

type StructTag struct {
Key string
IsInline bool
IsTaggedKey bool
IsOmitEmpty bool
IsString bool
Expand Down Expand Up @@ -84,6 +85,8 @@ func StructTagFromField(field reflect.StructField) *StructTag {
st.IsOmitEmpty = true
case "string":
st.IsString = true
case "inline":
st.IsInline = true
}
}
}
Expand Down