mailru / easyjson

Fast JSON serializer for golang.
MIT License
4.48k stars 421 forks source link

Cannot use X (type Y) as type Z errors when compiling code with structs having custom types #5

Closed FZambia closed 8 years ago

FZambia commented 8 years ago

Sorry for confusing title, but the problem is follows. I have a struct that looks like this:

type Message struct {
    UID       MessageID        `json:"uid"`
    Timestamp string           `json:"timestamp"`
    Info      *ClientInfo      `json:"info,omitempty"`
    Channel   Channel          `json:"channel"`
    Data      *json.RawMessage `json:"data"`
    Client    ConnID           `json:"client,omitempty"`
}

After generating *_easyjson.go file I tried to build my source code but got errors:

./message_easyjson.go:23: cannot use in.String() (type string) as type MessageID in assignment
./message_easyjson.go:35: cannot use in.String() (type string) as type Channel in assignment
./message_easyjson.go:47: cannot use in.String() (type string) as type ConnID in assignment
./message_easyjson.go:62: cannot use in.UID (type MessageID) as type string in argument to out.String
./message_easyjson.go:80: cannot use in.Channel (type Channel) as type string in argument to out.String
./message_easyjson.go:93: cannot use in.Client (type ConnID) as type string in argument to out.String
./message_easyjson.go:125: cannot use in.String() (type string) as type UserID in assignment
./message_easyjson.go:127: cannot use in.String() (type string) as type ConnID in assignment
./message_easyjson.go:162: cannot use in.User (type UserID) as type string in argument to out.String
./message_easyjson.go:166: cannot use in.Client (type ConnID) as type string in argument to out.String

Generated code - https://gist.github.com/FZambia/080a25f58ce81c510533

In my case types defined in this way:

type (
    Channel string
    ChannelID string
    UserID string
    ConnID string
    MessageID string
)

And ClientInfo is

type ClientInfo struct {
    User        UserID           `json:"user"`
    Client      ConnID           `json:"client"`
    DefaultInfo *json.RawMessage `json:"default_info,omitempty"`
    ChannelInfo *json.RawMessage `json:"channel_info,omitempty"`
}

How this can be fixed?

FZambia commented 8 years ago

Thanks!