Commit Graph

117 Commits

Author SHA1 Message Date
Rudo Thomas
1c79f577ba fix: Fix error messages when a comprehension iterates over a non-array.
Problems:
 - When iterating over an empty string in a list comprehension, the
   result is an empty string. This is a bug, it should be an error.
 - When iterating over a non-empty string in a list comprehension, the
   expected and unexpected types in the error message are swapped.
 - Error messages mention "std.flatMap" when object/list comprehensions
   would iterate over a value that is neither array nor string.

```
$ jsonnet --version
Jsonnet commandline interpreter (Go implementation) v0.21.0-rc2
$ jsonnet -e '[a for a in ""]'
""
$ jsonnet -e '[a for a in "b"]'
RUNTIME ERROR: Unexpected type array, expected string
	<cmdline>:1:1-17
	During evaluation
$ jsonnet -e '{[a]: 1 for a in 2}'
RUNTIME ERROR: std.flatMap second param must be array / string, got number
	<cmdline>:1:1-20
	<cmdline>:1:1-20
	During evaluation
$ jsonnet -e '[a for a in 1]'
RUNTIME ERROR: std.flatMap second param must be array / string, got number
	<cmdline>:1:1-15
	During evaluation
```

FWIW, the C++ implementation does not have any of these problems. It
gives:
```
RUNTIME ERROR: In comprehension, can only iterate over array.
```

In the Go implementation comprehensions are desugared to a call to
std.flatMap which does accept a string in the "arr" parameter.

