Skip to content

Commit 27f7aa9

Browse files
committed
finished
Signed-off-by: gang.liu <[email protected]>
1 parent 22f1ec7 commit 27f7aa9

File tree

5 files changed

+43
-16
lines changed

5 files changed

+43
-16
lines changed

conv.go

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package main
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"encoding/xml"
67
"errors"
8+
"io"
79
"log"
810

11+
mxj "github.com/clbanning/mxj/v2"
912
typev3 "github.com/envoyproxy/go-control-plane/envoy/type/v3"
1013
ep "github.com/wrossmorrow/envoy-extproc-sdk-go"
1114
)
@@ -37,14 +40,23 @@ func detectFormat(data []byte) (string, error) {
3740
return kJSON, nil
3841
}
3942

40-
var xmlObj any
41-
if err := xml.Unmarshal(data, &xmlObj); err == nil {
43+
if isValidXML(data) {
4244
return kXML, nil
4345
}
4446

4547
return "", errors.New("unknown format")
4648
}
4749

50+
func isValidXML(input []byte) bool {
51+
decoder := xml.NewDecoder(bytes.NewReader(input))
52+
for {
53+
err := decoder.Decode(new(any))
54+
if err != nil {
55+
return err == io.EOF
56+
}
57+
}
58+
}
59+
4860
func (s *convRequestProcessor) ProcessRequestBody(ctx *ep.RequestContext, body []byte) error {
4961
cancel := func(code int32) error {
5062
return ctx.CancelRequest(code, map[string]ep.HeaderValue{}, typev3.StatusCode_name[code])
@@ -98,22 +110,34 @@ func (s *convRequestProcessor) Init(opts *ep.ProcessingOptions, nonFlagArgs []st
98110

99111
func (s *convRequestProcessor) Finish() {}
100112

113+
// jsonToXML converts a JSON byte array to XML byte array.
114+
func jsonToXML(data []byte) ([]byte, error) {
115+
mxj.XMLEscapeChars(true)
116+
m, err := mxj.NewMapJsonReader(bytes.NewReader(data))
117+
if err != nil {
118+
return nil, err
119+
}
120+
121+
buf := &bytes.Buffer{}
122+
err = m.XmlIndentWriter(buf, "", "\t")
123+
if err != nil {
124+
return nil, err
125+
}
126+
return buf.Bytes(), nil
127+
}
128+
101129
// xmlToJSON converts XML data to JSON.
102130
func xmlToJSON(data []byte) ([]byte, error) {
103-
var m map[string]any
104-
err := xml.Unmarshal(data, &m)
131+
mxj.XMLEscapeChars(true)
132+
m, err := mxj.NewMapXmlReader(bytes.NewReader(data))
105133
if err != nil {
106134
return nil, err
107135
}
108-
return json.Marshal(m)
109-
}
110136

111-
// jsonToXML converts JSON data to XML.
112-
func jsonToXML(data []byte) ([]byte, error) {
113-
var m map[string]any
114-
err := json.Unmarshal(data, &m)
137+
buf := &bytes.Buffer{}
138+
err = m.JsonIndentWriter(buf, "", "\t")
115139
if err != nil {
116140
return nil, err
117141
}
118-
return xml.Marshal(m)
142+
return buf.Bytes(), nil
119143
}

deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ spec:
1919
containers:
2020
- name: xml-json-conv-demo-container
2121
image: >-
22-
release.daocloud.io/skoala/envoy-extproc-payloadlimit-demo-go@sha256:dcc5ba7aa2790e87b0cd46799363367b54202585794ebfe87248264f111e63d8
22+
release.daocloud.io/skoala/envoy-extproc-xml-json-conv-demo-go:v0.0.1
2323
ports:
2424
- containerPort: 50051
2525
protocol: TCP

envoy.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ static_resources:
22
listeners:
33
- name: listener
44
address:
5-
socket_address: {address: 0.0.0.0, port_value: 8000}
5+
socket_address: {address: 0.0.0.0, port_value: 8080}
66
filter_chains:
77
- filters:
88
- name: envoy.filters.network.http_connection_manager
@@ -68,8 +68,8 @@ static_resources:
6868
hostname: upstream
6969
address:
7070
socket_address:
71-
address: upstream
72-
port_value: 8000
71+
address: 127.0.0.1 # if in a k8s cluster, use the service name
72+
port_value: 5678
7373
- name: xml-json-conv
7474
dns_lookup_family: V4_ONLY
7575
lb_policy: LEAST_REQUEST
@@ -80,7 +80,7 @@ static_resources:
8080
- endpoint:
8181
address:
8282
socket_address:
83-
address: xml-json-conv
83+
address: 127.0.0.1 # if in a k8s cluster, use the service name
8484
port_value: 50051
8585
hostname: xml-json-conv
8686
type: LOGICAL_DNS

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/projectsesame/envoy-extproc-xml-json-conv-demo-go
33
go 1.21
44

55
require (
6+
github.com/clbanning/mxj/v2 v2.7.0
67
github.com/envoyproxy/go-control-plane v0.12.0
78
github.com/wrossmorrow/envoy-extproc-sdk-go v0.0.21
89
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/clbanning/mxj/v2 v2.7.0 h1:WA/La7UGCanFe5NpHF0Q3DNtnCsVoxbPKuyBNHWRyME=
2+
github.com/clbanning/mxj/v2 v2.7.0/go.mod h1:hNiWqW14h+kc+MdF9C6/YoRfjEJoR3ou6tn/Qo+ve2s=
13
github.com/cncf/xds/go v0.0.0-20231128003011-0fa0005c9caa h1:jQCWAUqqlij9Pgj2i/PB79y4KOPYVyFYdROxgaCwdTQ=
24
github.com/cncf/xds/go v0.0.0-20231128003011-0fa0005c9caa/go.mod h1:x/1Gn8zydmfq8dk6e9PdstVsDgu9RuyIIJqAaF//0IM=
35
github.com/envoyproxy/go-control-plane v0.12.0 h1:4X+VP1GHd1Mhj6IB5mMeGbLCleqxjletLK6K0rbxyZI=

0 commit comments

Comments
 (0)