Skip to content

fix: align std.lines with official semantics#1079

Open
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:fix/std-lines-semantics
Open

fix: align std.lines with official semantics#1079
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:fix/std-lines-semantics

Conversation

@He-Pin

@He-Pin He-Pin commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Motivation:
Official Jsonnet documents std.lines(arr) as concatenating an array of strings into a text file with a newline after each string. It is not a line-splitting function for string input.

The native sjsonnet implementation should therefore preserve array-to-text semantics and reject non-array input. This also resolves the behavioral confusion discussed in google/go-jsonnet#885 without treating the upstream issue thread as the specification.

Modification:

  • Rewrite the native std.lines path to iterate over Val.Arr directly with a StringBuilder.
  • Preserve existing std.join-compatible handling for null array elements.
  • Add regression coverage for array input, null elements, and string-input rejection.

Result:
std.lines(["a", "b"]) follows the documented text-file construction behavior, including the trailing newline. std.lines("a\nb\n") remains a type error because the documented parameter is an array. The table below includes the old sjsonnet behavior from master before this PR.

Case Official meaning C++ jsonnet / go-jsonnet jrsonnet sjsonnet before this PR sjsonnet after this PR
std.lines(["a", "b"]) array-to-text with trailing newlines "a\nb\n" "a\nb\n" "a\nb\n" "a\nb\n"
std.lines(["a", null, "b"]) preserve existing null-skipping join behavior "a\nb\n" "a\nb\n" "a\nb\n" "a\nb\n"
std.lines("a\nb\n") invalid input type type error type error [std.lines] Wrong parameter type: expected Array, got string type error

The observed external behavior on master already matches the documented cases above; this PR keeps that behavior explicit with focused native-path tests and implementation cleanup.

References:

Motivation:

go-jsonnet issue databricks#885 exposed confusion around std.lines: official Jsonnet treats it as array-to-text, not string-to-array. sjsonnet already rejected string input, but the native implementation materialized arrays unnecessarily and lacked a focused regression for this edge.

Modification:

Rewrite std.lines to build the newline-terminated string directly from Val.Arr while preserving null-skipping behavior. Add compatibility coverage and a golden error case for string input.

Result:

std.lines keeps the official array-to-text behavior, rejects string input, and avoids an unnecessary ujson materialization pass.

References:

google/go-jsonnet#885
@He-Pin He-Pin marked this pull request as draft July 4, 2026 02:05
@He-Pin He-Pin marked this pull request as ready for review July 4, 2026 02:43
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.

1 participant