Skip to content

Commit 3aa3aa0

Browse files
committed
0.0.1 release
1 parent de2cd3f commit 3aa3aa0

File tree

12 files changed

+602
-97
lines changed

12 files changed

+602
-97
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bin

.travis.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
sudo: false
2+
language: go
3+
4+
go:
5+
- 1.9
6+
- "1.10"
7+
- tip
8+
9+
os:
10+
- linux
11+
- osx
12+
13+
matrix:
14+
allow_failures:
15+
- go: tip
16+
fast_finish: true
17+
18+
script:
19+
- cd gss
20+
- go fmt
21+
- cd ./../cmd/gss
22+
- go fmt
23+
- cd ./../../cmd/gss.js
24+
- go fmt

README.md

+81-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,88 @@
1+
[![Build Status](https://travis-ci.org/spatialcurrent/go-simple-serializer.svg)](https://travis-ci.org/spatialcurrent/go-simple-serializer) [![GoDoc](https://godoc.org/github.com/spatialcurrent/go-simple-serializer?status.svg)](https://godoc.org/github.com/spatialcurrent/go-simple-serializer)
2+
13
# go-simple-serializer
24

35
# Description
46

5-
**go-simple-serializer** is a simple library for serializing and deserializing objects.
7+
**go-simple-serializer** (aka GSS) is a simple library for serializing/deserializing objects. GSS supports `csv`, `hcl`, `hcl2`, `json`, `jsonl`, `toml`, `yaml`. `hcl` and `hcl2` implementation is fragile and very much in `alpha`.
8+
9+
# Usage
10+
11+
**CLI**
12+
13+
You can use the command line tool to convert between formats.
14+
15+
```
16+
Usage: gss -i INPUT_FORMAT -o OUTPUT_FORMAT
17+
Options:
18+
-help
19+
Print help.
20+
-i string
21+
The input format: csv, hcl, hcl2, json, jsonl, toml, yaml
22+
-o string
23+
The output format: csv, hcl, hcl2, json, jsonl, toml, yaml
24+
-version
25+
Prints version to stdout.
26+
```
27+
28+
**Go**
29+
30+
You can import **go-simple-serializer** as a library with:
31+
32+
```go
33+
import (
34+
"github.com/spatialcurrent/go-simple-serializer/gss"
35+
)
36+
...
37+
output_string, err := gss.Convert(input_string, input_format, output_format)
38+
...
39+
```
40+
41+
**JavaScript**
42+
43+
```html
44+
<html>
45+
<head>
46+
<script src="https://...gss.js"></script>
47+
</head>
48+
<body>
49+
<script>
50+
var input = "{\"a\":1}";
51+
var output = gss.convert(input, "json", "yaml")
52+
...
53+
</script>
54+
</body>
55+
</html>
56+
```
57+
58+
**Android**
59+
60+
The `go-simple-serializer` code is available under `com.spatialcurrent.gss`. For example,
61+
62+
```java
63+
import com.spatialcurrent.gss.Gss;
64+
...
65+
String output_format = Gss.convert(input_string, input_format, output_format);
66+
...
67+
```
68+
69+
# Releases
70+
71+
**go-simple-serializer** is currently in **alpha**. See releases at https://github.com/spatialcurrent/go-simple-serializer/releases.
72+
73+
# Building
74+
75+
**CLI**
76+
77+
The `build_cli.sh` script is used to build executables for Linux and Windows.
78+
79+
**JavaScript**
80+
81+
You can compile GSS to pure JavaScript with the `scripts/build_javascript.sh` script.
82+
83+
**Android**
84+
85+
The `build_android.sh` script is used to build an [Android Archive](https://developer.android.com/studio/projects/android-library) (AAR) file and associated Javadocs.
686

787
# Contributing
888

cmd/gss.js/main.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
// GSS.JS is the Javascript version of GSS.
9+
//
10+
// Usage
11+
//
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+
package main
14+
15+
import (
16+
"github.com/spatialcurrent/go-simple-serializer/gss"
17+
)
18+
19+
import (
20+
"github.com/gopherjs/gopherjs/js"
21+
"honnef.co/go/js/console"
22+
)
23+
24+
var GO_GSS_VERSION = "0.0.1"
25+
26+
func main() {
27+
js.Global.Set("gss", map[string]interface{}{
28+
"version": GO_GSS_VERSION,
29+
"convert": Convert,
30+
})
31+
}
32+
33+
func Convert(input_string string, input_format string, output_format string) string {
34+
output_string, err := gss.Convert(input_string, input_format, output_format)
35+
if err != nil {
36+
console.Log(err.Error())
37+
return ""
38+
}
39+
return output_string
40+
}

cmd/gss/main.go

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"io/ioutil"
7+
"os"
8+
)
9+
10+
import (
11+
"github.com/pkg/errors"
12+
)
13+
14+
import (
15+
"github.com/spatialcurrent/go-simple-serializer/gss"
16+
)
17+
18+
var GO_GSS_VERSION = "0.0.1"
19+
20+
func printUsage() {
21+
fmt.Println("Usage: gss -i INPUT_FORMAT -o OUTPUT_FORMAT")
22+
}
23+
24+
func main() {
25+
26+
var input_format string
27+
var output_format string
28+
29+
var version bool
30+
var help bool
31+
32+
flag.StringVar(&input_format, "i", "", "The input format: csv, hcl, hcl2, json, jsonl, toml, yaml")
33+
flag.StringVar(&output_format, "o", "", "The output format: csv, hcl, hcl2, json, jsonl, toml, yaml")
34+
flag.BoolVar(&version, "version", false, "Prints version to stdout.")
35+
flag.BoolVar(&help, "help", false, "Print help.")
36+
37+
flag.Parse()
38+
39+
if help {
40+
printUsage()
41+
fmt.Println("Options:")
42+
flag.PrintDefaults()
43+
os.Exit(0)
44+
} else if len(os.Args) == 1 {
45+
fmt.Println("Error: Provided no arguments.")
46+
fmt.Println("Run \"gss -help\" for more information.")
47+
os.Exit(0)
48+
} else if len(os.Args) == 2 && os.Args[1] == "help" {
49+
printUsage()
50+
fmt.Println("Options:")
51+
flag.PrintDefaults()
52+
os.Exit(0)
53+
}
54+
55+
if version {
56+
fmt.Println(GO_GSS_VERSION)
57+
os.Exit(0)
58+
}
59+
60+
if len(input_format) == 0 {
61+
fmt.Println("Error: Provided no -input_format.")
62+
fmt.Println("Run \"gss -help\" for more information.")
63+
os.Exit(1)
64+
}
65+
66+
if len(output_format) == 0 {
67+
fmt.Println("Error: Provided no -output_format.")
68+
fmt.Println("Run \"gss -help\" for more information.")
69+
os.Exit(1)
70+
}
71+
72+
input_bytes, err := ioutil.ReadAll(os.Stdin)
73+
if err != nil {
74+
fmt.Println(errors.Wrap(err, "Error reading from stdin"))
75+
os.Exit(1)
76+
}
77+
78+
output_string, err := gss.Convert(string(input_bytes), input_format, output_format)
79+
if err != nil {
80+
fmt.Println(errors.Wrap(err, "Error converting"))
81+
os.Exit(1)
82+
}
83+
fmt.Println(output_string)
84+
}

gss/Convert.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package gss
2+
3+
import (
4+
"github.com/pkg/errors"
5+
)
6+
7+
func Convert(input_string string, input_format string, output_format string) (string, error) {
8+
if input_format == "json" || input_format == "hcl" || input_format == "hcl2" || input_format == "toml" || input_format == "yaml" {
9+
if output_format == "json" || input_format == "hcl" || input_format == "hcl2" || output_format == "toml" || output_format == "yaml" {
10+
object := map[string]interface{}{}
11+
err := Deserialize(input_string, input_format, &object)
12+
if err != nil {
13+
return "", errors.Wrap(err, "Error deserializing input")
14+
}
15+
output_string, err := Serialize(object, output_format)
16+
if err != nil {
17+
return "", errors.Wrap(err, "Error serializing output")
18+
}
19+
return output_string, nil
20+
} else if output_format == "jsonl" {
21+
object := []map[string]interface{}{}
22+
err := Deserialize(input_string, input_format, &object)
23+
if err != nil {
24+
return "", errors.Wrap(err, "Error deserializing input")
25+
}
26+
output_string, err := Serialize(object, output_format)
27+
if err != nil {
28+
return "", errors.Wrap(err, "Error serializing output")
29+
}
30+
return output_string, nil
31+
} else if output_format == "csv" {
32+
return "", errors.New("Error: incompatible output format \"" + output_format + "\"")
33+
} else {
34+
return "", errors.New("Error: unknown input format \"" + input_format + "\"")
35+
}
36+
} else if input_format == "jsonl" || input_format == "csv" {
37+
if output_format == "json" || output_format == "hcl" || output_format == "hcl2" || output_format == "toml" || output_format == "yaml" || output_format == "jsonl" || output_format == "csv" {
38+
object := []map[string]interface{}{}
39+
err := Deserialize(input_string, input_format, &object)
40+
if err != nil {
41+
return "", errors.Wrap(err, "Error deserializing input")
42+
}
43+
output_string, err := Serialize(object, output_format)
44+
if err != nil {
45+
return "", errors.Wrap(err, "Error serializing output")
46+
}
47+
return output_string, nil
48+
} else {
49+
return "", errors.New("Error: unknown output format \"" + input_format + "\"")
50+
}
51+
}
52+
return "", errors.New("Error: unknown input format \"" + input_format + "\"")
53+
}

0 commit comments

Comments
 (0)