Skip to content

Commit 5c02ade

Browse files
committed
json transform, except nested array
1 parent fca8b0d commit 5c02ade

File tree

4 files changed

+136
-99
lines changed

4 files changed

+136
-99
lines changed

example/files/valid.json

Lines changed: 94 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,100 @@
11
{
2-
"apiVersion": "v1",
3-
"kind": "Pod",
4-
"metadata": {
5-
"name": "rss-site",
6-
"labels": {
7-
"app": "web"
8-
}
9-
},
10-
"spec": {
11-
"containers": [
2+
"web-app": {
3+
"servlet": [
4+
{
5+
"servlet-name": "cofaxCDS",
6+
"servlet-class": "org.cofax.cds.CDSServlet",
7+
"init-param": {
8+
"configGlossary:installationAt": "Philadelphia, PA",
9+
"configGlossary:adminEmail": "[email protected]",
10+
"configGlossary:poweredBy": "Cofax",
11+
"configGlossary:poweredByIcon": "/images/cofax.gif",
12+
"configGlossary:staticPath": "/content/static",
13+
"templateProcessorClass": "org.cofax.WysiwygTemplate",
14+
"templateLoaderClass": "org.cofax.FilesTemplateLoader",
15+
"templatePath": "templates",
16+
"templateOverridePath": "",
17+
"defaultListTemplate": "listTemplate.htm",
18+
"defaultFileTemplate": "articleTemplate.htm",
19+
"useJSP": false,
20+
"jspListTemplate": "listTemplate.jsp",
21+
"jspFileTemplate": "articleTemplate.jsp",
22+
"cachePackageTagsTrack": 200,
23+
"cachePackageTagsStore": 200,
24+
"cachePackageTagsRefresh": 60,
25+
"cacheTemplatesTrack": 100,
26+
"cacheTemplatesStore": 50,
27+
"cacheTemplatesRefresh": 15,
28+
"cachePagesTrack": 200,
29+
"cachePagesStore": 100,
30+
"cachePagesRefresh": 10,
31+
"cachePagesDirtyRead": 10,
32+
"searchEngineListTemplate": "forSearchEnginesList.htm",
33+
"searchEngineFileTemplate": "forSearchEngines.htm",
34+
"searchEngineRobotsDb": "WEB-INF/robots.db",
35+
"useDataStore": true,
36+
"dataStoreClass": "org.cofax.SqlDataStore",
37+
"redirectionClass": "org.cofax.SqlRedirection",
38+
"dataStoreName": "cofax",
39+
"dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver",
40+
"dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon",
41+
"dataStoreUser": "sa",
42+
"dataStorePassword": "dataStoreTestQuery",
43+
"dataStoreTestQuery": "SET NOCOUNT ON;select test='test';",
44+
"dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log",
45+
"dataStoreInitConns": 10,
46+
"dataStoreMaxConns": 100,
47+
"dataStoreConnUsageLimit": 100,
48+
"dataStoreLogLevel": "debug",
49+
"maxUrlLength": 500
50+
}
51+
},
52+
{
53+
"servlet-name": "cofaxEmail",
54+
"servlet-class": "org.cofax.cds.EmailServlet",
55+
"init-param": {
56+
"mailHost": "mail1",
57+
"mailHostOverride": "mail2"
58+
}
59+
},
60+
{
61+
"servlet-name": "cofaxAdmin",
62+
"servlet-class": "org.cofax.cds.AdminServlet"
63+
},
1264
{
13-
"name": "developer",
14-
"developer": {
15-
"backend": ["golang","java","python"],
16-
"frontend": ["angular","javascript", {"test": "test"}],
17-
"test": [
18-
{"unit": "meter"},
19-
{"price": 343.32},
20-
{"discount": [11,57]}
21-
]
22-
},
23-
"image": "nginx",
24-
"ports": [
25-
{
26-
"containerPort": 80,
27-
"ports": [34,55],
28-
"status": ["on","off"]
29-
}
30-
]
65+
"servlet-name": "fileServlet",
66+
"servlet-class": "org.cofax.cds.FileServlet"
3167
},
3268
{
33-
"name": "rss-reader",
34-
"image": "a/rss-php-nginx:v1",
35-
"ports": [
36-
{
37-
"containerPort": 0.23,
38-
"test": [
39-
[23,334],
40-
["old","new"]
41-
]
42-
}
43-
]
69+
"servlet-name": "cofaxTools",
70+
"servlet-class": "org.cofax.cms.CofaxToolsServlet",
71+
"init-param": {
72+
"templatePath": "toolstemplates/",
73+
"log": 1,
74+
"logLocation": "/usr/local/tomcat/logs/CofaxTools.log",
75+
"logMaxSize": "",
76+
"dataLog": 1,
77+
"dataLogLocation": "/usr/local/tomcat/logs/dataLog.log",
78+
"dataLogMaxSize": "",
79+
"removePageCache": "/content/admin/remove?cache=pages&id=",
80+
"removeTemplateCache": "/content/admin/remove?cache=templates&id=",
81+
"fileTransferFolder": "/usr/local/tomcat/webapps/content/fileTransferFolder",
82+
"lookInContext": 1,
83+
"adminGroupID": 4,
84+
"betaServer": true
85+
}
4486
}
45-
]
87+
],
88+
"servlet-mapping": {
89+
"cofaxCDS": "/",
90+
"cofaxEmail": "/cofaxutil/aemail/*",
91+
"cofaxAdmin": "/admin/*",
92+
"fileServlet": "/static/*",
93+
"cofaxTools": "/tools/*"
94+
},
95+
"taglib": {
96+
"taglib-uri": "cofax.tld",
97+
"taglib-location": "/WEB-INF/tlds/cofax.tld"
98+
}
4699
}
47-
}
100+
}

