mirror of
https://github.com/netbirdio/easyjson.git
synced 2026-05-22 18:44:42 -07:00
Fix unmarshaling struct with interface field which is a pointer to a struct
This commit is contained in:
+7
-2
@@ -212,8 +212,13 @@ func (g *Generator) genTypeDecoderNoCheck(t reflect.Type, out string, tags field
|
||||
if t.NumMethod() != 0 {
|
||||
return fmt.Errorf("interface type %v not supported: only interface{} is allowed", t)
|
||||
}
|
||||
fmt.Fprintln(g.out, ws+out+" = in.Interface()")
|
||||
|
||||
fmt.Fprintln(g.out, ws+"if m, ok := "+out+".(easyjson.Unmarshaler); ok {")
|
||||
fmt.Fprintln(g.out, ws+"m.UnmarshalEasyJSON(in)")
|
||||
fmt.Fprintln(g.out, ws+"} else if m, ok := "+out+".(json.Unmarshaler); ok {")
|
||||
fmt.Fprintln(g.out, ws+"m.UnmarshalJSON(in.Raw())")
|
||||
fmt.Fprintln(g.out, ws+"} else {")
|
||||
fmt.Fprintln(g.out, ws+" "+out+" = in.Interface()")
|
||||
fmt.Fprintln(g.out, ws+"}")
|
||||
default:
|
||||
return fmt.Errorf("don't know how to decode %v", t)
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
|
||||
const pkgWriter = "github.com/mailru/easyjson/jwriter"
|
||||
const pkgLexer = "github.com/mailru/easyjson/jlexer"
|
||||
const pkgEasyJson = "github.com/mailru/easyjson"
|
||||
|
||||
// FieldNamer defines a policy for generating names for struct fields.
|
||||
type FieldNamer interface {
|
||||
@@ -59,6 +60,7 @@ func NewGenerator(filename string) *Generator {
|
||||
imports: map[string]string{
|
||||
pkgWriter: "jwriter",
|
||||
pkgLexer: "jlexer",
|
||||
pkgEasyJson: "easyjson",
|
||||
"encoding/json": "json",
|
||||
},
|
||||
fieldNamer: DefaultFieldNamer{},
|
||||
@@ -162,6 +164,7 @@ func (g *Generator) printHeader() {
|
||||
fmt.Println(" _ = json.RawMessage{}")
|
||||
fmt.Println(" _ = jlexer.Lexer{}")
|
||||
fmt.Println(" _ = jwriter.Writer{}")
|
||||
fmt.Println(" _ easyjson.Marshaler")
|
||||
fmt.Println(")")
|
||||
|
||||
fmt.Println()
|
||||
|
||||
Reference in New Issue
Block a user