package effects import ( "encoding/json" "errors" lucifer3 "git.aiterp.net/lucifer3/server" ) type serializedEffect struct { Manual *Manual `json:"manual,omitempty"` Solid *Solid `json:"solid,omitempty"` Gradient *Gradient `json:"gradient,omitempty"` Pattern *Pattern `json:"pattern,omitempty"` Random *Random `json:"random,omitempty"` VRange *VRange `json:"vrange,omitempty"` } type Serializable struct { Effect lucifer3.Effect } func (s *Serializable) UnmarshalJSON(raw []byte) error { value := serializedEffect{} err := json.Unmarshal(raw, &value) if err != nil { return err } switch { case value.Manual != nil: s.Effect = *value.Manual case value.Solid != nil: s.Effect = *value.Solid case value.Gradient != nil: s.Effect = *value.Gradient case value.Pattern != nil: s.Effect = *value.Pattern case value.Random != nil: s.Effect = *value.Random case value.VRange != nil: s.Effect = *value.VRange default: return errors.New("unsupported effect") } return nil } func (s *Serializable) MarshalJSON() ([]byte, error) { switch effect := s.Effect.(type) { case Manual: return json.Marshal(serializedEffect{Manual: &effect}) case Solid: return json.Marshal(serializedEffect{Solid: &effect}) case Gradient: return json.Marshal(serializedEffect{Gradient: &effect}) case Pattern: return json.Marshal(serializedEffect{Pattern: &effect}) case Random: return json.Marshal(serializedEffect{Random: &effect}) case VRange: return json.Marshal(serializedEffect{VRange: &effect}) default: panic(s.Effect.EffectDescription() + "is not understood by serializer") } }