Added null

This commit is contained in:
Mike Farah 2020-10-20 15:40:11 +11:00
parent 4f574efdc4
commit 49615f5581
4 changed files with 31 additions and 0 deletions

View File

@ -72,6 +72,8 @@ func CreateValueOperation(value interface{}, stringValue string) *Operation {
node.Tag = "!!bool" node.Tag = "!!bool"
case string: case string:
node.Tag = "!!str" node.Tag = "!!str"
case nil:
node.Tag = "!!null"
} }
return &Operation{ return &Operation{

View File

@ -35,6 +35,12 @@ var selectOperatorScenarios = []expressionScenario{
"D0, P[a things], (!!map)::{include: true}\n", "D0, P[a things], (!!map)::{include: true}\n",
"D0, P[a andMe], (!!map)::{include: fold}\n", "D0, P[a andMe], (!!map)::{include: fold}\n",
}, },
}, {
document: `[cat,~,dog]`,
expression: `.[] | select(. == ~)`,
expected: []string{
"D0, P[1], (!!null)::~\n",
},
}, },
} }

View File

@ -67,6 +67,20 @@ var valueOperatorScenarios = []expressionScenario{
"D0, P[], (!!bool)::false\n", "D0, P[], (!!bool)::false\n",
}, },
}, },
{
document: ``,
expression: `Null`,
expected: []string{
"D0, P[], (!!null)::Null\n",
},
},
{
document: ``,
expression: `~`,
expected: []string{
"D0, P[], (!!null)::~\n",
},
},
} }
func TestValueOperatorScenarios(t *testing.T) { func TestValueOperatorScenarios(t *testing.T) {

View File

@ -151,6 +151,12 @@ func stringValue(wrapped bool) lex.Action {
} }
} }
func nullValue() lex.Action {
return func(s *lex.Scanner, m *machines.Match) (interface{}, error) {
return &Token{TokenType: OperationToken, Operation: CreateValueOperation(nil, string(m.Bytes))}, nil
}
}
func selfToken() lex.Action { func selfToken() lex.Action {
return func(s *lex.Scanner, m *machines.Match) (interface{}, error) { return func(s *lex.Scanner, m *machines.Match) (interface{}, error) {
op := &Operation{OperationType: SelfReference} op := &Operation{OperationType: SelfReference}
@ -200,6 +206,9 @@ func initLexer() (*lex.Lexer, error) {
lexer.Add([]byte(`[Tt][Rr][Uu][Ee]`), booleanValue(true)) lexer.Add([]byte(`[Tt][Rr][Uu][Ee]`), booleanValue(true))
lexer.Add([]byte(`[Ff][Aa][Ll][Ss][Ee]`), booleanValue(false)) lexer.Add([]byte(`[Ff][Aa][Ll][Ss][Ee]`), booleanValue(false))
lexer.Add([]byte(`[Nn][Uu][Ll][Ll]`), nullValue())
lexer.Add([]byte(`~`), nullValue())
lexer.Add([]byte(`"[^ "]+"`), stringValue(true)) lexer.Add([]byte(`"[^ "]+"`), stringValue(true))
lexer.Add([]byte(`\[`), literalToken(OpenCollect, false)) lexer.Add([]byte(`\[`), literalToken(OpenCollect, false))