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

Is it possible to interpolate nested object? #983

Open
pombadev opened this issue Apr 9, 2022 · 4 comments
Open

Is it possible to interpolate nested object? #983

pombadev opened this issue Apr 9, 2022 · 4 comments
Labels

Comments

@pombadev
Copy link

pombadev commented Apr 9, 2022

My jsonnet file looks like this.

local constants = {
  sys: {
    os: 'linux',
    arch: ['amd64', 'x86_64'],
  },
};

{
  prefix: '~/.local',
  packages: [
    {
      local version = '1.18',

      name: 'golang',
      version: version,
      file: 'go%(version)s.%(sys.os)s-%(sys.arch[0])s.tar.gz' % { version: version } + constants,
      description: 'The Go programming language.',
      url: 'https://dl.google.com/go/' + self.file,
      prebuild: "curl -LO '%(url)s'" % self,

      build: |||
        rm -rf %(prefix)s/go
        tar -C %(prefix)s -xzf %(file)s
      ||| % { prefix: $.prefix, url: $.packages[0].file },
    },
  ],
}

I'm trying to access nested field but get an error:

...
file: 'go%(version)s.%(sys.os)s-%(sys.arch[0])s.tar.gz' % { version: version } + constants,
...

Error:

RUNTIME ERROR: No such field: sys.os

Expected:

file: "go1.18.linux-amd64.tar.gz"
@johnbartholomew
Copy link
Collaborator

Unfortunately, no. The format operator % / format function std.format do not perform full expression interpolation, they only look up the specified key in a flat object. This is the same as the Python 2 string-formatting % operator (https://docs.python.org/2/library/stdtypes.html#string-formatting-operations).

There was a related feature request from several years ago, #45, asking to add support for ES6 backtick strings (aka template literals).

(I'm aware this question was from a while ago, but perhaps having an answer will be useful for someone in the future)

@CertainLach
Copy link
Contributor

std.format supports nested objects: #1011
But syntax is limited to only objects, no array access.

@johnbartholomew
Copy link
Collaborator

std.format supports nested objects: #1011

Oh, huh... that's interesting. I did not realize that. Thanks for the correction!

@johnbartholomew
Copy link
Collaborator

Hmm... it looks like that feature was reverted: #1099

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

No branches or pull requests

3 participants