Skip to content

Commit d9bd17d

Browse files
committed
0.0.5 release
1 parent 6e755ae commit d9bd17d

9 files changed

Lines changed: 78 additions & 45 deletions

File tree

.travis.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,8 @@ matrix:
1515
- go: tip
1616
fast_finish: true
1717

18-
before_install:
19-
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then curl -L -s https://github.com/golang/dep/releases/download/v0.5.0/dep-linux-amd64 -o $GOPATH/bin/dep; fi
20-
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then wget https://github.com/golang/dep/releases/download/v0.5.0/dep-darwin-amd64 -O $GOPATH/bin/dep; fi
21-
- chmod +x $GOPATH/bin/dep
22-
2318
install:
24-
- dep ensure
19+
- go get ./...
2520
- go get -u github.com/alecthomas/gometalinter
2621
- gometalinter --install
2722

cmd/gss/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ func main() {
3434
var input_format string
3535
var input_header_text string
3636
var input_comment string
37+
var input_limit int
3738

3839
var output_format string
40+
var output_limit int
3941

4042
var version bool
4143
var verbose bool
@@ -44,7 +46,9 @@ func main() {
4446
flag.StringVar(&input_format, "i", "", "The input format: "+strings.Join(gss.Formats, ", "))
4547
flag.StringVar(&input_header_text, "h", "", "The input header if the stdin input has no header.")
4648
flag.StringVar(&input_comment, "c", "", "The input comment character, e.g., #. Commented lines are not sent to output.")
49+
flag.IntVar(&input_limit, "", -1, "The input limit")
4750
flag.StringVar(&output_format, "o", "", "The output format: "+strings.Join(gss.Formats, ", "))
51+
flag.IntVar(&output_limit, "", -1, "the output limit")
4852
flag.BoolVar(&version, "version", false, "Prints version to stdout")
4953
flag.BoolVar(&verbose, "verbose", false, "Print debug info to stdout")
5054
flag.BoolVar(&help, "help", false, "Print help.")
@@ -95,7 +99,7 @@ func main() {
9599
input_header = strings.Split(input_header_text, ",")
96100
}
97101

98-
output_string, err := gss.Convert(string(input_bytes), input_format, input_header, input_comment, output_format, verbose)
102+
output_string, err := gss.Convert(input_bytes, input_format, input_header, input_comment, input_limit, output_format, output_limit, verbose)
99103
if err != nil {
100104
fmt.Println(errors.Wrap(err, "Error converting"))
101105
os.Exit(1)

gss/Convert.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import (
1414

1515
// Convert converts an input_string from the input_format to the output_format.
1616
// Returns the output string and error, if any.
17-
func Convert(input_string string, input_format string, input_header []string, input_comment string, output_format string, verbose bool) (string, error) {
17+
func Convert(input_bytes []byte, input_format string, input_header []string, input_comment string, input_limit int, output_format string, output_limit int, verbose bool) (string, error) {
1818

19-
input_type, err := GetType(input_string, input_format)
19+
input_type, err := GetType(input_bytes, input_format)
2020
if err != nil {
2121
return "", errors.Wrap(err, "error creating new object for format "+input_format)
2222
}
@@ -29,7 +29,7 @@ func Convert(input_string string, input_format string, input_header []string, in
2929

3030
if input_format == "bson" || input_format == "json" || input_format == "hcl" || input_format == "hcl2" || input_format == "properties" || input_format == "toml" || input_format == "yaml" {
3131
if output_format == "bson" || output_format == "json" || input_format == "hcl" || input_format == "hcl2" || output_format == "properties" || output_format == "toml" || output_format == "yaml" {
32-
object, err := Deserialize(input_string, input_format, input_header, input_comment, input_type, verbose)
32+
object, err := Deserialize(string(input_bytes), input_format, input_header, input_comment, input_limit, input_type, verbose)
3333
if err != nil {
3434
return "", errors.Wrap(err, "Error deserializing input")
3535
}
@@ -42,7 +42,7 @@ func Convert(input_string string, input_format string, input_header []string, in
4242
}
4343
return output_string, nil
4444
} else if output_format == "jsonl" {
45-
object, err := Deserialize(input_string, input_format, input_header, input_comment, input_type, verbose)
45+
object, err := Deserialize(string(input_bytes), input_format, input_header, input_comment, input_limit, input_type, verbose)
4646
if err != nil {
4747
return "", errors.Wrap(err, "Error deserializing input")
4848
}
@@ -60,7 +60,7 @@ func Convert(input_string string, input_format string, input_header []string, in
6060
}
6161
} else if input_format == "jsonl" || input_format == "csv" || input_format == "tsv" {
6262
if output_format == "bson" || output_format == "json" || output_format == "hcl" || output_format == "hcl2" || output_format == "toml" || output_format == "yaml" || output_format == "jsonl" || output_format == "csv" || output_format == "tsv" {
63-
object, err := Deserialize(input_string, input_format, input_header, input_comment, input_type, verbose)
63+
object, err := Deserialize(string(input_bytes), input_format, input_header, input_comment, input_limit, input_type, verbose)
6464
if err != nil {
6565
return "", errors.Wrap(err, "Error deserializing input")
6666
}

gss/Deserialize.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func unescapePropertyText(in string) string {
3636
}
3737

3838
// Deserialize reads in an object from a string given format
39-
func Deserialize(input string, format string, input_header []string, input_comment string, output_type reflect.Type, verbose bool) (interface{}, error) {
39+
func Deserialize(input string, format string, input_header []string, input_comment string, input_limit int, output_type reflect.Type, verbose bool) (interface{}, error) {
4040

4141
if format == "csv" || format == "tsv" {
4242
output := reflect.MakeSlice(output_type, 0, 0)
@@ -149,18 +149,23 @@ func Deserialize(input string, format string, input_header []string, input_comme
149149
}
150150
} else if format == "jsonl" {
151151
output := reflect.MakeSlice(output_type, 0, 0)
152-
scanner := bufio.NewScanner(strings.NewReader(input))
153-
scanner.Split(bufio.ScanLines)
154-
for scanner.Scan() {
155-
line := strings.TrimSpace(scanner.Text())
156-
if len(input_comment) == 0 || !strings.HasPrefix(line, input_comment) {
157-
ptr := reflect.New(output_type.Elem())
158-
ptr.Elem().Set(reflect.MakeMap(output_type.Elem()))
159-
err := json.Unmarshal([]byte(line), ptr.Interface())
160-
if err != nil {
161-
return nil, errors.Wrap(err, "Error reading object from JSON line")
152+
if input_limit != 0 {
153+
scanner := bufio.NewScanner(strings.NewReader(input))
154+
scanner.Split(bufio.ScanLines)
155+
for scanner.Scan() {
156+
line := strings.TrimSpace(scanner.Text())
157+
if len(input_comment) == 0 || !strings.HasPrefix(line, input_comment) {
158+
ptr := reflect.New(output_type.Elem())
159+
ptr.Elem().Set(reflect.MakeMap(output_type.Elem()))
160+
err := json.Unmarshal([]byte(line), ptr.Interface())
161+
if err != nil {
162+
return nil, errors.Wrap(err, "Error reading object from JSON line")
163+
}
164+
output = reflect.Append(output, ptr.Elem())
165+
if input_limit > 0 && output.Len() >= input_limit {
166+
break
167+
}
162168
}
163-
output = reflect.Append(output, ptr.Elem())
164169
}
165170
}
166171
return output.Interface(), nil

gss/GetType.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,23 @@ import (
1717
// GetType takes in the content of an object as a string and the serialization format.
1818
// Returns the type using reflection.
1919
// This type is fixed and can be passed through functions without losing type information (unlike an empty object).
20-
func GetType(content string, format string) (reflect.Type, error) {
20+
func GetType(content []byte, format string) (reflect.Type, error) {
2121

22-
if format == "json" {
23-
content = strings.TrimLeftFunc(content, unicode.IsSpace)
24-
if len(content) > 0 && content[0] == '[' {
25-
return reflect.TypeOf([]map[string]interface{}{}), nil
22+
if format == "json" || format == "yaml" {
23+
str := string(content)
24+
if format == "json" {
25+
str = strings.TrimLeftFunc(str, unicode.IsSpace)
26+
if len(str) > 0 && str[0] == '[' {
27+
return reflect.TypeOf([]map[string]interface{}{}), nil
28+
}
29+
return reflect.TypeOf(map[string]interface{}{}), nil
30+
} else if format == "yaml" {
31+
str = strings.TrimLeftFunc(str, unicode.IsSpace)
32+
if len(str) > 0 && str[0] == '-' {
33+
return reflect.TypeOf([]map[string]interface{}{}), nil
34+
}
35+
return reflect.TypeOf(map[string]interface{}{}), nil
2636
}
27-
return reflect.TypeOf(map[string]interface{}{}), nil
28-
} else if format == "yaml" {
29-
content = strings.TrimLeftFunc(content, unicode.IsSpace)
30-
if len(content) > 0 && content[0] == '-' {
31-
return reflect.TypeOf([]map[string]interface{}{}), nil
32-
}
33-
return reflect.TypeOf(map[string]interface{}{}), nil
3437
} else if format == "bson" || format == "hcl" || format == "hcl2" || format == "properties" || format == "toml" {
3538
return reflect.TypeOf(map[string]interface{}{}), nil
3639
} else if format == "jsonl" || format == "csv" || format == "tsv" {

gss/Version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
package gss
99

1010
// VERSION is the version of this package and updated during a release.
11-
var VERSION = "0.0.4"
11+
var VERSION = "0.0.5"

gssjs/Convert.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ func Convert(input_string string, input_format string, output_format string, opt
2525

2626
input_header := []string{}
2727
input_comment := ""
28+
input_limit := -1
29+
output_limit := -1
2830

2931
if v, ok := m["header"]; ok {
3032
switch v.(type) {
@@ -45,11 +47,25 @@ func Convert(input_string string, input_format string, output_format string, opt
4547
}
4648
}
4749

48-
output_string, err := gss.Convert(input_string, input_format, input_header, input_comment, output_format, false)
50+
if v, ok := m["input_limit"]; ok {
51+
switch v := v.(type) {
52+
case int:
53+
input_limit = v
54+
}
55+
}
56+
57+
if v, ok := m["output_limit"]; ok {
58+
switch v := v.(type) {
59+
case int:
60+
output_limit = v
61+
}
62+
}
63+
64+
output_string, err := gss.Convert([]byte(input_string), input_format, input_header, input_comment, input_limit, output_format, output_limit, false)
4965
if err != nil {
5066
console.Error(errors.Wrap(err, "error converting input").Error())
5167
return ""
5268
}
53-
69+
5470
return output_string
55-
}
71+
}

gssjs/Deserialize.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func Deserialize(input_string string, input_format string, options *js.Object) i
2525

2626
input_header := []string{}
2727
input_comment := ""
28+
input_limit := -1
2829

2930
if v, ok := m["header"]; ok {
3031
switch v.(type) {
@@ -45,17 +46,24 @@ func Deserialize(input_string string, input_format string, options *js.Object) i
4546
}
4647
}
4748

48-
input_type, err := gss.GetType(input_string, input_format)
49+
if v, ok := m["input_limit"]; ok {
50+
switch v := v.(type) {
51+
case int:
52+
input_limit = v
53+
}
54+
}
55+
56+
input_type, err := gss.GetType([]byte(input_string), input_format)
4957
if err != nil {
5058
console.Error(errors.Wrap(err, "error creating new object for format "+input_format))
5159
return ""
5260
}
5361

54-
output_object, err := gss.Deserialize(input_string, input_format, input_header, input_comment, input_type, false)
62+
output_object, err := gss.Deserialize(input_string, input_format, input_header, input_comment, input_limit, input_type, false)
5563
if err != nil {
5664
console.Error(errors.Wrap(err, "error deserializing input into object").Error())
5765
return ""
5866
}
5967

6068
return output_object
61-
}
69+
}

plugins/gss/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ import (
2222
func main() {}
2323

2424
//export Convert
25-
func Convert(input_string *C.char, input_format *C.char, input_header *C.char, input_comment *C.char, output_format *C.char, output_string **C.char) *C.char {
25+
func Convert(input_string *C.char, input_format *C.char, input_header *C.char, input_comment *C.char, input_limit C.long, output_format *C.char, output_limit C.long, output_string **C.char) *C.char {
2626

2727
s, err := gss.Convert(
28-
C.GoString(input_string),
28+
[]byte(C.GoString(input_string)),
2929
C.GoString(input_format),
3030
strings.Split(C.GoString(input_header), ","),
3131
C.GoString(input_comment),
32+
int(input_limit),
3233
C.GoString(output_format),
34+
int(output_limit),
3335
false)
3436
if err != nil {
3537
return C.CString(err.Error())

0 commit comments

Comments
 (0)