Skip to content

Commit

Permalink
some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
neelance committed May 23, 2017
1 parent 969dab9 commit 3d63ae8
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 58 deletions.
5 changes: 0 additions & 5 deletions internal/common/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ type Lexer struct {
descComment string
}

type BasicLit struct {
Type rune
Text string
}

type Ident struct {
Name string
Loc errors.Location
Expand Down
46 changes: 46 additions & 0 deletions internal/common/literals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package common

import (
"strconv"
"text/scanner"
)

type Literal interface {
Value() interface{}
}

type BasicLit struct {
Type rune
Text string
}

func (lit *BasicLit) Value() interface{} {
switch lit.Type {
case scanner.Int, scanner.Float:
value, err := strconv.ParseFloat(lit.Text, 64)
if err != nil {
panic(err)
}
return value

case scanner.String:
value, err := strconv.Unquote(lit.Text)
if err != nil {
panic(err)
}
return value

case scanner.Ident:
switch lit.Text {
case "true":
return true
case "false":
return false
default:
return lit.Text
}

default:
panic("invalid literal")
}
}
32 changes: 0 additions & 32 deletions internal/common/values.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package common

import (
"strconv"
"text/scanner"

"github.com/neelance/graphql-go/errors"
Expand Down Expand Up @@ -125,34 +124,3 @@ func parseValue(l *Lexer, constOnly bool) interface{} {
panic("unreachable")
}
}

func UnmarshalLiteral(lit *BasicLit) interface{} {
switch lit.Type {
case scanner.Int, scanner.Float:
value, err := strconv.ParseFloat(lit.Text, 64)
if err != nil {
panic(err)
}
return value

case scanner.String:
value, err := strconv.Unquote(lit.Text)
if err != nil {
panic(err)
}
return value

case scanner.Ident:
switch lit.Text {
case "true":
return true
case "false":
return false
default:
return lit.Text
}

default:
panic("invalid literal")
}
}
8 changes: 4 additions & 4 deletions internal/exec/resolvable/packer.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ func (p *ValuePacker) Pack(r *Request, value interface{}) (reflect.Value, error)
return reflect.Value{}, errors.Errorf("got null for non-null")
}

if lit, ok := value.(*common.BasicLit); ok {
value = common.UnmarshalLiteral(lit)
if lit, ok := value.(common.Literal); ok {
value = lit.Value()
}

coerced, err := unmarshalInput(p.ValueType, value)
Expand All @@ -281,8 +281,8 @@ func (p *unmarshalerPacker) Pack(r *Request, value interface{}) (reflect.Value,
return reflect.Value{}, errors.Errorf("got null for non-null")
}

if lit, ok := value.(*common.BasicLit); ok {
value = common.UnmarshalLiteral(lit)
if lit, ok := value.(common.Literal); ok {
value = lit.Value()
}

v := reflect.New(p.ValueType)
Expand Down
26 changes: 11 additions & 15 deletions internal/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,35 +410,31 @@ func validateValue(v interface{}, t common.Type) (bool, string) {
return true, ""
}

if l, ok := t.(*common.List); ok {
if _, ok := v.([]interface{}); !ok {
return validateValue(v, l.OfType)
}
}

if _, ok := v.(common.Variable); ok {
// TODO
return true, ""
}

if v, ok := v.(*common.BasicLit); ok {
if validateLiteral(v, t) {
return true, ""
switch t := t.(type) {
case *schema.Scalar, *schema.Enum:
if lit, ok := v.(*common.BasicLit); ok {
if validateBasicLit(lit, t) {
return true, ""
}
}
}

switch t := t.(type) {
case *common.List:
v, ok := v.([]interface{})
list, ok := v.([]interface{})
if !ok {
return false, fmt.Sprintf("Expected %q, found not a list.", t)
return validateValue(v, t.OfType) // single value instead of list
}
for i, entry := range v {
for i, entry := range list {
if ok, reason := validateValue(entry, t.OfType); !ok {
return false, fmt.Sprintf("In element #%d: %s", i, reason)
}
}
return true, ""

case *schema.InputObject:
v, ok := v.(map[string]interface{})
if !ok {
Expand Down Expand Up @@ -466,7 +462,7 @@ func validateValue(v interface{}, t common.Type) (bool, string) {
return false, fmt.Sprintf("Expected type %q, found %s.", t, common.Stringify(v))
}

func validateLiteral(v *common.BasicLit, t common.Type) bool {
func validateBasicLit(v *common.BasicLit, t common.Type) bool {
switch t := t.(type) {
case *schema.Scalar:
switch t.Name {
Expand Down
4 changes: 2 additions & 2 deletions introspection/introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (r *Field) DeprecationReason() *string {
if d == nil {
return nil
}
reason := common.UnmarshalLiteral(d.Args.MustGet("reason").Value.(*common.BasicLit)).(string)
reason := d.Args.MustGet("reason").Value.(*common.BasicLit).Value().(string)
return &reason
}

Expand Down Expand Up @@ -281,7 +281,7 @@ func (r *EnumValue) DeprecationReason() *string {
if d == nil {
return nil
}
reason := common.UnmarshalLiteral(d.Args.MustGet("reason").Value.(*common.BasicLit)).(string)
reason := d.Args.MustGet("reason").Value.(*common.BasicLit).Value().(string)
return &reason
}

Expand Down

0 comments on commit 3d63ae8

Please sign in to comment.