Skip to content

lib: Add string.find and object.{map,asNamedArray}#38

Open
camh- wants to merge 3 commits intomasterfrom
extend-lib
Open

lib: Add string.find and object.{map,asNamedArray}#38
camh- wants to merge 3 commits intomasterfrom
extend-lib

Conversation

@camh-
Copy link
Member

@camh- camh- commented Dec 28, 2024

Add some functions to the jnx library:

  • string.find: Returns the index of a substring within a string or -1 if
    not present.
  • map: Apply a function to each [k, v] pair of an object and return an
    array of values of the result of that function.
  • asNamedArray: Converts an object to an array of values from that
    object where each object has a field added with the key of that field
    (defaults to the 'name' field).

Hermitise this repository as the version of golangci-lint used is not
compatible with more recent Go releases so we will either need to pin to
an older version of Go or upgrade golangci-lint. The former is tricky to
do simply, and the latter is painful with the churn in golangci-lint.

The alternative is to use hermit and pin the versions currently
required. This is what is done here:

    hermit init --no-git
    hermit install go-1.13.5
    hermit install golangci-lint-1.37.0

Remove the custom version login for golangci-lint from the Makefile, and
add the hermit auto-activation. Note that the only 1.13 version of Go in
hermit is 1.13.5, while the latest point release is 1.13.15. I chose
1.13 as that is what is in the `go.mod` file.

Subsequent commits will bump these versions and adjust the code to work
with newer golangci-lint errors.

Update GitHub CI to rely on hermit for installing the right versions
instead of using specific actions. Also run on all PRs, not just those
to be merged to master. This allows CI to run for stacked PRs.
Rename `jx` to `jnx` as the jx binary name is already taken by jenkinsx.
But jnx is also a better name since it sounds like jinx, and that's what
you say when someone duplicates a word at the same time as you. jsonnet
is about de-duplicating through templating, so jnx works. It can also be
pronounced as a single syllable, which is also nice.

Split the Go flag version of jnx into a completely separate program
rather than having a `parseCLI()` function selected by build tags.

The only reason for the flag version is to use the flag code path of
jsonnext to demonstrate its use and to verify it works. jnx itself is
going to evolve in future to contain subcommands which I do not intend
to support in the flag version of jnx. So split it out so jnx can evolve
on its own.

Keep `jx.jsonnet` for now for backwards compatibility. It will be
removed when I know I've removed all existing uses under that name.
Add some functions to the jnx library:
* string.find: Returns the index of a substring within a string or -1 if
  not present.
* map: Apply a function to each [k, v] pair of an object and return an
  array of values of the result of that function.
* asNamedArray: Converts an object to an array of values from that
  object where each object has a field added with the key of that field
  (defaults to the 'name' field).
@camh- camh- requested a review from juliaogris December 28, 2024 22:02
juliaogris
juliaogris previously approved these changes Jan 6, 2025
Copy link
Member

@juliaogris juliaogris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just out of curiosity:
so the standard library has no substring find method in jsonnet?
also: isn't map typically used on arrays?
other than that, no questions, one lil typo. lgtm.

if !std.isString(str) then
error ('string.find first param must be a string, got ' + std.type(str))
else if !std.isString(char) then
error ('string.find secomd param must be a string, got ' + std.type(char))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/secomd/second/

@camh-
Copy link
Member Author

camh- commented Feb 1, 2025

just out of curiosity:
so the standard library has no substring find method in jsonnet?

It looks like there is one now - I guess it was not there when I wrote this:

std.findSubstr(pat, str)
Available since version 0.10.0.

Returns an array that contains the indexes of all occurrences of pat in str.

Not quite the same, but if I needed my function signature, I could implemented my find in terms of this one. I'll remove this.

also: isn't map typically used on arrays?

Probably. But it's still the same operation regardless of the data structure - apply a function to each element of the data structure. Perhaps I should call it mapKV() to so as to make room for mapK() and mapV(), but those two could still exist in the presence of a map() function. 🤷

Base automatically changed from rename-to-jnx to master February 4, 2025 05:30
@camh- camh- dismissed juliaogris’s stale review February 4, 2025 05:30

The base branch was changed.

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

Successfully merging this pull request may close these issues.

2 participants