Skip to content

Commit

Permalink
upside down
Browse files Browse the repository at this point in the history
  • Loading branch information
hyorigo committed Jan 8, 2024
1 parent ff3e11b commit bec8efe
Showing 1 changed file with 84 additions and 84 deletions.
168 changes: 84 additions & 84 deletions dataconv/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,90 @@ import (
"go.starlark.net/starlarkstruct"
)

// Marshal converts Go values into Starlark types. It only supports common Go types, won't wrap any custom types like Starlight does.
func Marshal(data interface{}) (v starlark.Value, err error) {
switch x := data.(type) {
case nil:
v = starlark.None
case bool:
v = starlark.Bool(x)
case string:
v = starlark.String(x)
case int:
v = starlark.MakeInt(x)
case int8:
v = starlark.MakeInt(int(x))
case int16:
v = starlark.MakeInt(int(x))
case int32:
v = starlark.MakeInt(int(x))
case int64:
v = starlark.MakeInt64(x)
case uint:
v = starlark.MakeUint(x)
case uint8:
v = starlark.MakeUint(uint(x))
case uint16:
v = starlark.MakeUint(uint(x))
case uint32:
v = starlark.MakeUint(uint(x))
case uint64:
v = starlark.MakeUint64(x)
case float32:
v = starlark.Float(float64(x))
case float64:
v = starlark.Float(x)
case time.Time:
v = startime.Time(x)
case []interface{}:
var elems = make([]starlark.Value, len(x))
for i, val := range x {
elems[i], err = Marshal(val)
if err != nil {
return
}
}
v = starlark.NewList(elems)
case map[interface{}]interface{}:
dict := &starlark.Dict{}
var elem starlark.Value
for ki, val := range x {
var key starlark.Value
key, err = Marshal(ki)
if err != nil {
return
}

elem, err = Marshal(val)
if err != nil {
return
}
if err = dict.SetKey(key, elem); err != nil {
return
}
}
v = dict
case map[string]interface{}:
dict := &starlark.Dict{}
var elem starlark.Value
for key, val := range x {
elem, err = Marshal(val)
if err != nil {
return
}
if err = dict.SetKey(starlark.String(key), elem); err != nil {
return
}
}
v = dict
case Marshaler:
v, err = x.MarshalStarlark()
default:
return starlark.None, fmt.Errorf("unrecognized type: %#v", x)
}
return
}

// Unmarshal converts a starlark.Value into it's Golang counterpart.
func Unmarshal(x starlark.Value) (val interface{}, err error) {
switch v := x.(type) {
Expand Down Expand Up @@ -192,87 +276,3 @@ func Unmarshal(x starlark.Value) (val interface{}, err error) {
}
return
}

// Marshal converts Go values into Starlark types. It only supports common Go types, won't wrap any custom types like Starlight does.
func Marshal(data interface{}) (v starlark.Value, err error) {
switch x := data.(type) {
case nil:
v = starlark.None
case bool:
v = starlark.Bool(x)
case string:
v = starlark.String(x)
case int:
v = starlark.MakeInt(x)
case int8:
v = starlark.MakeInt(int(x))
case int16:
v = starlark.MakeInt(int(x))
case int32:
v = starlark.MakeInt(int(x))
case int64:
v = starlark.MakeInt64(x)
case uint:
v = starlark.MakeUint(x)
case uint8:
v = starlark.MakeUint(uint(x))
case uint16:
v = starlark.MakeUint(uint(x))
case uint32:
v = starlark.MakeUint(uint(x))
case uint64:
v = starlark.MakeUint64(x)
case float32:
v = starlark.Float(float64(x))
case float64:
v = starlark.Float(x)
case time.Time:
v = startime.Time(x)
case []interface{}:
var elems = make([]starlark.Value, len(x))
for i, val := range x {
elems[i], err = Marshal(val)
if err != nil {
return
}
}
v = starlark.NewList(elems)
case map[interface{}]interface{}:
dict := &starlark.Dict{}
var elem starlark.Value
for ki, val := range x {
var key starlark.Value
key, err = Marshal(ki)
if err != nil {
return
}

elem, err = Marshal(val)
if err != nil {
return
}
if err = dict.SetKey(key, elem); err != nil {
return
}
}
v = dict
case map[string]interface{}:
dict := &starlark.Dict{}
var elem starlark.Value
for key, val := range x {
elem, err = Marshal(val)
if err != nil {
return
}
if err = dict.SetKey(starlark.String(key), elem); err != nil {
return
}
}
v = dict
case Marshaler:
v, err = x.MarshalStarlark()
default:
return starlark.None, fmt.Errorf("unrecognized type: %#v", x)
}
return
}

0 comments on commit bec8efe

Please sign in to comment.