MCPcopy Create free account
hub / github.com/dgraph-io/dgraph / valAndValType

Function valAndValType

types/facets/utils.go:75–117  ·  view source on GitHub ↗

valAndValType returns interface val and valtype for facet.

(val string)

Source from the content-addressed store, hash-verified

73
74// valAndValType returns interface val and valtype for facet.
75func valAndValType(val string) (interface{}, api.Facet_ValType, error) {
76 if len(val) == 0 { // empty string case
77 return "", api.Facet_STRING, nil
78 }
79 // strings should be in quotes.
80 if len(val) >= 2 && val[0] == '"' && val[len(val)-1] == '"' {
81 uq, err := strconv.Unquote(val)
82 return uq, api.Facet_STRING, errors.Wrapf(err, "could not unquote %q:", val)
83 }
84 if intVal, err := strconv.ParseInt(val, 0, 64); err == nil {
85 return intVal, api.Facet_INT, nil
86 } else if numErr := err.(*strconv.NumError); numErr.Err == strconv.ErrRange {
87 // if we have only digits in val, then val is a big integer : return error
88 // otherwise try to parse as float.
89 allNumChars := true
90 for _, v := range val {
91 if !unicode.IsDigit(v) {
92 allNumChars = false
93 break
94 }
95 }
96 if allNumChars {
97 return nil, api.Facet_INT, err
98 }
99 }
100 if floatVal, err := strconv.ParseFloat(val, 64); err == nil {
101 // We can't store NaN as it is because it serializes into invalid JSON.
102 if math.IsNaN(floatVal) {
103 return nil, api.Facet_FLOAT, errors.Errorf("Got invalid value: NaN")
104 }
105
106 return floatVal, api.Facet_FLOAT, nil
107 } else if numErr := err.(*strconv.NumError); numErr.Err == strconv.ErrRange {
108 return nil, api.Facet_FLOAT, err
109 }
110 if val == "true" || val == "false" {
111 return val == "true", api.Facet_BOOL, nil
112 }
113 if t, err := types.ParseTime(val); err == nil {
114 return t, api.Facet_DATETIME, nil
115 }
116 return nil, api.Facet_STRING, errors.Errorf("Could not parse the facet value : [%s]", val)
117}
118
119// FacetFor returns Facet for given key and val.
120func FacetFor(key, val string) (*api.Facet, error) {

Callers 1

FacetForFunction · 0.85

Calls 2

ParseTimeFunction · 0.92
ErrorfMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…