-
Notifications
You must be signed in to change notification settings - Fork 28
/
entry.go
68 lines (57 loc) · 2.02 KB
/
entry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2016 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package vomtest
import (
"fmt"
"reflect"
"v.io/v23/vom"
)
// Entry represents a test entry, which contains a value and hex bytes. The hex
// bytes represent the golden vom encoding of the value. Encoding tests encode
// the value and expect to get the hex bytes, while decoding tests decode the
// hex bytes and expect to get the value.
type Entry struct {
Label string // Label describes the entry
ValueLabel string // ValueLabel describes the Value
Value reflect.Value // Value for vom test
Version vom.Version // Version of encoding
HexType string // Hex bytes representing the type message(s).
HexValue string // Hex bytes representing the value message.
}
// Name returns the name of the entry, which combines the entry and value
// labels.
func (e Entry) Name() string {
return e.Label + " " + e.ValueLabel
}
// HexVersion returns the version as hex bytes.
func (e Entry) HexVersion() string {
return fmt.Sprintf("%x", byte(e.Version))
}
// Hex returns the full hex bytes, including the version, types and value.
func (e Entry) Hex() string {
return e.HexVersion() + e.HexType + e.HexValue
}
// Bytes returns the full binary bytes, including the version, types and value.
func (e Entry) Bytes() []byte {
return hexToBytes(e.Hex())
}
// TypeBytes returns the binary bytes including the version and types. Returns
// a nil slice if no type messages are encoded.
func (e Entry) TypeBytes() []byte {
if e.HexType == "" {
return nil
}
return hexToBytes(e.HexVersion() + e.HexType)
}
// ValueBytes returns the binary bytes including the version and value.
func (e Entry) ValueBytes() []byte {
return hexToBytes(e.HexVersion() + e.HexValue)
}
func hexToBytes(hex string) []byte {
var b []byte
if _, err := fmt.Sscanf(hex, "%x", &b); err != nil {
panic(fmt.Errorf("hex %s isn't valid: %v", hex, err))
}
return b
}