Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.mergePatch loses hidden fields #2 #1041

Open
dxlr8r opened this issue Dec 2, 2022 · 7 comments
Open

std.mergePatch loses hidden fields #2 #1041

dxlr8r opened this issue Dec 2, 2022 · 7 comments

Comments

@dxlr8r
Copy link

dxlr8r commented Dec 2, 2022

Sorry for resurrecting an old thread. But I came a cross the same issue the other day. I use Jsonnet for Terraform, and the way I create modules using Jsonnet, and want to pass data between the modules (which are merged using std.mergePatch), hidden fields I want to lookup are missing after I merge them.

@sparkprime
Copy link
Collaborator

Can't you make them non-hidden? Merge Patch doesn't have a concept of hidden fields because it's a JSON standard.

@dxlr8r
Copy link
Author

dxlr8r commented Dec 2, 2022

Terraform is very particular with what fields it accept; so the short answer no. The longer answer is, yes of course there are workarounds. I have one implemented, but the code is far more readable, maintainable less DRY without it. Now I have to add several lines of boilerplate per module.

@sparkprime
Copy link
Collaborator

Can you use the language's mixin support instead of std.mergePatch?

@dxlr8r
Copy link
Author

dxlr8r commented Jan 20, 2023

That would be very difficult. In this case, I want to merge several sources. Some of those sources are jsonnet, and there mixin can be used. Others are HCL converted to JSON, one could perhaps write some code to edit the JSON and add + where needed, but that seems like a dirty hack.

@sparkprime
Copy link
Collaborator

you could write a wrapper that imports the json and uses it to build an object that will mix together nicely

@dxlr8r
Copy link
Author

dxlr8r commented Feb 2, 2023

I could, many different workarounds one could think of. Personally I believe people would except std.mergePatch to merge Jsonnet objects. My alternatives for my use case is to either create a recursive function that merges Jsonnet objects, which would work like std.mergePatch but without deleting hidden fields, or wait for std.mergePatch to be updated.

@serge-salamanka
Copy link

i have come up to the function like this to solve import from json to make it a jsonnet object

local main       = import 'lib/main.libsonnet';
local overrides  = std.parseJson(importstr 'etc/config.json'); // for safe import

// reference to the helpful comment because of the missing documentation https://stackoverflow.com/a/76149084
local jsonnet_object(o) = {
    [c.key]+: if std.isObject(c.value) then jsonnet_object(c.value) else c.value
    for c in std.objectKeysValues(o)
    if std.isObject(c.value)
} + {
  [c.key]: c.value
  for c in std.objectKeysValues(o)
  if !std.isObject(c.value)
};

local config     =  main {config+: jsonnet_object(overrides.config)};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants