-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.go
More file actions
55 lines (49 loc) · 1.46 KB
/
json.go
File metadata and controls
55 lines (49 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package LibraryController
import (
"errors"
"fmt"
"reflect"
"github.com/Eclalang/Ecla/interpreter/eclaType"
"github.com/Eclalang/LibraryController/utils"
"github.com/Eclalang/json"
)
type Json struct {
functionMap map[string]interface{}
}
func NewJson() *Json {
return &Json{
functionMap: map[string]interface{}{
"marshal": nil,
"unmarshal": nil,
},
}
}
func (j *Json) Call(name string, args []eclaType.Type) ([]eclaType.Type, error) {
newArgs := make([]any, len(args))
for k, arg := range args {
newArgs[k] = utils.EclaTypeToGo(arg)
}
if _, ok := j.functionMap[name]; !ok {
return nil, errors.New(fmt.Sprintf("Method %s not found in package json", name))
}
switch name {
case "marshal":
if len(newArgs) == 1 {
if reflect.TypeOf(newArgs[0]).Kind() == reflect.Map {
content, err := json.Marshal(newArgs[0].(map[string]any))
return []eclaType.Type{utils.GoToEclaType(content)}, err
} else if reflect.TypeOf(newArgs[0]).Kind() == reflect.Slice {
content, err := json.Marshal(newArgs[0].([]map[string]any))
return []eclaType.Type{utils.GoToEclaType(content)}, err
}
}
case "unmarshal":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && len(newArgs) == 1 {
content, err := json.Unmarshal(newArgs[0].(string))
return []eclaType.Type{utils.GoToEclaType(content)}, err
}
default:
return nil, errors.New(fmt.Sprintf("Method %s not found in package json", name))
}
return []eclaType.Type{eclaType.Null{}}, nil
}