Skip to content

Instantly share code, notes, and snippets.

@tejainece
Last active August 29, 2015 13:59
Show Gist options
  • Select an option

  • Save tejainece/10558111 to your computer and use it in GitHub Desktop.

Select an option

Save tejainece/10558111 to your computer and use it in GitHub Desktop.
package main
import "fmt"
import "encoding/json"
import "time"
type (
Container map[string]Variable
Variable interface {
GetName() string
SetName(string)
GetType() int
SetType(int)
}
VariableImpl struct {
Name string
Type int
}
StringVariable struct {
VariableImpl
Value string
}
StringListVariable struct {
VariableImpl
Value []string
}
CountDownVariable struct {
VariableImpl
Value time.Time
}
)
func (cont *Container) UnmarshalJSON(data []byte) error {
mpv := map[string]VariableImpl{}
_ = json.Unmarshal(data, &mpv)
mpvr := map[string]json.RawMessage{}
_ = json.Unmarshal(data, &mpvr)
for index, vimp := range mpv {
switch vimp.Type {
case 0:
strvar := StringVariable{}
_ = json.Unmarshal(mpvr[index], &strvar)
(*cont)[index] = strvar
case 1:
strlvar := StringListVariable{}
_ = json.Unmarshal(mpvr[index], &strlvar)
(*cont)[index] = strlvar
case 2:
cdvar := CountDownVariable{}
_ = json.Unmarshal(mpvr[index], &cdvar)
(*cont)[index] = cdvar
}
}
return nil
}
func (variable VariableImpl) GetType() int {
return variable.Type
}
func (variable VariableImpl) SetType(new_type int) {
variable.Type = new_type
}
func (variable VariableImpl) GetName() string {
return variable.Name
}
func (variable VariableImpl) SetName(new_name string) {
variable.Name = new_name
}
func NewStringVariable(name string, value string) (intvar StringVariable) {
intvar = StringVariable{VariableImpl: VariableImpl{Name: name, Type: 0}, Value: value}
return
}
func NewStringListVariable(name string, value []string) (intvar StringListVariable) {
intvar = StringListVariable{VariableImpl: VariableImpl{Name: name, Type: 1}, Value: value}
return
}
func NewCountDownVariable(name string, value time.Time) (countdown CountDownVariable) {
countdown = CountDownVariable{VariableImpl: VariableImpl{Name: name, Type: 2}, Value: value}
return
}
func main() {
/*container := Container{
"location": NewStringVariable("location", "Bangalore"),
"countdown1": NewCountDownVariable("countdown1", time.Now()),
"languages": NewStringListVariable("languages", []string{"Tamil", "Telugu", "English", "Hindi"}),
}
bytes, _ :=json.Marshal(container)
str := string(bytes)
fmt.Println(str)*/
data := `{"countdown1":{"Name":"countdown1","Type":2,"Value":"2009-11-10T23:00:00Z"},"languages":{"Name":"languages","Type":1,"Value":["Tamil","Telugu","English","Hindi"]},"location":{"Name":"location","Type":0,"Value":"Bangalore"}}
`
container := Container{}
json.Unmarshal([]byte(data), &container)
fmt.Println(container)
}
package main
import "fmt"
import "encoding/json"
import "time"
import "bytes"
import "errors"
type (
Container struct {
Variables map[string]Variable
}
Variable interface {
GetName() string
SetName(string)
GetType() int
SetType(int)
}
VariableImpl struct {
Name string
Type int
}
StringVariable struct {
VariableImpl
Value string
}
StringListVariable struct {
VariableImpl
Value []string
}
CountDownVariable struct {
VariableImpl
Value time.Time
}
)
func (cont *Container) UnmarshalJSON(data []byte) error {
b := bytes.NewBuffer(data)
dec := json.NewDecoder(b)
mp := map[string]json.RawMessage{}
if err := dec.Decode(&mp); err != nil {
return err
}
vars, ok := mp["Variables"]
if ok {
mpv := map[string]VariableImpl{}
_ = json.Unmarshal(vars, &mpv)
mpvr := map[string]json.RawMessage{}
_ = json.Unmarshal(vars, &mpvr)
cont.Variables = map[string]Variable{}
for index, vimp := range mpv {
switch vimp.Type {
case 0:
strvar := StringVariable{}
_ = json.Unmarshal(mpvr[index], &strvar)
cont.Variables[index] = strvar
case 1:
strlvar := StringListVariable{}
_ = json.Unmarshal(mpvr[index], &strlvar)
cont.Variables[index] = strlvar
case 2:
cdvar := CountDownVariable{}
_ = json.Unmarshal(mpvr[index], &cdvar)
cont.Variables[index] = cdvar
}
}
} else {
return errors.New("error")
}
return nil
}
func (variable VariableImpl) GetType() int {
return variable.Type
}
func (variable VariableImpl) SetType(new_type int) {
variable.Type = new_type
}
func (variable VariableImpl) GetName() string {
return variable.Name
}
func (variable VariableImpl) SetName(new_name string) {
variable.Name = new_name
}
func NewStringVariable(name string, value string) (intvar StringVariable) {
intvar = StringVariable{VariableImpl: VariableImpl{Name: name, Type: 0}, Value: value}
return
}
func NewStringListVariable(name string, value []string) (intvar StringListVariable) {
intvar = StringListVariable{VariableImpl: VariableImpl{Name: name, Type: 1}, Value: value}
return
}
func NewCountDownVariable(name string, value time.Time) (countdown CountDownVariable) {
countdown = CountDownVariable{VariableImpl: VariableImpl{Name: name, Type: 2}, Value: value}
return
}
func main() {
/*container := Container{Variables: map[string]Variable{
"location": NewStringVariable("location", "Bangalore"),
"countdown1": NewCountDownVariable("countdown1", time.Now()),
"languages": NewStringListVariable("languages", []string{"Tamil", "Telugu", "English", "Hindi"}),
},
}
bytes, _ :=json.Marshal(container)
str := string(bytes)
fmt.Println(str)*/
data := `{"Variables":{"countdown1":{"Name":"countdown1","Type":2,"Value":"2009-11-10T23:00:00Z"},"languages":{"Name":"languages","Type":1,"Value":["Tamil","Telugu","English","Hindi"]},"location":{"Name":"location","Type":0,"Value":"Bangalore"}}}`
container := Container{}
json.Unmarshal([]byte(data), &container)
fmt.Println(container)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment