Skip to content

Commit

Permalink
Add NullValue to allow sending null in a value literal
Browse files Browse the repository at this point in the history
  • Loading branch information
clery committed Mar 10, 2019
1 parent c13c47a commit 1967934
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
29 changes: 29 additions & 0 deletions language/ast/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var _ Value = (*BooleanValue)(nil)
var _ Value = (*EnumValue)(nil)
var _ Value = (*ListValue)(nil)
var _ Value = (*ObjectValue)(nil)
var _ Value = (*NullValue)(nil)

// Variable implements Node, Value
type Variable struct {
Expand Down Expand Up @@ -300,3 +301,31 @@ func (f *ObjectField) GetLoc() *Location {
func (f *ObjectField) GetValue() interface{} {
return f.Value
}

// NullField implements Node, Value
type NullValue struct {
Kind string
Loc *Location
}

func NewNullValue(v *NullValue) *NullValue {
if v == nil {
v = &NullValue{}
}
return &NullValue{
Kind: kinds.NullValue,
Loc: v.Loc,
}
}

func (v *NullValue) GetKind() string {
return v.Kind
}

func (v *NullValue) GetLoc() *Location {
return v.Loc
}

func (v *NullValue) GetValue() interface{} {
return nil
}
1 change: 1 addition & 0 deletions language/kinds/kinds.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
ListValue = "ListValue"
ObjectValue = "ObjectValue"
ObjectField = "ObjectField"
NullValue = "NullValue"

// Directives
Directive = "Directive"
Expand Down
14 changes: 10 additions & 4 deletions language/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -610,15 +610,21 @@ func parseValueLiteral(parser *Parser, isConst bool) (ast.Value, error) {
Value: value,
Loc: loc(parser, token.Start),
}), nil
} else if token.Value != "null" {
} else if token.Value == "null" {
if err := advance(parser); err != nil {
return nil, err
}
return ast.NewEnumValue(&ast.EnumValue{
Value: token.Value,
Loc: loc(parser, token.Start),
return ast.NewNullValue(&ast.NullValue{
Loc: loc(parser, token.Start),
}), nil
}
if err := advance(parser); err != nil {
return nil, err
}
return ast.NewEnumValue(&ast.EnumValue{
Value: token.Value,
Loc: loc(parser, token.Start),
}), nil
case lexer.DOLLAR:
if !isConst {
return parseVariable(parser)
Expand Down

0 comments on commit 1967934

Please sign in to comment.