You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "Revolyssup (via GitHub)" <gi...@apache.org> on 2024/04/01 07:21:43 UTC

Re: [PR] feat: Autogenerate admin api key if not passed [apisix]

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


##########
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:
   The lyaml library doesnt do null parsing properly. It converts NULL into`[]:` producing invalid yaml. And when I use the workaround of replacing null [here](https://github.com/apache/apisix/pull/11080/files/26a948aa6366ec39de33d875e66c0a819d5526ea#diff-ab3c55c5825b9d6c1f2a2e296c658ea10092998b5a53feca14fd3988bcf8209aR80), it messes up boolean values so these lines fix them back. I will refactor it to make it a little concise.



-- 
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