krakend / krakend-lua

a lua interpreter for the KrakenD framework
Apache License 2.0
7 stars 14 forks source link

Transform response body #7

Closed jume-dev closed 4 years ago

jume-dev commented 4 years ago

Hi there, first of all, I'd like to thank you for offering this awesome method of scripting for the krakenD gateway. I am a complete novice considering go and lua, but I got some nice things working with only small effort. Sadly I got stuck now and struggle to find appropriate documentation. I hope it is ok to open an issue here even if it is mainly a question on how to do things.

For context, this is what I am trying to achieve. I get a json response from a ruby backend with snake case keys like this:

{
  "items": [
    {"id": 1, "name": "foo", "funny_property": true, "long_name": "foo bar"},
    {"id": 2, "name": "foo2", "funny_property": false, "long_name": "foo2 bar2"},
    ...
  ]
}

And I want to prepare it for the javascript frontend which likes to have it's json with camelCase like this:

{
  "items": [
    {"id": 1, "name": "foo", "funnyProperty": true, "longName": "foo bar"},
    {"id": 2, "name": "foo2", "funnyProperty": false, "longName": "foo2 bar2"},
    ...
  ]
}

While I think the main algorithm isn't that hard to write (even for a lua novice), I struggle with what data types I really get from methods like: response.load():data():get('items') and what I can do with it. For example while I managed to iterate over this list of items I couldn't figure out how to iterate over every key, value pair of the item itself.

Any hints on tackling this are highly appreciated :) Thanks for reading so far and have a nice day

kpacha commented 4 years ago

I'm not a LUA expert, but I think it could be done using the stdlib. For example, this pair of lines

result, _ = string.gsub("some_var", "(%w)_(%w)", function(w,x) return w..string.upper(x) end)

print(result)

will return

someVar
jume-dev commented 4 years ago

Ah yes, I see, I should've added more details, sorry about that.

Yes I got that already but what I struggle with is how to reflect about every key from the backend response to even make that tranformation.

So for example I tried iterating over every pair in the items array I mentioned above and got an error, that items is not the right data type table but that it is userdata.

Maybe I just clarify what my question really is: How do I reflect on every key in a backend response without knowing the explicit response? I think this is not really a lua question but more a question on the lua intepreter you guys wrote here.

Please guide me to the right place, if this is wrong here to ask.

vladkras commented 3 years ago

you don't need Lua here, just use flatmap_filter:

          "extra_config": {
              "github.com/devopsfaith/krakend/proxy": {
                "flatmap_filter": [
                    {
                        "type": "move",
                        "args": ["items.*.funny_property", "items.*.funnyProperty"]
                    },
                    {
                        "type": "move",
                        "args": ["items.*.long_name", "items.*.longName"]
                    }
                ]
            }
          }
jume-dev commented 3 years ago

you don't need Lua here, just use flatmap_filter:

          "extra_config": {
              "github.com/devopsfaith/krakend/proxy": {
                "flatmap_filter": [
                    {
                        "type": "move",
                        "args": ["items.*.funny_property", "items.*.funnyProperty"]
                    },
                    {
                        "type": "move",
                        "args": ["items.*.long_name", "items.*.longName"]
                    }
                ]
            }
          }

Thanks for pointing that out, flatmap is indeed helpful for smaller objects. But if I read the docs correctly, there is no way to transform all the properties by reflection, right?

github-actions[bot] commented 2 years ago

This issue was marked as resolved a long time ago and now has been automatically locked as there has not been any recent activity after it. You can still open a new issue and reference this link.