Skip to content

Commit f453d9c

Browse files
committed
NewObject func
1 parent fd08c80 commit f453d9c

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

gss/Convert.go

+1-23
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,14 @@
77

88
package gss
99

10-
import (
11-
"strings"
12-
)
13-
1410
import (
1511
"github.com/pkg/errors"
1612
)
1713

1814
// Convert converts an input_string from the input_format to the output_format and returns an error, if any.
1915
func Convert(input_string string, input_format string, input_header []string, input_comment string, output_format string) (string, error) {
2016

21-
var object interface{}
22-
23-
if input_format == "json" {
24-
input_string = strings.TrimSpace(input_string)
25-
if len(input_string) > 0 && input_string[0] == '[' {
26-
object = []map[string]interface{}{}
27-
} else {
28-
object = map[string]interface{}{}
29-
}
30-
} else if input_format == "yaml" {
31-
input_string = strings.TrimSpace(input_string)
32-
if len(input_string) > 0 && input_string[0] == '-' {
33-
object = []map[string]interface{}{}
34-
} else {
35-
object = map[string]interface{}{}
36-
}
37-
} else if input_format == "bson" || input_format == "hcl" || input_format == "hcl2" || input_format == "properties" || input_format == "toml" {
38-
object = map[string]interface{}{}
39-
}
17+
object := NewObject(input_string, input_format)
4018

4119
if input_format == "bson" || input_format == "json" || input_format == "hcl" || input_format == "hcl2" || input_format == "properties" || input_format == "toml" || input_format == "yaml" {
4220
if output_format == "bson" || output_format == "json" || input_format == "hcl" || input_format == "hcl2" || output_format == "properties" || output_format == "toml" || output_format == "yaml" {

gss/NewObject.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package gss
2+
3+
import (
4+
"strings"
5+
"unicode"
6+
)
7+
8+
func NewObject(content string, format string) interface{} {
9+
if format == "json" {
10+
content = strings.TrimLeftFunc(content, unicode.IsSpace)
11+
if len(content) > 0 && content[0] == '[' {
12+
return []map[string]interface{}{}
13+
}
14+
return map[string]interface{}{}
15+
} else if format == "yaml" {
16+
content = strings.TrimLeftFunc(content, unicode.IsSpace)
17+
if len(content) > 0 && content[0] == '-' {
18+
return []map[string]interface{}{}
19+
}
20+
return map[string]interface{}{}
21+
} else if format == "bson" || format == "hcl" || format == "hcl2" || format == "properties" || format == "toml" {
22+
return map[string]interface{}{}
23+
}
24+
25+
return nil
26+
}

0 commit comments

Comments
 (0)