|
5 | 5 | // |
6 | 6 | // ================================================================= |
7 | 7 |
|
8 | | -// GSS.JS is the Javascript version of GSS. |
| 8 | +// gss.js is the Javascript package for go-simple-serializer (GSS). |
9 | 9 | // |
10 | 10 | // Usage |
11 | 11 | // |
12 | 12 | // In you html document, the simplest workflow is to add GSS as a script and call gss.Convert(input_string, input_format, output_format); |
| 13 | +// |
13 | 14 | package main |
14 | 15 |
|
15 | 16 | import ( |
16 | | - "fmt" |
17 | 17 | "github.com/gopherjs/gopherjs/js" |
18 | | - "github.com/pkg/errors" |
19 | 18 | "github.com/spatialcurrent/go-simple-serializer/gss" |
20 | | - "honnef.co/go/js/console" |
| 19 | + "github.com/spatialcurrent/go-simple-serializer/gssjs" |
21 | 20 | ) |
22 | 21 |
|
23 | 22 | func main() { |
24 | 23 | js.Global.Set("gss", map[string]interface{}{ |
25 | 24 | "version": gss.VERSION, |
26 | | - "convert": Convert, |
27 | | - "deserialize": Deserialize, |
28 | | - "serialize": Serialize, |
| 25 | + "formats": gss.Formats, |
| 26 | + "convert": gssjs.Convert, |
| 27 | + "deserialize": gssjs.Deserialize, |
| 28 | + "serialize": gssjs.Serialize, |
29 | 29 | }) |
30 | 30 | } |
31 | | - |
32 | | -func Convert(input_string string, input_format string, output_format string, options *js.Object) string { |
33 | | - |
34 | | - m := map[string]interface{}{} |
35 | | - for _, key := range js.Keys(options) { |
36 | | - m[key] = options.Get(key).Interface() |
37 | | - } |
38 | | - |
39 | | - input_header := []string{} |
40 | | - input_comment := "" |
41 | | - |
42 | | - if v, ok := m["header"]; ok { |
43 | | - switch v.(type) { |
44 | | - case []string: |
45 | | - input_header = v.([]string) |
46 | | - case []interface{}: |
47 | | - input_header = make([]string, 0, len(v.([]interface{}))) |
48 | | - for _, h := range v.([]interface{}) { |
49 | | - input_header = append(input_header, fmt.Sprint(h)) |
50 | | - } |
51 | | - } |
52 | | - } |
53 | | - |
54 | | - if v, ok := m["input_comment"]; ok { |
55 | | - switch v := v.(type) { |
56 | | - case string: |
57 | | - input_comment = v |
58 | | - } |
59 | | - } |
60 | | - |
61 | | - output_string, err := gss.Convert(input_string, input_format, input_header, input_comment, output_format, false) |
62 | | - if err != nil { |
63 | | - console.Error(err.Error()) |
64 | | - return "" |
65 | | - } |
66 | | - return output_string |
67 | | -} |
68 | | - |
69 | | -func Deserialize(input_string string, input_format string, options *js.Object) interface{} { |
70 | | - |
71 | | - m := map[string]interface{}{} |
72 | | - for _, key := range js.Keys(options) { |
73 | | - m[key] = options.Get(key).Interface() |
74 | | - } |
75 | | - |
76 | | - input_header := []string{} |
77 | | - input_comment := "" |
78 | | - |
79 | | - if v, ok := m["header"]; ok { |
80 | | - switch v.(type) { |
81 | | - case []string: |
82 | | - input_header = v.([]string) |
83 | | - case []interface{}: |
84 | | - input_header = make([]string, 0, len(v.([]interface{}))) |
85 | | - for _, h := range v.([]interface{}) { |
86 | | - input_header = append(input_header, fmt.Sprint(h)) |
87 | | - } |
88 | | - } |
89 | | - } |
90 | | - |
91 | | - if v, ok := m["input_comment"]; ok { |
92 | | - switch v := v.(type) { |
93 | | - case string: |
94 | | - input_comment = v |
95 | | - } |
96 | | - } |
97 | | - |
98 | | - input_type, err := gss.GetType(input_string, input_format) |
99 | | - if err != nil { |
100 | | - console.Error(errors.Wrap(err, "error creating new object for format "+input_format)) |
101 | | - return "" |
102 | | - } |
103 | | - |
104 | | - output_object, err := gss.Deserialize(input_string, input_format, input_header, input_comment, input_type, false) |
105 | | - if err != nil { |
106 | | - console.Error(err.Error()) |
107 | | - return "" |
108 | | - } |
109 | | - |
110 | | - return output_object |
111 | | -} |
112 | | - |
113 | | -func Serialize(input_object interface{}, output_format string) interface{} { |
114 | | - |
115 | | - output_string, err := gss.Serialize(input_object, output_format) |
116 | | - if err != nil { |
117 | | - console.Error(err.Error()) |
118 | | - return "" |
119 | | - } |
120 | | - |
121 | | - return output_string |
122 | | -} |
0 commit comments