koding / tunnel

Tunnel proxy package in Go
BSD 3-Clause "New" or "Revised" License
322 stars 71 forks source link

client: ClientState json marshalling #22

Closed mmatczuk closed 8 years ago

mmatczuk commented 8 years ago

ClientState json Marshaller and UnMarshaller implementation generated using https://github.com/campoy/jsonenums.

rjeczalik commented 8 years ago

Hey @mmatczuk, unfortunately we can't implement custom json.{Marshaler,Unmarshaler} for ClientState, as this change would break existing code (e.g. we encode ClientState in JSON and store it internaly in DB, after update the values wouldn't be decoded anymore). Moreover this would open surface for other PRs e.g. regarding missing xml.Marshaler, yaml.Marshaler or other encodings, which I don't think should be done in the tunnel library.

The best solution that I strongly encourage is to write your own models and enums, and keep the encoding logic there - this way you'd have full control over the encoding/decoding.

type YourModel struct {
    ...
    State TunnelState
    ...
}

type TunnelState tunnel.ClientState

var (
    TunnelUnknown = tunnel.ClientUnknown
    TunnelStarted = tunnel.ClientStarted
    ...
)

func (t TunnelState) MarshalJSON() ([]byte, error) { ... }
func (t TunnelState) UnmarshalJSON([]byte) error { ... }

func (t TunnelState) MarshalBinary() ([]byte, error) { ... }
func (t TunnelState) UnmarshalBinary([]byte) error { ... }

...

WDYT?

mmatczuk commented 8 years ago

Braking any code is not worth adding this change. Please close this request. Thanks for advise, this is actually what I did before sending PR.

rjeczalik commented 8 years ago

@mmatczuk 👍