Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/release-build.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
name: Release Build
on:
release:
types: [created]
types: [published]
workflow_dispatch:

jobs:
releases-matrix:
name: Release Go Binary
Expand Down
4 changes: 2 additions & 2 deletions test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,11 @@ This implementation would be closer to the `Scalar consensus` as it does not alw
|`$['a','a']`|`{"a":1}`|`[1 1]`|`[1 1]`|:white_check_mark:|
|`$[?(@.key<3),?(@.key>6)]`|`[{"key": 1}, {"key": 8}, {"key": 3}, {"key": 10}, {"key": 7}, {"key": 2}, {"key": 6}, {"key": 4}]`|none|`[]`|:question:|
|`$['key','another']`|`{ "key": "value", "another": "entry" }`|`[value entry]`|`[value entry]`|:white_check_mark:|
|`$['missing','key']`|`{ "key": "value", "another": "entry" }`|`[value]`|`nil`|:no_entry:|
|`$['missing','key']`|`{ "key": "value", "another": "entry" }`|`[value]`|`[value]`|:white_check_mark:|
|`$[:]['c','d']`|`[{"c":"cc1","d":"dd1","e":"ee1"},{"c":"cc2","d":"dd2","e":"ee2"}]`|`[cc1 dd1 cc2 dd2]`|`[[cc1 dd1] [cc2 dd2]]`|:no_entry:|
|`$[0]['c','d']`|`[{"c":"cc1","d":"dd1","e":"ee1"},{"c":"cc2","d":"dd2","e":"ee2"}]`|`[cc1 dd1]`|`[cc1 dd1]`|:white_check_mark:|
|`$.*['c','d']`|`[{"c":"cc1","d":"dd1","e":"ee1"},{"c":"cc2","d":"dd2","e":"ee2"}]`|`[cc1 dd1 cc2 dd2]`|`[[cc1 dd1] [cc2 dd2]]`|:no_entry:|
|`$..['c','d']`|`[{"c":"cc1","d":"dd1","e":"ee1"}, {"c": "cc2", "child": {"d": "dd2"}}, {"c": "cc3"}, {"d": "dd4"}, {"child": {"c": "cc5"}}]`|none|`[cc1 dd1]`|:question:|
|`$..['c','d']`|`[{"c":"cc1","d":"dd1","e":"ee1"}, {"c": "cc2", "child": {"d": "dd2"}}, {"c": "cc3"}, {"d": "dd4"}, {"child": {"c": "cc5"}}]`|none|`[cc1 dd1 cc2 dd2 cc3 dd4 cc5]`|:question:|
|`$[4,1]`|`[1,2,3,4,5]`|`[5 2]`|`[5 2]`|:white_check_mark:|
|`$.*[0,:5]`|`{ "a": [ "string", null, true ], "b": [ false, "string", 5.4 ] }`|none|`nil`|:question:|
|`$[1:3,4]`|`[1,2,3,4,5]`|none|`nil`|:question:|
Expand Down
10 changes: 5 additions & 5 deletions test/union_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ func Test_Union(t *testing.T) {
expectedError: "",
},
{
query: `$['missing','key']`, // TODO : should we error with invald keys?
query: `$['missing','key']`,
data: `{ "key": "value", "another": "entry" }`,
expected: nil,
expected: []interface{}{"value"},
consensus: []interface{}{"value"},
expectedError: "union: invalid token key 'missing' not found",
expectedError: "",
},
{
query: `$[:]['c','d']`,
Expand All @@ -71,7 +71,7 @@ func Test_Union(t *testing.T) {
{
query: `$..['c','d']`,
data: `[{"c":"cc1","d":"dd1","e":"ee1"}, {"c": "cc2", "child": {"d": "dd2"}}, {"c": "cc3"}, {"d": "dd4"}, {"child": {"c": "cc5"}}]`,
expected: []interface{}{"cc1", "dd1"},
expected: []interface{}{"cc1", "dd1", "cc2", "dd2", "cc3", "dd4", "cc5"},
consensus: consensusNone,
expectedError: "",
},
Expand Down Expand Up @@ -113,5 +113,5 @@ func Test_Union(t *testing.T) {
}

batchTest(t, tests)
// printConsensusMatrix(tests)
//printConsensusMatrix(tests)
}
3 changes: 3 additions & 0 deletions token/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ type Options struct {
AllowMapReferenceByIndexInSubscript bool
// AllowStringReferenceByIndexInSubscript allow string characters to be referenced by index in subscript tokens.
AllowStringReferenceByIndexInSubscript bool

// FailUnionOnInvalidIdentifier force union tokens to fail on missing or invalid keys or invalid index.
FailUnionOnInvalidIdentifier bool
}
26 changes: 17 additions & 9 deletions token/union.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,28 @@ import (
func newUnionToken(arguments []interface{}, options *Options) *unionToken {
allowMap := false
allowString := false
failUnionOnInvalidIdentifier := false

if options != nil {
allowMap = options.AllowMapReferenceByIndex || options.AllowMapReferenceByIndexInUnion
allowString = options.AllowStringReferenceByIndex || options.AllowStringReferenceByIndexInUnion

failUnionOnInvalidIdentifier = options.FailUnionOnInvalidIdentifier
}

return &unionToken{
arguments: arguments,
allowMap: allowMap,
allowString: allowString,
arguments: arguments,
allowMap: allowMap,
allowString: allowString,
failUnionOnInvalidIdentifier: failUnionOnInvalidIdentifier,
}
}

type unionToken struct {
arguments []interface{}
allowMap bool
allowString bool
arguments []interface{}
allowMap bool
allowString bool
failUnionOnInvalidIdentifier bool
}

func (token *unionToken) String() string {
Expand Down Expand Up @@ -163,7 +168,7 @@ func (token *unionToken) getUnionByKey(obj interface{}, keys []string) ([]interf
}
}

if len(missingKeys) > 0 {
if token.failUnionOnInvalidIdentifier && len(missingKeys) > 0 {
sort.Strings(missingKeys)
return nil, getInvalidTokenKeyNotFoundError(token.Type(), strings.Join(missingKeys, ","))
}
Expand All @@ -183,7 +188,7 @@ func (token *unionToken) getUnionByKey(obj interface{}, keys []string) ([]interf
}
}

if len(missingKeys) > 0 {
if token.failUnionOnInvalidIdentifier && len(missingKeys) > 0 {
sort.Strings(missingKeys)
return nil, getInvalidTokenKeyNotFoundError(token.Type(), strings.Join(missingKeys, ","))
}
Expand Down Expand Up @@ -267,7 +272,10 @@ func (token *unionToken) getUnionByIndex(obj interface{}, indices []int64) (inte
idx = length + idx
}
if idx < 0 || idx >= length {
return nil, getInvalidTokenOutOfRangeError(token.Type())
if token.failUnionOnInvalidIdentifier {
return nil, getInvalidTokenOutOfRangeError(token.Type())
}
continue
}

if mapKeys != nil {
Expand Down
94 changes: 89 additions & 5 deletions token/union_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ func Test_newUnionToken(t *testing.T) {
allowString: false,
},
},
{
input: input{
options: &Options{
FailUnionOnInvalidIdentifier: true,
},
},
expected: &unionToken{
allowMap: false,
allowString: false,
failUnionOnInvalidIdentifier: true,
},
},
{
input: input{
options: &Options{
Expand Down Expand Up @@ -270,7 +282,7 @@ func Test_UnionToken_Apply(t *testing.T) {
},
},
expected: expected{
err: "union: invalid token out of range",
value: []interface{}{"one", "four"},
},
},
{
Expand Down Expand Up @@ -336,7 +348,7 @@ func Test_UnionToken_Apply(t *testing.T) {
},
},
expected: expected{
err: "union: invalid token key 'e' not found",
value: []interface{}{"one", "four"},
},
},
{
Expand Down Expand Up @@ -491,12 +503,36 @@ func Test_UnionToken_getUnionByIndex(t *testing.T) {
err: "union: invalid token target. expected [array slice] got [int]",
},
},
{
input: input{
token: &unionToken{
failUnionOnInvalidIdentifier: true,
},
obj: []string{"one", "two", "three"},
keys: []int64{4},
},
expected: expected{
err: "union: invalid token out of range",
},
},
{
input: input{
token: &unionToken{},
obj: []string{"one", "two", "three"},
keys: []int64{4},
},
expected: expected{
obj: []interface{}{},
},
},
{
input: input{
token: &unionToken{
failUnionOnInvalidIdentifier: true,
},
obj: []string{"one", "two", "three"},
keys: []int64{-10},
},
expected: expected{
err: "union: invalid token out of range",
},
Expand All @@ -508,7 +544,7 @@ func Test_UnionToken_getUnionByIndex(t *testing.T) {
keys: []int64{-10},
},
expected: expected{
err: "union: invalid token out of range",
obj: []interface{}{},
},
},
{
Expand Down Expand Up @@ -703,7 +739,9 @@ func Test_UnionToken_getUnionByKey(t *testing.T) {
},
{
input: input{
token: &unionToken{},
token: &unionToken{
failUnionOnInvalidIdentifier: true,
},
obj: map[string]interface{}{
"a": "one",
"b": "two",
Expand All @@ -727,12 +765,46 @@ func Test_UnionToken_getUnionByKey(t *testing.T) {
"d": "four",
"e": "five",
},
keys: []string{"a", "b", "c", "f"},
},
expected: expected{
obj: []interface{}{"one", "two", "three"},
},
},
{
input: input{
token: &unionToken{
failUnionOnInvalidIdentifier: true,
},
obj: map[string]interface{}{
"a": "one",
"b": "two",
"c": "three",
"d": "four",
"e": "five",
},
keys: []string{"a", "b", "c", "f", "one", "blah"},
},
expected: expected{
err: "union: invalid token key 'blah,f,one' not found",
},
},
{
input: input{
token: &unionToken{},
obj: map[string]interface{}{
"a": "one",
"b": "two",
"c": "three",
"d": "four",
"e": "five",
},
keys: []string{"a", "b", "c", "f", "one", "blah"},
},
expected: expected{
obj: []interface{}{"one", "two", "three"},
},
},
{
input: input{
token: &unionToken{},
Expand Down Expand Up @@ -764,14 +836,26 @@ func Test_UnionToken_getUnionByKey(t *testing.T) {
},
},
},
{
input: input{
token: &unionToken{
failUnionOnInvalidIdentifier: true,
},
obj: sampleStruct{},
keys: []string{"missing", "gone"},
},
expected: expected{
err: "union: invalid token key 'gone,missing' not found",
},
},
{
input: input{
token: &unionToken{},
obj: sampleStruct{},
keys: []string{"missing", "gone"},
},
expected: expected{
err: "union: invalid token key 'gone,missing' not found",
obj: []interface{}{},
},
},
{
Expand Down