The fix: Desugar comprehensions to a call to a new hidden builtin which
only accepts arrays.
2025-03-21 16:37:59 +00:00
John Bartholomew
9a89560b0d feat: support onEmpty optional parameter on std.minArray,std.maxArray 2025-03-11 00:33:41 +00:00
Eduardo Sánchez Muñoz
6a15a2f3fc fix: output original value from array in std.minArray and std.maxArray instead of result of keyF 2025-03-10 23:28:39 +00:00
John Bartholomew
b3fed3ba12 feat: add builtin atan2 and hypot functions 2025-01-18 17:05:14 +00:00
Marko Mikulicic
94a40b2991
Fix field visibility in objectHas for extended objects (#737) 2024-06-10 21:26:12 +01:00
Johannes Gräger
dec1aa2be3
feat: Go implementation for manifestYamlDoc and escapeStringJson (#742)
* Builtins for escapeStringJson and manifestYamlDoc

* Benchmark and tests
2024-06-09 19:27:15 +01:00
itchyny
fa70aa4711
feat: implement std.splitLimitR (#745) 2024-06-09 19:25:22 +01:00
Rohit Jangid
2a7260d60c
feat: Add more math functions (#702)
Co-authored-by: Dave Cunningham <sparkprime@gmail.com>
2023-06-13 20:14:26 +01:00
Deep Goel
572c054137
feat: implement std.avg (#700)
* feat: implement std.avg
2023-06-13 17:42:05 +01:00
Rohit Jangid
aece6e9b90
feat: add more crypto functions (#699)
* feat: add more crypto functions
2023-06-13 17:18:02 +01:00
Tejesh Raut
fae841124f
Implement std.trim for string (#684)
* Implement std.trim for string
2023-06-13 13:05:28 +01:00
Deep Goel
7edd5d373b
feat: implement std.maxArray (#696) 2023-05-25 13:45:24 +01:00
Rohit Jangid
4bb6e388b7
feat: implement std.equalsIgnoreCase (#692) 2023-05-25 13:32:39 +01:00
Rohit Jangid
25d3372c98
feat: implement std.remove and std.removeAt (#689)
* feat: implement std.remove and std.removeAt

* Update builtins.go

---------

Co-authored-by: Dave Cunningham <sparkprime@gmail.com>
2023-05-25 13:32:21 +01:00
Rohit Jangid
3c7c0cbac8
feat: implement objectRemoveKey (#686) 2023-05-25 12:55:30 +01:00
Yuki Yugui Sonoda
868d9c6f11
Resolves a false-positive detection of multi-doc YAML streams (#693)
Fixes #673.
2023-05-03 19:37:04 +01:00
Deep Goel
9c0b362ba7
feat: implement std.minArray (#685)
* feat: implement std.minArray
2023-05-03 19:13:56 +01:00
Rohit Jangid
76e4fc2221
feat: implement std.contains (#691) 2023-05-03 19:06:07 +01:00
Rohit Jangid
18377e6f9f
Add std.round function in standard library. (#683)
Co-authored-by: Dave Cunningham <sparkprime@gmail.com>
2023-04-13 15:32:54 +01:00
Tejesh Raut
c63eb61eb3
Implement std.isEmpty for string (#678)
* Implement std.isEmpty for string
2023-04-13 15:24:47 +01:00
Saloni Juneja
91ebf4b09e Add std.xor for 2 booleans 2023-04-13 15:14:10 +01:00
Saloni Juneja
67c8bf4a53 Implement std.xor for booleans 2023-03-28 17:13:31 +01:00
Rohit Jangid
83b9f8341f feat: implement std.sum 2023-02-17 13:19:50 +00:00
Iain Lane
506b37365e
fix: Make lstripChars and rStripChars work with multibyte characters (#663)
We're currently working with the length of the string itself, which is
the number of bytes. That's wrong in these functions which operate at
the level of characters. When applied to a string with a multibyte
character they can return the wrong result. For example, evaluating
`std.rstripChars('• foo\n', '\n')` results in `"• f"`.

What we need to do instead is get the number of runes, and remove one
rune at a time.
2023-01-29 17:44:20 +00:00
Salva Corts
0504998e1e
Fix std.member matching sub strings (#657)
* Fix std.member for strings

* Add test case for matching substr in std.member
2022-11-23 20:42:49 +00:00
Dave Cunningham
c1825dc54d Update to latest CPP version 2022-10-26 16:13:21 +01:00
Jayme Bird
3aab052ecc fix: use rune for length over string 2022-10-25 19:30:01 +01:00
Jayme Bird
59e5a7be3f feat: add builtin stripChars go impl 2022-10-25 16:22:34 +01:00
Jayme Bird
dbe6fa55d8 feat: add builtin rStripChars go impl 2022-10-25 16:22:34 +01:00
Jayme Bird
bb0d8a9d2a feat: add builtin lStripChars go impl 2022-10-25 16:22:34 +01:00
Jayme Bird
c8492297df feat: add builtin member go impl 2022-10-25 16:22:34 +01:00
Jesse
2655afd2bd
memory align structs (#635)
feat: align most structs and add lint rule to enforce struct alignment
2022-10-19 06:19:15 +01:00
Jayme Bird
8abb4aa639 feat: implementation of manifestTomlEx in Go
Co-authored-by: Wojciech Kocjan <wojciech@kocjan.org>
2022-10-19 04:50:41 +01:00
Jesse-Cameron
b82fd4c7f5 fix: calculate the number of elements after we convert to a slice 2022-09-27 21:46:37 +01:00
Jesse-Cameron
e9a59202cf perf: use native builtin for foldl and foldr
benchmark                                 old ns/op      new ns/op      delta
Benchmark_Builtin_substr-8                17914126       16328579       -8.85%
Benchmark_Builtin_reverse-8               360776847      346691957      -3.90%
Benchmark_Builtin_parseInt-8              8686867        8314151        -4.29%
Benchmark_Builtin_base64Decode-8          22157223       21749268       -1.84%
Benchmark_Builtin_base64DecodeBytes-8     281975629      281841745      -0.05%
Benchmark_Builtin_base64-8                23824149       23716470       -0.45%
Benchmark_Builtin_base64_byte_array-8     141846327      141377054      -0.33%
Benchmark_Builtin_manifestJsonEx-8        9317781        9279067        -0.42%
Benchmark_Builtin_comparison-8            128783954      128362069      -0.33%
Benchmark_Builtin_comparison2-8           2082396660     2029123362     -2.56%
Benchmark_Builtin_foldl-8                 159908963      31278937       -80.44%
2022-09-27 21:46:37 +01:00
Jesse-Cameron
c450a168d4 perf: add size hint to map creation
in order to avoid unnecessary hashmap resizing, add size parameters when
creating maps
2022-09-16 18:26:18 +01:00
Jayme Bird
5440cc833f feat: add test data 2022-09-04 15:59:03 +01:00
Jayme Bird
6e180a9368 feat: add go implementation of std.parseInt 2022-09-04 15:59:03 +01:00
Jack Dockerty
9e9321f69a add non-negative check in second parameter for subStr 2022-03-18 02:06:06 +00:00
Greg Roodt
e6a9581713
Adds std.parseYaml (#339)
Add support for std.parseYaml.

Co-authored-by: Stanisław Barzowski <stanislaw.barzowski@gmail.com>
2021-05-20 14:16:23 +02:00
Lucas Servén Marín
5d0f3fb5ed builtins.go: fix builtinManifestJSONEx regression
This commit fixes a regression in std.manifestJsonEx that caused the
standard library function to error when the given value was an array.

Signed-off-by: Lucas Servén Marín <lserven@gmail.com>
2021-02-15 14:44:08 +01:00
Hanyu Cui
f742f2463e
Allow setting a different output stream than os.Stderr for std.trace (#514)
Allow std.trace output to other streams
2021-02-09 14:44:23 +01:00
Hanyu Cui
7d3bda3911
Add std. manifestJsonMinified (#492)
Pick up std changes in cpp and update builtinManifestJSONEx
2020-12-31 11:20:31 +01:00
Stanisław Barzowski
797bfd89ed Better handling of stack traces.
Fixes multiple issues with stack traces leading to missing
stack trace lines. Also, we no longer put builtin context
on the line which *calls* the builtin as if it was a part
of the builtin itself.

Code for stack trace handling was centralized. We no longer
need traceElement argument in ~every function. Now the stack
trace state is kept solely in the interpreter.
2020-11-21 15:36:05 +01:00
Stanisław Barzowski
fe28095772 Support for array comparison 2020-10-30 20:42:46 +01:00
Stanisław Barzowski
570101d43c Desugar locals in object comprehension.
Desugar the locals in object comprehensions
"traditionally" instead of handling them manually.

Object comprehensions allow the locals to depend
on the index variable which means that they are separate
for each field. It doesn't make sense to treat them as
a property of the whole object.

Fixes #358.
2020-10-22 14:54:50 +02:00
Stanisław Barzowski
8fcbda5b50 Linter 2020-08-13 21:06:34 +02:00
Stanisław Barzowski
a1f3af26dc Support strings in the builtin std.flatMap.
Fixes #438.
2020-07-28 19:50:22 +02:00
Benoit Gagnon
0856640baf Fix std.manifestJsonEx parameter names
documented as "value" and "indent" in the standard library reference
2020-07-17 21:36:14 +02:00
Weston McNamee
d767ab7bf5 fix(stdlib): 🐛 manifestJsonEx was escaping HTML
minimal repo: https://play.golang.org/p/GQBNMe6k_nm

https://stackoverflow.com/questions/28595664/how-to-stop-json-marshal-from-escaping-and\nminimal

fix proof: https://play.golang.org/p/SJM3KLkYW-

resolves #423
2020-06-13 21:59:31 +02:00