Align string member empty needle semantics#1084
Open
He-Pin wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
std.memberaccepts either an array or a string as its first argument. For strings, the official Jsonnet stdlib defines membership throughstd.findSubstr(x, arr), andstd.findSubstr("", "abc")returns an empty array. That means an empty string should not be considered a string member.sjsonnet's native implementation delegated directly to JVM
String.contains, which treats the empty string as contained in every string. This madestd.member("abc", "")andstd.member("", "")returntrue, even though the official stdlib-derived semantics returnfalse.Modification
std.memberto be non-empty before callingString.contains.std.member([""], "")remainstrue.Result
std.member("abc", "")std.member("", "")std.member([""], "")std.findSubstr("", "abc")falsefalsetrue[]truetruetrue[]falsefalsetrue[]truetruetrue[]falsefalsetrue[]sjsonnet now matches cpp-jsonnet and jrsonnet for string membership with an empty needle, while preserving normal substring membership and array element membership.
References
std.member: https://jsonnet.org/ref/stdlib.html#std-memberstd.findSubstr: https://jsonnet.org/ref/stdlib.html#std-findSubstrmemberdefinition: https://github.com/google/jsonnet/blob/master/stdlib/std.jsonnetmemberimplementation: https://github.com/google/go-jsonnet/blob/master/builtins.go