You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/09/16 03:34:19 UTC

[GitHub] [apisix] arthur-zhang commented on a change in pull request #5016: feat: xml-json-conversion plugin convert xml data from request body to json response, and vice versa

arthur-zhang commented on a change in pull request #5016:
URL: https://github.com/apache/apisix/pull/5016#discussion_r709725297



##########
File path: apisix/plugins/xml-json-conversion.lua
##########
@@ -0,0 +1,156 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+
+local core    = require("apisix.core")
+local xml2lua = require("xml2lua")
+local handler = require("xmlhandler.tree")
+local cjson   = require('cjson.safe')
+local string  = require("string")
+
+local schema = {
+    type = "object",
+    properties = {
+        from = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "xml"
+        },
+        to = {
+            type = "string",
+            enum = {"json", "xml"},
+            default = "json"
+        }
+    },
+    additionalProperties = false,
+}
+
+local plugin_name = "xml-json-conversion"
+
+local _M = {
+    version = 0.1,
+    priority = 90,
+    name = plugin_name,
+    schema = schema,
+}
+
+function _M.check_schema(conf)
+    return core.schema.check(schema, conf)
+end
+
+local function xml2json(xml_data)
+    local convertHandler = handler:new()
+    local parser = xml2lua.parser(convertHandler)
+    parser:parse(xml_data)
+    return 200, cjson.encode(convertHandler.root)
+end
+
+local function json2xml(table_data)
+    local xmlStr = xml2lua.toXml(cjson.decode(table_data))
+    xmlStr = string.gsub(xmlStr, "%s+", "")

Review comment:
       I think Camel-Case naming is not good here




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org