diff options
Diffstat (limited to 'src/hub/src/spreadspace.org/sfive/s5cvt.go')
-rw-r--r-- | src/hub/src/spreadspace.org/sfive/s5cvt.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/hub/src/spreadspace.org/sfive/s5cvt.go b/src/hub/src/spreadspace.org/sfive/s5cvt.go new file mode 100644 index 0000000..3402e7f --- /dev/null +++ b/src/hub/src/spreadspace.org/sfive/s5cvt.go @@ -0,0 +1,57 @@ +package sfive + +import ( + "encoding/json" +) + +type StatsDecoder interface { + Decode(jsonString []byte) (StatisticsData, error) +} + +type StatsEncoder interface { + Encode(data StatisticsData) []byte +} + +type StatefulDecoder struct { + sourceId SourceId +} + +type PlainDecoder struct{} + +type PlainEncoder struct{} + +func NewStatefulDecoder(jsonString []byte) (decoder StatsDecoder) { + res := new(StatefulDecoder) + err := json.Unmarshal(jsonString, &res.sourceId) + if err != nil { + return nil + } + return res +} + +func NewPlainDecoder() (decoder StatsDecoder) { + return new(PlainDecoder) +} + +func (self *StatefulDecoder) Decode(jsonString []byte) (dat StatisticsData, err error) { + err = json.Unmarshal(jsonString, &dat) + if err != nil { + return + } + dat.CopyFrom(&self.sourceId) + return +} + +func (self *PlainDecoder) Decode(jsonString []byte) (dat StatisticsData, err error) { + err = json.Unmarshal(jsonString, &dat) + return +} + +func (self *PlainEncoder) Encode(data StatisticsData) []byte { + res, err := json.Marshal(&data) + if err != nil { + panic("oh fuck I cannot event marshal my own data") + } + return res +} + |