Skip to content

Commit fa54f29

Browse files
committed
0.0.4 release
1 parent 7327b21 commit fa54f29

9 files changed

Lines changed: 96 additions & 20 deletions

File tree

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,13 @@ The `Convert`, `Deserialize`, and `Serialize` functions are the core functions t
4747

4848
```go
4949
...
50-
output_string, err := gss.Convert(input_string, input_format, input_header, input_comment, output_format)
50+
output_string, err := gss.Convert(input_string, input_format, input_header, input_comment, output_format, verbose)
5151
...
52-
output = map[string]interface{}{}
53-
err := gss.Deserialize(input, format, input_header, input_comment, &output)
52+
input_type, err := GetType(input_string, input_format)
53+
if err != nil {
54+
return "", errors.Wrap(err, "error creating new object for format "+input_format)
55+
}
56+
output, err := gss.Deserialize(input, format, input_header, input_comment, input_type, verbose)
5457
...
5558
output_string, err := gss.Serialize(input, format)
5659
...
@@ -84,7 +87,7 @@ The `go-simple-serializer` code is available for use in Android applications und
8487
```java
8588
import com.spatialcurrent.gss.Gss;
8689
...
87-
String output_format = Gss.convert(input_string, input_format, input_header, input_comment, output_format);
90+
String output_format = Gss.convert(input_string, input_format, input_header, input_comment, output_format, verbose);
8891
...
8992
```
9093

cmd/gss.js/main.go

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ package main
1414

1515
import (
1616
"fmt"
17-
"github.com/spatialcurrent/go-simple-serializer/gss"
18-
)
19-
20-
import (
2117
"github.com/gopherjs/gopherjs/js"
18+
"github.com/pkg/errors"
19+
"github.com/spatialcurrent/go-simple-serializer/gss"
2220
"honnef.co/go/js/console"
2321
)
2422

2523
func main() {
2624
js.Global.Set("gss", map[string]interface{}{
27-
"version": gss.VERSION,
28-
"convert": Convert,
25+
"version": gss.VERSION,
26+
"convert": Convert,
27+
"deserialize": Deserialize,
28+
"serialize": Serialize,
2929
})
3030
}
3131

@@ -60,8 +60,63 @@ func Convert(input_string string, input_format string, output_format string, opt
6060

6161
output_string, err := gss.Convert(input_string, input_format, input_header, input_comment, output_format, false)
6262
if err != nil {
63-
console.Log(err.Error())
63+
console.Error(err.Error())
6464
return ""
6565
}
6666
return output_string
6767
}
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+
}

examples/c/test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ main(int argc, char **argv) {
1818
char *input_string = "{\"a\":\"b\",\"c\":[\"d\"]}";
1919
char *output_string;
2020

21+
printf("Version: %s\n", Version());
2122
printf("%s\n", input_string);
2223

2324
err = Convert(input_string, "json", "", "", "yaml", &output_string);

examples/cpp/test.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ int main(int argc, char **argv) {
4848
std::string output_format("yaml");
4949
char *output_char_ptr;
5050

51+
// Write version to stderr
52+
std::cout << "Version" << Version() <<std::endl;
53+
5154
// Write input to stderr
5255
std::cout << input_string << std::endl;
5356

examples/python/test.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
# You can add current directory with LD_LIBRARY_PATH=. python test.py
1616
lib = cdll.LoadLibrary("gss.so")
1717

18-
# Define Function Definition
18+
# Define Function Definitions
19+
version = lib.Version
20+
version.argtypes = []
21+
version.restype = c_char_p
22+
1923
convert = lib.Convert
2024
convert.argtypes = [c_char_p, c_char_p, c_char_p, c_char_p, c_char_p, POINTER(c_char_p)]
2125
convert.restype = c_char_p
@@ -25,6 +29,8 @@
2529
input_string = "{\"a\":\"b\",\"c\":[\"d\"]}"
2630
output_string_pointer = c_char_p()
2731

32+
print "Version:", version
33+
2834
print input_string
2935

3036
err = convert(input_string, "json", "", "", "yaml", byref(output_string_pointer))

gss/Deserialize.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,16 @@ func Deserialize(input string, format string, input_header []string, input_comme
137137
for scanner.Scan() {
138138
line := strings.TrimSpace(scanner.Text())
139139
if len(input_comment) == 0 || !strings.HasPrefix(line, input_comment) {
140-
obj := reflect.MakeMap(output_type.Elem())
141-
err := json.Unmarshal([]byte(line), obj.Interface())
140+
ptr := reflect.New(output_type.Elem())
141+
ptr.Elem().Set(reflect.MakeMap(output_type.Elem()))
142+
err := json.Unmarshal([]byte(line), ptr.Interface())
142143
if err != nil {
143144
return nil, errors.Wrap(err, "Error reading object from JSON line")
144145
}
145-
output = reflect.Append(output, obj)
146+
output = reflect.Append(output, ptr.Elem())
146147
}
147148
}
148-
return output, nil
149+
return output.Interface(), nil
149150
} else if format == "hcl" {
150151
ptr := reflect.New(output_type)
151152
ptr.Elem().Set(reflect.MakeMap(output_type))

gss/GetType.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import (
1414
"unicode"
1515
)
1616

17+
// GetType takes in the content of an object as a string and the serialization format.
18+
// Returns the type using reflection.
19+
// This type is fixed and can be passed through functions without losing type information (unlike an empty object).
1720
func GetType(content string, format string) (reflect.Type, error) {
1821

1922
if format == "json" {

gss/Version.go

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

88
package gss
99

10-
var VERSION = "0.0.3"
10+
var VERSION = "0.0.4"

plugins/gss/main.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import (
1919
"github.com/spatialcurrent/go-simple-serializer/gss"
2020
)
2121

22-
var GO_GSS_VERSION = "0.0.2"
23-
2422
func main() {}
2523

2624
//export Convert
@@ -31,7 +29,8 @@ func Convert(input_string *C.char, input_format *C.char, input_header *C.char, i
3129
C.GoString(input_format),
3230
strings.Split(C.GoString(input_header), ","),
3331
C.GoString(input_comment),
34-
C.GoString(output_format))
32+
C.GoString(output_format),
33+
false)
3534
if err != nil {
3635
return C.CString(err.Error())
3736
}
@@ -40,3 +39,8 @@ func Convert(input_string *C.char, input_format *C.char, input_header *C.char, i
4039

4140
return nil
4241
}
42+
43+
//export Version
44+
func Version() *C.char {
45+
return C.CString(gss.VERSION)
46+
}

0 commit comments

Comments
 (0)