| 479 | } |
| 480 | |
| 481 | func 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) |