From 465b153cfd1ac854f1c7c8bb34a343a41fafe67b Mon Sep 17 00:00:00 2001 From: BenTrapani Date: Fri, 29 Jul 2016 15:31:48 -0400 Subject: [PATCH] Sample generated output from example struct --- example/testcasing_jsonenums.go | 78 +++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 example/testcasing_jsonenums.go diff --git a/example/testcasing_jsonenums.go b/example/testcasing_jsonenums.go new file mode 100644 index 0000000..0c8d501 --- /dev/null +++ b/example/testcasing_jsonenums.go @@ -0,0 +1,78 @@ +// generated by jsonenums -type=TestCasing; DO NOT EDIT + +package main + +import ( + "database/sql/driver" + "encoding/json" + "fmt" + + "github.com/pkg/errors" +) + +var ( + _TestCasingNameToValue = map[string]TestCasing{ + "case_madness_a": caseMadnessA, + "case_ma_dn_e_es_b": caseMaDnEEsB, + "normal_case_example": normalCaseExample, + } + + _TestCasingValueToName = map[TestCasing]string{ + caseMadnessA: "case_madness_a", + caseMaDnEEsB: "case_ma_dn_e_es_b", + normalCaseExample: "normal_case_example", + } +) + +func init() { + var v TestCasing + if _, ok := interface{}(v).(fmt.Stringer); ok { + _TestCasingNameToValue = map[string]TestCasing{ + interface{}(caseMadnessA).(fmt.Stringer).String(): caseMadnessA, + interface{}(caseMaDnEEsB).(fmt.Stringer).String(): caseMaDnEEsB, + interface{}(normalCaseExample).(fmt.Stringer).String(): normalCaseExample, + } + } +} + +// MarshalJSON is generated so TestCasing satisfies json.Marshaler. +func (r TestCasing) MarshalJSON() ([]byte, error) { + if s, ok := interface{}(r).(fmt.Stringer); ok { + return json.Marshal(s.String()) + } + s, ok := _TestCasingValueToName[r] + if !ok { + return nil, fmt.Errorf("invalid TestCasing: %d", r) + } + return json.Marshal(s) +} + +// UnmarshalJSON is generated so TestCasing satisfies json.Unmarshaler. +func (r *TestCasing) UnmarshalJSON(data []byte) error { + var s string + if err := json.Unmarshal(data, &s); err != nil { + return fmt.Errorf("TestCasing should be a string, got %s", data) + } + v, ok := _TestCasingNameToValue[s] + if !ok { + return fmt.Errorf("invalid TestCasing %q", s) + } + *r = v + return nil +} + +//Scan an input string into this structure for use with GORP +func (r *TestCasing) Scan(i interface{}) error { + switch i.(type) { + case string: + r.UnmarshalJSON([]byte(i.(string))) + default: + return errors.Errorf("Can't scan %T into type %T", i, r) + } + return nil +} + +func (r TestCasing) Value() (driver.Value, error) { + bytes, err := r.MarshalJSON() + return string(bytes), err +}