Skip to content

Commit 6e755ae

Browse files
committed
refractor to better support javascript
1 parent fa54f29 commit 6e755ae

8 files changed

Lines changed: 185 additions & 106 deletions

File tree

cmd/gss.js/main.go

Lines changed: 7 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -5,118 +5,26 @@
55
//
66
// =================================================================
77

8-
// GSS.JS is the Javascript version of GSS.
8+
// gss.js is the Javascript package for go-simple-serializer (GSS).
99
//
1010
// Usage
1111
//
1212
// 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+
//
1314
package main
1415

1516
import (
16-
"fmt"
1717
"github.com/gopherjs/gopherjs/js"
18-
"github.com/pkg/errors"
1918
"github.com/spatialcurrent/go-simple-serializer/gss"
20-
"honnef.co/go/js/console"
19+
"github.com/spatialcurrent/go-simple-serializer/gssjs"
2120
)
2221

2322
func main() {
2423
js.Global.Set("gss", map[string]interface{}{
2524
"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,
2929
})
3030
}
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-
}

cmd/gss/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
//
66
// =================================================================
77

8+
// gss is the command line program for go-simple-serializer (GSS).
9+
//
810
package main
911

1012
import (
@@ -23,8 +25,6 @@ import (
2325
"github.com/spatialcurrent/go-simple-serializer/gss"
2426
)
2527

26-
var GO_GSS_FORMATS = []string{"csv", "tsv", "hcl", "hcl2", "json", "jsonl", "properties", "toml", "yaml"}
27-
2828
func printUsage() {
2929
fmt.Println("Usage: gss -i INPUT_FORMAT -o OUTPUT_FORMAT [-h HEADER] [-c COMMENT]")
3030
}
@@ -41,10 +41,10 @@ func main() {
4141
var verbose bool
4242
var help bool
4343

44-
flag.StringVar(&input_format, "i", "", "The input format: "+strings.Join(GO_GSS_FORMATS, ", "))
44+
flag.StringVar(&input_format, "i", "", "The input format: "+strings.Join(gss.Formats, ", "))
4545
flag.StringVar(&input_header_text, "h", "", "The input header if the stdin input has no header.")
4646
flag.StringVar(&input_comment, "c", "", "The input comment character, e.g., #. Commented lines are not sent to output.")
47-
flag.StringVar(&output_format, "o", "", "The output format: "+strings.Join(GO_GSS_FORMATS, ", "))
47+
flag.StringVar(&output_format, "o", "", "The output format: "+strings.Join(gss.Formats, ", "))
4848
flag.BoolVar(&version, "version", false, "Prints version to stdout")
4949
flag.BoolVar(&verbose, "verbose", false, "Print debug info to stdout")
5050
flag.BoolVar(&help, "help", false, "Print help.")

gss/Deserialize.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,18 @@ func Deserialize(input string, format string, input_header []string, input_comme
112112
ptr := reflect.New(output_type)
113113
ptr.Elem().Set(reflect.MakeMap(output_type))
114114
err := bson.Unmarshal([]byte(input), ptr.Interface())
115-
return ptr.Elem().Interface(), err
115+
if err != nil {
116+
return nil, errors.Wrap(err, "error unmarshalling bytes into BSON")
117+
}
118+
return ptr.Elem().Interface(), nil
119+
} else if output_type.Kind() == reflect.Slice {
120+
ptr := reflect.New(output_type)
121+
ptr.Elem().Set(reflect.MakeSlice(output_type, 0, 0))
122+
err := bson.Unmarshal([]byte(input), ptr.Interface())
123+
if err != nil {
124+
return nil, errors.Wrap(err, "error unmarshalling bytes into BSON")
125+
}
126+
return ptr.Elem().Interface(), nil
116127
} else {
117128
return nil, errors.New("Invalid output type for bson " + fmt.Sprint(output_type))
118129
}
@@ -121,12 +132,18 @@ func Deserialize(input string, format string, input_header []string, input_comme
121132
ptr := reflect.New(output_type)
122133
ptr.Elem().Set(reflect.MakeMap(output_type))
123134
err := json.Unmarshal([]byte(input), ptr.Interface())
124-
return ptr.Elem().Interface(), err
135+
if err != nil {
136+
return nil, errors.Wrap(err, "error unmarshalling bytes into JSON")
137+
}
138+
return ptr.Elem().Interface(), nil
125139
} else if output_type.Kind() == reflect.Slice {
126140
ptr := reflect.New(output_type)
127141
ptr.Elem().Set(reflect.MakeSlice(output_type, 0, 0))
128142
err := json.Unmarshal([]byte(input), ptr.Interface())
129-
return ptr.Elem().Interface(), err
143+
if err != nil {
144+
return nil, errors.Wrap(err, "error unmarshalling bytes into JSON")
145+
}
146+
return ptr.Elem().Interface(), nil
130147
} else {
131148
return nil, errors.New("Invalid output type for json " + fmt.Sprint(output_type))
132149
}

gss/Formats.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// =================================================================
2+
//
3+
// Copyright (C) 2018 Spatial Current, Inc. - All Rights Reserved
4+
// Released as open source under the MIT License. See LICENSE file.
5+
//
6+
// =================================================================
7+
8+
package gss
9+
10+
// Formats is the formats supported by go-simple-serializer (GSS).
11+
var Formats = []string{"bson", "csv", "tsv", "hcl", "hcl2", "json", "jsonl", "properties", "toml", "yaml"}

gss/Version.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77

88
package gss
99

10+
// VERSION is the version of this package and updated during a release.
1011
var VERSION = "0.0.4"

gssjs/Convert.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// =================================================================
2+
//
3+
// Copyright (C) 2018 Spatial Current, Inc. - All Rights Reserved
4+
// Released as open source under the MIT License. See LICENSE file.
5+
//
6+
// =================================================================
7+
8+
package gssjs
9+
10+
import (
11+
"fmt"
12+
"github.com/gopherjs/gopherjs/js"
13+
"github.com/pkg/errors"
14+
"github.com/spatialcurrent/go-simple-serializer/gss"
15+
"honnef.co/go/js/console"
16+
)
17+
18+
// Convert is a function provided to gss.js that wraps gss.Convert to support JavaScript.
19+
func Convert(input_string string, input_format string, output_format string, options *js.Object) string {
20+
21+
m := map[string]interface{}{}
22+
for _, key := range js.Keys(options) {
23+
m[key] = options.Get(key).Interface()
24+
}
25+
26+
input_header := []string{}
27+
input_comment := ""
28+
29+
if v, ok := m["header"]; ok {
30+
switch v.(type) {
31+
case []string:
32+
input_header = v.([]string)
33+
case []interface{}:
34+
input_header = make([]string, 0, len(v.([]interface{})))
35+
for _, h := range v.([]interface{}) {
36+
input_header = append(input_header, fmt.Sprint(h))
37+
}
38+
}
39+
}
40+
41+
if v, ok := m["input_comment"]; ok {
42+
switch v := v.(type) {
43+
case string:
44+
input_comment = v
45+
}
46+
}
47+
48+
output_string, err := gss.Convert(input_string, input_format, input_header, input_comment, output_format, false)
49+
if err != nil {
50+
console.Error(errors.Wrap(err, "error converting input").Error())
51+
return ""
52+
}
53+
54+
return output_string
55+
}

gssjs/Deserialize.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// =================================================================
2+
//
3+
// Copyright (C) 2018 Spatial Current, Inc. - All Rights Reserved
4+
// Released as open source under the MIT License. See LICENSE file.
5+
//
6+
// =================================================================
7+
8+
package gssjs
9+
10+
import (
11+
"fmt"
12+
"github.com/gopherjs/gopherjs/js"
13+
"github.com/pkg/errors"
14+
"github.com/spatialcurrent/go-simple-serializer/gss"
15+
"honnef.co/go/js/console"
16+
)
17+
18+
// Deserialize is a function provided to gss.js that wraps gss.Deserialize to support JavaScript.
19+
func Deserialize(input_string string, input_format string, options *js.Object) interface{} {
20+
21+
m := map[string]interface{}{}
22+
for _, key := range js.Keys(options) {
23+
m[key] = options.Get(key).Interface()
24+
}
25+
26+
input_header := []string{}
27+
input_comment := ""
28+
29+
if v, ok := m["header"]; ok {
30+
switch v.(type) {
31+
case []string:
32+
input_header = v.([]string)
33+
case []interface{}:
34+
input_header = make([]string, 0, len(v.([]interface{})))
35+
for _, h := range v.([]interface{}) {
36+
input_header = append(input_header, fmt.Sprint(h))
37+
}
38+
}
39+
}
40+
41+
if v, ok := m["input_comment"]; ok {
42+
switch v := v.(type) {
43+
case string:
44+
input_comment = v
45+
}
46+
}
47+
48+
input_type, err := gss.GetType(input_string, input_format)
49+
if err != nil {
50+
console.Error(errors.Wrap(err, "error creating new object for format "+input_format))
51+
return ""
52+
}
53+
54+
output_object, err := gss.Deserialize(input_string, input_format, input_header, input_comment, input_type, false)
55+
if err != nil {
56+
console.Error(errors.Wrap(err, "error deserializing input into object").Error())
57+
return ""
58+
}
59+
60+
return output_object
61+
}

gssjs/Serialize.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// =================================================================
2+
//
3+
// Copyright (C) 2018 Spatial Current, Inc. - All Rights Reserved
4+
// Released as open source under the MIT License. See LICENSE file.
5+
//
6+
// =================================================================
7+
8+
package gssjs
9+
10+
import (
11+
"github.com/pkg/errors"
12+
"github.com/spatialcurrent/go-simple-serializer/gss"
13+
"honnef.co/go/js/console"
14+
)
15+
16+
// Serialize is a function provided to gss.js that wraps gss.Serialize to support JavaScript.
17+
func Serialize(input_object interface{}, output_format string) interface{} {
18+
19+
output_string, err := gss.Serialize(input_object, output_format)
20+
if err != nil {
21+
console.Error(errors.Wrap(err, "error serializing object").Error())
22+
return ""
23+
}
24+
25+
return output_string
26+
}

0 commit comments

Comments
 (0)