example/json.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"fmt"
54
"log"
65

76
"github.com/mstgnz/transformer/json"
@@ -16,6 +15,8 @@ func runJson() {
1615
knot, _ := json.DecodeJson(byt)
1716
knot = knot.Reset()
1817

19-
fmt.Println(knot.Next.Next.Value)
20-
//fmt.Println(knot.GetNode(nil, "test")[0].Value)
18+
knot.Print()
19+
20+
log.Println()
21+
2122
}

example/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package main
22

33
func main() {
44
// Json Node
5-
//runJson()
5+
runJson()
66

77
// Xml Node
8-
runXml()
8+
//runXml()
99

1010
// Yaml Node
1111
//runYml()

json/json.go

Lines changed: 36 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ import (
88
"log"
99
"os"
1010
"reflect"
11-
"strings"
1211

1312
"github.com/mstgnz/transformer/node"
1413
"github.com/pkg/errors"
1514
)
1615

1716
var (
18-
Knot *node.Node
19-
Parent *node.Node
17+
Knot *node.Node
2018

2119
key string
2220
objStart bool
@@ -46,7 +44,7 @@ func ReadJson(filename string) ([]byte, error) {
4644
// DecodeJson
4745
// Converts a byte slice to a `node.Node` structure.
4846
func DecodeJson(data []byte) (*node.Node, error) {
49-
dec := json.NewDecoder(strings.NewReader(string(data)))
47+
dec := json.NewDecoder(bytes.NewReader(data))
5048
for {
5149
token, err := dec.Token()
5250
if err == io.EOF || err != nil {
@@ -56,109 +54,94 @@ func DecodeJson(data []byte) (*node.Node, error) {
5654
value := fmt.Sprintf("%v", token)
5755
// If the type of the object is json.Delim
5856
if reflect.TypeOf(token).String() == "json.Delim" {
59-
jsonDelim(token, value)
57+
jsonDelim(value)
6058
} else {
61-
nonJsonDelim(token, value)
59+
nonJsonDelim(value)
6260
}
6361
}
6462
}
6563

6664
// jsonDelim
67-
func jsonDelim(token json.Token, value string) {
68-
// If no Node has been created yet, don't enter here, skip JSON start -> {
65+
func jsonDelim(value string) {
6966
if !Knot.Exists() {
67+
objStart = true
68+
Knot = &node.Node{}
7069
return
7170
}
72-
// If the value of object token is a JSON object or array
7371
switch value {
7472
case "{": // set open object - {
75-
// If the key is not empty; this is an object. A new Node will be added next to the existing Node, and the newly added Node will return.
76-
// If the key is empty; There is an array object, and the Node will be created in the array and the newly added Node will be returned.
7773
if arrStart && len(key) == 0 {
78-
// An object only starts without a key in an array.
79-
Knot = Knot.AddToValue(Knot, node.Value{Node: &node.Node{Prev: Knot}})
74+
sub := &node.Node{}
75+
Knot.Value.Array = append(Knot.Value.Array, &node.Value{Node: sub})
76+
sub.Parent = Knot
77+
Knot = sub
8078
} else {
81-
Knot = Knot.AddToNext(Knot, Parent, key)
79+
sub := &node.Node{}
80+
Knot.Next = &node.Node{Key: key, Value: &node.Value{Node: sub}, Prev: Knot, Parent: Knot.Parent}
81+
sub.Parent = Knot.Next
82+
Knot = sub
8283
}
83-
Parent = Knot
8484
objStart = true
8585
arrStart = false
8686
key = ""
8787
case "[": // set open array - [
88-
// If the key is not null and objStart is true; The initial value of the Node will be set.
89-
// If the key is not empty and objStart is false; A new Node will be created next to the Node.
90-
// If the key is empty; this is a nested array object. will be added directly to the current Node's array.
9188
if len(key) > 0 {
92-
// If objStart is true, then the initial value of the Node is set.
9389
if objStart {
94-
Knot = Knot.AddToValue(Knot, node.Value{Array: []node.Value{}})
90+
Knot.Key = key
91+
Knot.Value = &node.Value{Array: []*node.Value{}}
9592
} else {
96-
// If objStart is false, a new Node is created next to the current Node.
97-
Knot = Knot.AddToNext(Knot, Parent, key)
93+
Knot.Next = &node.Node{Key: key, Value: &node.Value{}, Parent: Knot.Parent, Prev: Knot}
94+
Knot = Knot.Next
9895
}
99-
Parent = Knot
10096
arrStart = true
10197
objStart = false
10298
arrCount++
10399
key = ""
104-
} else {
105-
// If there is no key, it is a nested array.
106-
_ = append(Knot.Value.Array, node.Value{Worth: value})
107100
}
108101
case "]": // set close array
109-
arrCount--
110102
arrStart = false
103+
if arrCount > 0 {
104+
arrCount--
105+
}
111106
case "}": // set close object and set parent Node
112-
Parent = nil
113-
// if the parent is not empty
107+
objStart = false
108+
if arrCount > 0 {
109+
arrStart = true
110+
}
114111
if Knot.Parent != nil {
115-
// if there is an unclosed array and there is no key
116-
if arrCount > 0 && len(Knot.Key) == 0 {
117-
arrStart = true
118-
}
119-
// use parent as a node
120112
Knot = Knot.Parent
121-
// assign the parent of the node as parent, it can be nil or node
122-
Parent = Knot.Parent
123113
}
124114
default: // shouldn't go here
125-
log.Println("default not set -> ", token)
115+
log.Println("default not set")
126116
}
127117
}
128118

129119
// nonJsonDelim
130-
func nonJsonDelim(_ json.Token, value string) {
131-
// If the loop object is not a json.Delim, the key and value fields will be set.
132-
// Since the JSON object is a key-value pair, first the key will be set and then the value will be set.
120+
func nonJsonDelim(value string) {
133121
if len(key) == 0 {
134-
// If an array object is open, this key-value is essentially an array object.
135-
// If the array is not empty
136122
if arrStart {
137-
_ = append(Knot.Value.Array, node.Value{Worth: value})
123+
Knot.Value.Array = append(Knot.Value.Array, &node.Value{Worth: value})
138124
} else {
139125
key = value
140126
}
141127
} else {
142-
// If objStart is true, then the initial value of the Node is set.
143128
if objStart {
144-
Knot = Knot.AddToValue(Knot, node.Value{Worth: value})
129+
Knot.Key = key
130+
Knot.Value = &node.Value{Worth: value}
145131
} else {
146-
Knot = Knot.AddToNext(Knot, Parent, key)
132+
Knot.Next = &node.Node{Key: key, Parent: Knot.Parent, Prev: Knot}
133+
Knot.Next.Value = &node.Value{Worth: value}
134+
Knot = Knot.Next
147135
}
148-
// Here objStart and key values are reset.
149136
objStart = false
150137
key = ""
151138
}
152139
}
153140

154141
// NodeToJson
155-
// TODO test
156142
func NodeToJson(node *node.Node) ([]byte, error) {
157-
var (
158-
buf bytes.Buffer
159-
enc *json.Encoder
160-
)
161-
enc = json.NewEncoder(&buf)
143+
var buf bytes.Buffer
144+
enc := json.NewEncoder(&buf)
162145
enc.SetIndent(" ", " ")
163146
err := enc.Encode(node)
164147
if err != nil {

0 commit comments

Comments
 (0)