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

Function Marshal

types/conversion.go:481–634  ·  view source on GitHub ↗
(from Val, to *Val)

Source from the content-addressed store, hash-verified

479}
480
481func Marshal(from Val, to *Val) error {
482 if to == nil {
483 return errors.Errorf("invalid conversion %s to nil", from.Tid.Name())
484 }
485
486 fromID := from.Tid
487 toID := to.Tid
488 val := from.Value
489 res := &to.Value
490
491 // This is a default value from sg.fillVars, don't convert it's empty.
492 // Fixes issue #2980.
493 if val == nil {
494 *to = ValueForType(toID)
495 return nil
496 }
497
498 switch fromID {
499 case BinaryID:
500 vc := val.([]byte)
501 switch toID {
502 case StringID, DefaultID:
503 *res = string(vc)
504 case BinaryID:
505 *res = vc
506 default:
507 return cantConvert(fromID, toID)
508 }
509 case StringID, DefaultID:
510 vc := val.(string)
511 switch toID {
512 case StringID, DefaultID:
513 *res = vc
514 case BinaryID:
515 *res = []byte(vc)
516 default:
517 return cantConvert(fromID, toID)
518 }
519 case IntID:
520 vc := val.(int64)
521 switch toID {
522 case StringID, DefaultID:
523 *res = strconv.FormatInt(vc, 10)
524 case BinaryID:
525 var bs [8]byte
526 binary.LittleEndian.PutUint64(bs[:], uint64(vc))
527 *res = bs[:]
528 default:
529 return cantConvert(fromID, toID)
530 }
531 case FloatID:
532 vc := val.(float64)
533 switch toID {
534 case StringID, DefaultID:
535 *res = strconv.FormatFloat(vc, 'G', -1, 64)
536 case BinaryID:
537 var bs [8]byte
538 u := math.Float64bits(vc)

Callers 13

ToBinaryFunction · 0.92
byteValFunction · 0.92
getTypeValFunction · 0.92
facetToStringFunction · 0.92
convertToTypeFunction · 0.92
ValidateAndConvertFunction · 0.92
replaceVarInFuncMethod · 0.92
addValueMethod · 0.92
ValueMarshalledMethod · 0.92
rebuildTokIndexFunction · 0.92
toStringFunction · 0.85
toBinaryFunction · 0.85

Calls 8

ValueForTypeFunction · 0.85
cantConvertFunction · 0.85
FloatArrayAsBytesFunction · 0.85
FloatArrayAsStringFunction · 0.85
NameMethod · 0.65
ErrorfMethod · 0.45
StringMethod · 0.45
ErrorMethod · 0.45

Tested by 2

toStringFunction · 0.68
TestParseFunction · 0.68