MCPcopy
hub / github.com/dgraph-io/dgraph / checkValueType

Function checkValueType

dql/parser.go:370–421  ·  view source on GitHub ↗
(vm varMap)

Source from the content-addressed store, hash-verified

368}
369
370func checkValueType(vm varMap) error {
371 for k, v := range vm {
372 typ := v.Type
373
374 if len(typ) == 0 {
375 return errors.Errorf("Type of variable %v not specified", k)
376 }
377
378 // Ensure value is not nil if the variable is required.
379 if typ[len(typ)-1] == '!' {
380 if v.Value == "" {
381 return errors.Errorf("Variable %v should be initialised", k)
382 }
383 typ = typ[:len(typ)-1]
384 }
385
386 // Type check the values.
387 if v.Value != "" {
388 switch typ {
389 case "int":
390 {
391 if _, err := strconv.ParseInt(v.Value, 0, 64); err != nil {
392 return errors.Wrapf(err, "Expected an int but got %v", v.Value)
393 }
394 }
395 case "float":
396 {
397 if _, err := strconv.ParseFloat(v.Value, 64); err != nil {
398 return errors.Wrapf(err, "Expected a float but got %v", v.Value)
399 }
400 }
401 case "bool":
402 {
403 if _, err := strconv.ParseBool(v.Value); err != nil {
404 return errors.Wrapf(err, "Expected a bool but got %v", v.Value)
405 }
406 }
407 case "float32vector":
408 {
409 if _, err := types.ParseVFloat(v.Value); err != nil {
410 return errors.Wrapf(err, "Expected a vector32float but got %v", v.Value)
411 }
412 }
413 case "string": // Value is a valid string. No checks required.
414 default:
415 return errors.Errorf("Type %q not supported", typ)
416 }
417 }
418 }
419
420 return nil
421}
422
423func substituteVar(f string, res *string, vmap varMap) error {
424 if len(f) > 0 && f[0] == '$' {

Callers 1

getVariablesAndQueryFunction · 0.85

Calls 2

ParseVFloatFunction · 0.92
ErrorfMethod · 0.45

Tested by

no test coverage detected