Revolyssup commented on code in PR #11080:
URL: https://github.com/apache/apisix/pull/11080#discussion_r1546044281


##########
apisix/core/id.lua:
##########
@@ -61,19 +63,88 @@ local function write_file(path, data)
     return true
 end
 
+local function generate_yaml(table)
+    -- By default lyaml will parse null values as []
+    -- The following logic is a workaround so that null values are parsed as 
null
+    local function replace_null(tbl)
+        for k, v in pairs(tbl) do
+            if type(v) == "table" then
+                replace_null(v)
+            elseif v == nil then
+                tbl[k] = "<PLACEHOLDER>"
+            end
+        end
+    end
+
+    -- Replace null values with "<PLACEHOLDER>"
+    replace_null(table)
+
+    -- Convert Lua table to YAML string without parsing null values
+    local yaml = lyaml.dump({ table }, { no_nil = true })
+
+    -- Replace "<PLACEHOLDER>" with null except for empty arrays
+    yaml = yaml:gsub("<PLACEHOLDER>", "null"):gsub("%[%s*%]", "null")
+
+    -- Ensure boolean values remain intact
+    yaml = yaml:gsub(":%s*true%s*true", ": true"):gsub(":%s*false%s*true", ": 
false")
+
+    -- Replace *no_nil with true
+    yaml = yaml:gsub("&no_nil", "true")
+
+    -- Remove any occurrences of *no_nil
+    yaml = yaml:gsub(":%s*%*no_nil", ": true")
+
+    -- Remove duplicates for boolean values
+    yaml = yaml:gsub("true%s*true", "true"):gsub("false%s*false", "false")

Review Comment:
   I removed the unnecessary lines.



-- 
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-unsubscr...@apisix.apache.org

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

Reply via email to