From de47c4237f1364a48de85aed0842d4d0cdc7ab0a Mon Sep 17 00:00:00 2001 From: Tommy Situ Date: Sun, 21 Jan 2024 22:24:41 +0000 Subject: [PATCH] Use forked goxml2json library (#12) --- core/util/util.go | 2 +- go.mod | 3 +- go.sum | 32 +++- .../goxml2json/.gitignore | 0 .../{basgys => SpectoLabs}/goxml2json/LICENSE | 0 .../goxml2json/README.md | 29 +++- .../goxml2json/converter.go | 7 +- .../goxml2json/decoder.go | 35 ++-- .../{basgys => SpectoLabs}/goxml2json/doc.go | 0 .../goxml2json/encoder.go | 44 +++-- .../SpectoLabs/goxml2json/jstype.go | 74 ++++++++ .../SpectoLabs/goxml2json/plugins.go | 161 ++++++++++++++++++ .../SpectoLabs/goxml2json/struct.go | 47 +++++ vendor/github.com/basgys/goxml2json/struct.go | 25 --- vendor/modules.txt | 8 +- 15 files changed, 392 insertions(+), 75 deletions(-) rename vendor/github.com/{basgys => SpectoLabs}/goxml2json/.gitignore (100%) rename vendor/github.com/{basgys => SpectoLabs}/goxml2json/LICENSE (100%) rename vendor/github.com/{basgys => SpectoLabs}/goxml2json/README.md (74%) rename vendor/github.com/{basgys => SpectoLabs}/goxml2json/converter.go (64%) rename vendor/github.com/{basgys => SpectoLabs}/goxml2json/decoder.go (80%) rename vendor/github.com/{basgys => SpectoLabs}/goxml2json/doc.go (100%) rename vendor/github.com/{basgys => SpectoLabs}/goxml2json/encoder.go (84%) create mode 100644 vendor/github.com/SpectoLabs/goxml2json/jstype.go create mode 100644 vendor/github.com/SpectoLabs/goxml2json/plugins.go create mode 100644 vendor/github.com/SpectoLabs/goxml2json/struct.go delete mode 100644 vendor/github.com/basgys/goxml2json/struct.go diff --git a/core/util/util.go b/core/util/util.go index c4ba2f9d6..5493c60d2 100644 --- a/core/util/util.go +++ b/core/util/util.go @@ -25,7 +25,7 @@ import ( "strconv" "time" - xj "github.com/basgys/goxml2json" + xj "github.com/SpectoLabs/goxml2json" "github.com/tdewolff/minify/v2" mjson "github.com/tdewolff/minify/v2/json" "github.com/tdewolff/minify/v2/xml" diff --git a/go.mod b/go.mod index 2ea7afdbf..76cbfb25f 100644 --- a/go.mod +++ b/go.mod @@ -6,9 +6,9 @@ require ( github.com/ChrisTrenkamp/xsel v0.9.6 github.com/SpectoLabs/goproxy v0.0.0-20220724221645-71c396c297b7 github.com/SpectoLabs/goproxy/ext v0.0.0-20220724221645-71c396c297b7 + github.com/SpectoLabs/goxml2json v0.0.0-20240121223617-8e03292c14ea github.com/SpectoLabs/raymond v2.0.3-0.20240313210732-e0e216cf0920+incompatible github.com/antonholmquist/jason v1.0.1-0.20160829104012-962e09b85496 - github.com/basgys/goxml2json v1.1.0 github.com/beevik/etree v1.1.0 github.com/boltdb/bolt v1.2.1-0.20160424201119-d97499360d1e github.com/brianvoe/gofakeit/v6 v6.19.0 @@ -47,7 +47,6 @@ require ( require ( github.com/BurntSushi/toml v1.2.1 // indirect github.com/aymerick/raymond v2.0.2+incompatible // indirect - github.com/bitly/go-simplejson v0.5.1 // indirect github.com/corpix/uarand v0.0.0-20170903190822-2b8494104d86 // indirect github.com/fsnotify/fsnotify v1.5.4 // indirect github.com/google/go-cmp v0.5.9 // indirect diff --git a/go.sum b/go.sum index 07f2d7989..6fd8d3bd1 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,8 @@ github.com/SpectoLabs/goproxy v0.0.0-20220724221645-71c396c297b7 h1:TTZvVLRa0sjo github.com/SpectoLabs/goproxy v0.0.0-20220724221645-71c396c297b7/go.mod h1:tj8G0iNIS9fkG5PSrzQYLUXcvhtqI8XbIld9PqpYaFw= github.com/SpectoLabs/goproxy/ext v0.0.0-20220724221645-71c396c297b7 h1:MxMvFwL66nKnrmjHhp9GODbHVZ+/qS2MmvnXz/nDdR4= github.com/SpectoLabs/goproxy/ext v0.0.0-20220724221645-71c396c297b7/go.mod h1:rnDN1CpKP9iJXwxRtnvM7d5wdPHQY0K5NkHeXWPlQ+o= +github.com/SpectoLabs/goxml2json v0.0.0-20240121223617-8e03292c14ea h1:VAu3xT0OShIqnm4K3TdPpxCd3k4uyXq/H6W76ueIWf0= +github.com/SpectoLabs/goxml2json v0.0.0-20240121223617-8e03292c14ea/go.mod h1:P8Yk6l9gKxTfgnJeXVEQKCH7nGgvhNdeLs0zVs2h2KM= github.com/SpectoLabs/raymond v2.0.3-0.20240313210732-e0e216cf0920+incompatible h1:qI0OVNVzpRLUMHD3lxGOXSHsH87xHp09IuQm9hjTfTA= github.com/SpectoLabs/raymond v2.0.3-0.20240313210732-e0e216cf0920+incompatible/go.mod h1:Cv+3TFLm3T7C7ML7gEK4HnHcTpfc4HjJFWvS91cc6tw= github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw= @@ -14,8 +16,6 @@ github.com/antonholmquist/jason v1.0.1-0.20160829104012-962e09b85496 h1:dESITduf github.com/antonholmquist/jason v1.0.1-0.20160829104012-962e09b85496/go.mod h1:+GxMEKI0Va2U8h3os6oiUAetHAlGMvxjdpAH/9uvUMA= github.com/aymerick/raymond v2.0.2+incompatible h1:VEp3GpgdAnv9B2GFyTvqgcKvY+mfKMjPOA3SbKLtnU0= github.com/aymerick/raymond v2.0.2+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g= -github.com/basgys/goxml2json v1.1.0 h1:4ln5i4rseYfXNd86lGEB+Vi652IsIXIvggKM/BhUKVw= -github.com/basgys/goxml2json v1.1.0/go.mod h1:wH7a5Np/Q4QoECFIU8zTQlZwZkrilY0itPfecMw41Dw= github.com/beevik/etree v1.1.0 h1:T0xke/WvNtMoCqgzPhkX2r4rjY3GDZFi+FjpRZY2Jbs= github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A= github.com/bitly/go-simplejson v0.5.1 h1:xgwPbetQScXt1gh9BmoJ6j9JMr3TElvuIyjR8pgdoow= @@ -159,9 +159,12 @@ github.com/xeipuuv/gojsonreference v0.0.0-20150808065054-e02fc20de94c/go.mod h1: github.com/xeipuuv/gojsonschema v0.0.0-20170225172124-ff0417f4272e h1:vhu5zToI4Rj62MgHj/UrrrM8RqNtKoCZUx8ARkpyzKA= github.com/xeipuuv/gojsonschema v0.0.0-20170225172124-ff0417f4272e/go.mod h1:5yf86TLmAcydyeJq5YvxkGPE2fm/u4myDekKRoLuqhs= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio= golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -173,18 +176,27 @@ golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86h golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210331212208-0fccb6fa2b5c/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= +golang.org/x/net v0.13.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA= golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -197,16 +209,30 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= +golang.org/x/term v0.10.0/go.mod h1:lpqdcUyK/oCiQxvxVrppt5ggO2KCZ5QblwqPnfZ6d5o= golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8= golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -215,6 +241,8 @@ golang.org/x/tools v0.0.0-20190206041539-40960b6deb8e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/vendor/github.com/basgys/goxml2json/.gitignore b/vendor/github.com/SpectoLabs/goxml2json/.gitignore similarity index 100% rename from vendor/github.com/basgys/goxml2json/.gitignore rename to vendor/github.com/SpectoLabs/goxml2json/.gitignore diff --git a/vendor/github.com/basgys/goxml2json/LICENSE b/vendor/github.com/SpectoLabs/goxml2json/LICENSE similarity index 100% rename from vendor/github.com/basgys/goxml2json/LICENSE rename to vendor/github.com/SpectoLabs/goxml2json/LICENSE diff --git a/vendor/github.com/basgys/goxml2json/README.md b/vendor/github.com/SpectoLabs/goxml2json/README.md similarity index 74% rename from vendor/github.com/basgys/goxml2json/README.md rename to vendor/github.com/SpectoLabs/goxml2json/README.md index 04e0b7faf..0abdfcda4 100644 --- a/vendor/github.com/basgys/goxml2json/README.md +++ b/vendor/github.com/SpectoLabs/goxml2json/README.md @@ -53,7 +53,7 @@ Go package that converts XML to JSON ```json { "osm": { - "-version": "0.6", + "-version": 0.6, "-generator": "CGImap 0.0.2", "bounds": { "-minlat": "54.0889580", @@ -66,17 +66,42 @@ Go package that converts XML to JSON } ``` +**With type conversion** + +```go + package main + + import ( + "fmt" + "strings" + + xj "github.com/basgys/goxml2json" + ) + + func main() { + // xml is an io.Reader + xml := strings.NewReader(`19.95`) + json, err := xj.Convert(xml, xj.WithTypeConverter(xj.Float)) + if err != nil { + panic("That's embarrassing...") + } + + fmt.Println(json.String()) + // {"price": 19.95} + } +``` + ### Contributing Feel free to contribute to this project if you want to fix/extend/improve it. ### Contributors - [DirectX](https://github.com/directx) + - [powerslacker](https://github.com/powerslacker) - [samuelhug](https://github.com/samuelhug) ### TODO - * Extract data types in JSON (numbers, boolean, ...) * Categorise errors * Option to prettify the JSON output * Benchmark diff --git a/vendor/github.com/basgys/goxml2json/converter.go b/vendor/github.com/SpectoLabs/goxml2json/converter.go similarity index 64% rename from vendor/github.com/basgys/goxml2json/converter.go rename to vendor/github.com/SpectoLabs/goxml2json/converter.go index 3e4cbce2a..a1311ab80 100644 --- a/vendor/github.com/basgys/goxml2json/converter.go +++ b/vendor/github.com/SpectoLabs/goxml2json/converter.go @@ -6,17 +6,18 @@ import ( ) // Convert converts the given XML document to JSON -func Convert(r io.Reader) (*bytes.Buffer, error) { +func Convert(r io.Reader, ps ...plugin) (*bytes.Buffer, error) { // Decode XML document root := &Node{} - err := NewDecoder(r).Decode(root) + err := NewDecoder(r, ps...).Decode(root) if err != nil { return nil, err } // Then encode it in JSON buf := new(bytes.Buffer) - err = NewEncoder(buf).Encode(root) + e := NewEncoder(buf, ps...) + err = e.Encode(root) if err != nil { return nil, err } diff --git a/vendor/github.com/basgys/goxml2json/decoder.go b/vendor/github.com/SpectoLabs/goxml2json/decoder.go similarity index 80% rename from vendor/github.com/basgys/goxml2json/decoder.go rename to vendor/github.com/SpectoLabs/goxml2json/decoder.go index af03356c9..a45079f47 100644 --- a/vendor/github.com/basgys/goxml2json/decoder.go +++ b/vendor/github.com/SpectoLabs/goxml2json/decoder.go @@ -19,6 +19,8 @@ type Decoder struct { err error attributePrefix string contentPrefix string + excludeAttrs map[string]bool + formatters []nodeFormatter } type element struct { @@ -35,6 +37,16 @@ func (dec *Decoder) SetContentPrefix(prefix string) { dec.contentPrefix = prefix } +func (dec *Decoder) AddFormatters(formatters []nodeFormatter) { + dec.formatters = formatters +} + +func (dec *Decoder) ExcludeAttributes(attrs []string) { + for _, attr := range attrs { + dec.excludeAttrs[attr] = true + } +} + func (dec *Decoder) DecodeWithCustomPrefixes(root *Node, contentPrefix string, attributePrefix string) error { dec.contentPrefix = contentPrefix dec.attributePrefix = attributePrefix @@ -42,21 +54,17 @@ func (dec *Decoder) DecodeWithCustomPrefixes(root *Node, contentPrefix string, a } // NewDecoder returns a new decoder that reads from r. -func NewDecoder(r io.Reader) *Decoder { - return &Decoder{r: r} +func NewDecoder(r io.Reader, plugins ...plugin) *Decoder { + d := &Decoder{r: r, contentPrefix: contentPrefix, attributePrefix: attrPrefix, excludeAttrs: map[string]bool{}} + for _, p := range plugins { + d = p.AddToDecoder(d) + } + return d } // Decode reads the next JSON-encoded value from its // input and stores it in the value pointed to by v. func (dec *Decoder) Decode(root *Node) error { - - if dec.contentPrefix == "" { - dec.contentPrefix = contentPrefix - } - if dec.attributePrefix == "" { - dec.attributePrefix = attrPrefix - } - xmlDec := xml.NewDecoder(dec.r) // That will convert the charset if the provided XML is non-UTF-8 @@ -85,6 +93,9 @@ func (dec *Decoder) Decode(root *Node) error { // Extract attributes as children for _, a := range se.Attr { + if _, ok := dec.excludeAttrs[a.Name.Local]; ok { + continue + } elem.n.AddChild(dec.attributePrefix+a.Name.Local, &Node{Data: a.Value}) } case xml.CharData: @@ -101,6 +112,10 @@ func (dec *Decoder) Decode(root *Node) error { } } + for _, formatter := range dec.formatters { + formatter.Format(root) + } + return nil } diff --git a/vendor/github.com/basgys/goxml2json/doc.go b/vendor/github.com/SpectoLabs/goxml2json/doc.go similarity index 100% rename from vendor/github.com/basgys/goxml2json/doc.go rename to vendor/github.com/SpectoLabs/goxml2json/doc.go diff --git a/vendor/github.com/basgys/goxml2json/encoder.go b/vendor/github.com/SpectoLabs/goxml2json/encoder.go similarity index 84% rename from vendor/github.com/basgys/goxml2json/encoder.go rename to vendor/github.com/SpectoLabs/goxml2json/encoder.go index 0a542b8a8..c9fd995b6 100644 --- a/vendor/github.com/basgys/goxml2json/encoder.go +++ b/vendor/github.com/SpectoLabs/goxml2json/encoder.go @@ -12,25 +12,16 @@ type Encoder struct { err error contentPrefix string attributePrefix string + tc encoderTypeConverter } // NewEncoder returns a new encoder that writes to w. -func NewEncoder(w io.Writer) *Encoder { - return &Encoder{w: w} -} - -func (enc *Encoder) SetAttributePrefix(prefix string) { - enc.attributePrefix = prefix -} - -func (enc *Encoder) SetContentPrefix(prefix string) { - enc.contentPrefix = prefix -} - -func (enc *Encoder) EncodeWithCustomPrefixes(root *Node, contentPrefix string, attributePrefix string) error { - enc.contentPrefix = contentPrefix - enc.attributePrefix = attributePrefix - return enc.Encode(root) +func NewEncoder(w io.Writer, plugins ...plugin) *Encoder { + e := &Encoder{w: w, contentPrefix: contentPrefix, attributePrefix: attrPrefix} + for _, p := range plugins { + e = p.AddToEncoder(e) + } + return e } // Encode writes the JSON encoding of v to the stream @@ -41,12 +32,6 @@ func (enc *Encoder) Encode(root *Node) error { if root == nil { return nil } - if enc.contentPrefix == "" { - enc.contentPrefix = contentPrefix - } - if enc.attributePrefix == "" { - enc.attributePrefix = attrPrefix - } enc.err = enc.format(root, 0) @@ -82,7 +67,7 @@ func (enc *Encoder) format(n *Node, lvl int) error { enc.write(label) enc.write("\": ") - if len(children) > 1 { + if n.ChildrenAlwaysAsArray || len(children) > 0 { // Array enc.write("[") for j, c := range children { @@ -106,8 +91,14 @@ func (enc *Encoder) format(n *Node, lvl int) error { enc.write("}") } else { - // TODO : Extract data type - enc.write(sanitiseString(n.Data)) + s := sanitiseString(n.Data) + if enc.tc == nil { + // do nothing + } else { + s = enc.tc.Convert(s) + } + enc.write(s) + } return nil @@ -124,6 +115,7 @@ func sanitiseString(s string) string { var buf bytes.Buffer buf.WriteByte('"') + start := 0 for i := 0; i < len(s); { if b := s[i]; b < utf8.RuneSelf { @@ -192,6 +184,8 @@ func sanitiseString(s string) string { if start < len(s) { buf.WriteString(s[start:]) } + buf.WriteByte('"') + return buf.String() } diff --git a/vendor/github.com/SpectoLabs/goxml2json/jstype.go b/vendor/github.com/SpectoLabs/goxml2json/jstype.go new file mode 100644 index 000000000..c0c08cb5f --- /dev/null +++ b/vendor/github.com/SpectoLabs/goxml2json/jstype.go @@ -0,0 +1,74 @@ +package xml2json + +import ( + "strconv" + "strings" +) + +// https://cswr.github.io/JsonSchema/spec/basic_types/ +// JSType is a JavaScript extracted from a string +type JSType int + +const ( + Bool JSType = iota + Int + Float + String + Null +) + +// Str2JSType extract a JavaScript type from a string +func Str2JSType(s string) JSType { + var ( + output JSType + ) + s = strings.TrimSpace(s) // santize the given string + switch { + case isBool(s): + output = Bool + case isFloat(s): + output = Float + case isInt(s): + output = Int + case isNull(s): + output = Null + default: + output = String // if all alternatives have been eliminated, the input is a string + } + return output +} + +func isBool(s string) bool { + return s == "true" || s == "false" +} + +func isFloat(s string) bool { + var output = false + if strings.Contains(s, ".") { + _, err := strconv.ParseFloat(s, 64) + if err == nil { // the string successfully converts to a decimal + output = true + } + } + return output +} + +func isInt(s string) bool { + var output = false + if len(s) >= 1 { + _, err := strconv.Atoi(s) + if err == nil { // the string successfully converts to an int + if s != "0" && s[0] == '0' { + // if the first rune is '0' and there is more than 1 rune, then the input is most likely a float or intended to be + // a string value -- such as in the case of a guid, or an international phone number + } else { + output = true + } + } + } + return output +} + +func isNull(s string) bool { + return s == "null" +} diff --git a/vendor/github.com/SpectoLabs/goxml2json/plugins.go b/vendor/github.com/SpectoLabs/goxml2json/plugins.go new file mode 100644 index 000000000..60137f05d --- /dev/null +++ b/vendor/github.com/SpectoLabs/goxml2json/plugins.go @@ -0,0 +1,161 @@ +package xml2json + +import ( + "strings" +) + +type ( + // an plugin is added to an encoder or/and to an decoder to allow custom functionality at runtime + plugin interface { + AddToEncoder(*Encoder) *Encoder + AddToDecoder(*Decoder) *Decoder + } + // a type converter overides the default string sanitization for encoding json + encoderTypeConverter interface { + Convert(string) string + } + // customTypeConverter converts strings to JSON types using a best guess approach, only parses the JSON types given + // when initialized via WithTypeConverter + customTypeConverter struct { + parseTypes []JSType + } + + attrPrefixer string + contentPrefixer string + + excluder []string + + nodesFormatter struct { + list []nodeFormatter + } + nodeFormatter struct { + path string + plugin nodePlugin + } + + nodePlugin interface { + AddTo(*Node) + } + + arrayFormatter struct{} +) + +// WithTypeConverter allows customized js type conversion behavior by passing in the desired JSTypes +func WithTypeConverter(ts ...JSType) *customTypeConverter { + return &customTypeConverter{parseTypes: ts} +} + +func (tc *customTypeConverter) parseAsString(t JSType) bool { + if t == String { + return true + } + for i := 0; i < len(tc.parseTypes); i++ { + if tc.parseTypes[i] == t { + return false + } + } + return true +} + +// Adds the type converter to the encoder +func (tc *customTypeConverter) AddToEncoder(e *Encoder) *Encoder { + e.tc = tc + return e +} + +func (tc *customTypeConverter) AddToDecoder(d *Decoder) *Decoder { + return d +} + +func (tc *customTypeConverter) Convert(s string) string { + // remove quotes if they exists + if strings.HasPrefix(s, `"`) && strings.HasSuffix(s, `"`) { + s = s[1 : len(s)-1] + } + jsType := Str2JSType(s) + if tc.parseAsString(jsType) { + // add the quotes removed at the start of this func + s = `"` + s + `"` + } + return s +} + +// WithAttrPrefix appends the given prefix to the json output of xml attribute fields to preserve namespaces +func WithAttrPrefix(prefix string) *attrPrefixer { + ap := attrPrefixer(prefix) + return &ap +} + +func (a *attrPrefixer) AddToEncoder(e *Encoder) *Encoder { + e.attributePrefix = string((*a)) + return e +} + +func (a *attrPrefixer) AddToDecoder(d *Decoder) *Decoder { + d.attributePrefix = string((*a)) + return d +} + +// WithContentPrefix appends the given prefix to the json output of xml content fields to preserve namespaces +func WithContentPrefix(prefix string) *contentPrefixer { + c := contentPrefixer(prefix) + return &c +} + +func (c *contentPrefixer) AddToEncoder(e *Encoder) *Encoder { + e.contentPrefix = string((*c)) + return e +} + +func (c *contentPrefixer) AddToDecoder(d *Decoder) *Decoder { + d.contentPrefix = string((*c)) + return d +} + +// ExcludeAttributes excludes some xml attributes, for example, xmlns:xsi, xsi:noNamespaceSchemaLocation +func ExcludeAttributes(attrs []string) *excluder { + ex := excluder(attrs) + return &ex +} + +func (ex *excluder) AddToEncoder(e *Encoder) *Encoder { + return e +} + +func (ex *excluder) AddToDecoder(d *Decoder) *Decoder { + d.ExcludeAttributes([]string((*ex))) + return d +} + +// WithNodes formats specific nodes +func WithNodes(n ...nodeFormatter) *nodesFormatter { + return &nodesFormatter{list: n} +} + +func (nf *nodesFormatter) AddToEncoder(e *Encoder) *Encoder { + return e +} + +func (nf *nodesFormatter) AddToDecoder(d *Decoder) *Decoder { + d.AddFormatters(nf.list) + return d +} + +func NodePlugin(path string, plugin nodePlugin) nodeFormatter { + return nodeFormatter{path: path, plugin: plugin} +} + +func (nf *nodeFormatter) Format(node *Node) { + child := node.GetChild(nf.path) + if child != nil { + nf.plugin.AddTo(child) + } +} + +func ToArray() *arrayFormatter { + return &arrayFormatter{} +} + +func (af *arrayFormatter) AddTo(n *Node) { + n.ChildrenAlwaysAsArray = true +} diff --git a/vendor/github.com/SpectoLabs/goxml2json/struct.go b/vendor/github.com/SpectoLabs/goxml2json/struct.go new file mode 100644 index 000000000..350e1ac72 --- /dev/null +++ b/vendor/github.com/SpectoLabs/goxml2json/struct.go @@ -0,0 +1,47 @@ +package xml2json + +import ( + "strings" +) + +// Node is a data element on a tree +type Node struct { + Children map[string]Nodes + Data string + ChildrenAlwaysAsArray bool +} + +// Nodes is a list of nodes +type Nodes []*Node + +// AddChild appends a node to the list of children +func (n *Node) AddChild(s string, c *Node) { + // Lazy lazy + if n.Children == nil { + n.Children = map[string]Nodes{} + } + + n.Children[s] = append(n.Children[s], c) +} + +// IsComplex returns whether it is a complex type (has children) +func (n *Node) IsComplex() bool { + return len(n.Children) > 0 +} + +// GetChild returns child by path if exists. Path looks like "grandparent.parent.child.grandchild" +func (n *Node) GetChild(path string) *Node { + result := n + names := strings.Split(path, ".") + for _, name := range names { + children, exists := result.Children[name] + if !exists { + return nil + } + if len(children) == 0 { + return nil + } + result = children[0] + } + return result +} diff --git a/vendor/github.com/basgys/goxml2json/struct.go b/vendor/github.com/basgys/goxml2json/struct.go deleted file mode 100644 index 1f423b1ce..000000000 --- a/vendor/github.com/basgys/goxml2json/struct.go +++ /dev/null @@ -1,25 +0,0 @@ -package xml2json - -// Node is a data element on a tree -type Node struct { - Children map[string]Nodes - Data string -} - -// Nodes is a list of nodes -type Nodes []*Node - -// AddChild appends a node to the list of children -func (n *Node) AddChild(s string, c *Node) { - // Lazy lazy - if n.Children == nil { - n.Children = map[string]Nodes{} - } - - n.Children[s] = append(n.Children[s], c) -} - -// IsComplex returns whether it is a complex type (has children) -func (n *Node) IsComplex() bool { - return len(n.Children) > 0 -} diff --git a/vendor/modules.txt b/vendor/modules.txt index abd258148..d8aa5aaaf 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -21,6 +21,9 @@ github.com/SpectoLabs/goproxy # github.com/SpectoLabs/goproxy/ext v0.0.0-20220724221645-71c396c297b7 ## explicit github.com/SpectoLabs/goproxy/ext/auth +# github.com/SpectoLabs/goxml2json v0.0.0-20240121223617-8e03292c14ea +## explicit; go 1.16 +github.com/SpectoLabs/goxml2json # github.com/SpectoLabs/raymond v2.0.3-0.20240313210732-e0e216cf0920+incompatible ## explicit github.com/SpectoLabs/raymond @@ -32,14 +35,9 @@ github.com/antonholmquist/jason github.com/aymerick/raymond/ast github.com/aymerick/raymond/lexer github.com/aymerick/raymond/parser -# github.com/basgys/goxml2json v1.1.0 -## explicit -github.com/basgys/goxml2json # github.com/beevik/etree v1.1.0 ## explicit github.com/beevik/etree -# github.com/bitly/go-simplejson v0.5.1 -## explicit; go 1.17 # github.com/boltdb/bolt v1.2.1-0.20160424201119-d97499360d1e ## explicit github.com/boltdb/bolt