This repository has been archived by the owner on Jan 4, 2018. It is now read-only.
forked from glycerine/go-capnproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointer.go
59 lines (50 loc) · 1.43 KB
/
pointer.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
package capnp
// A Pointer is a reference to a Cap'n Proto struct, list, or interface.
type Pointer interface {
// Segment returns the segment this pointer points into.
// If nil, then this is an invalid pointer.
Segment() *Segment
// HasData reports whether the object referenced by the pointer has
// non-zero size.
HasData() bool
// value converts the pointer into a raw value.
value(paddr Address) rawPointer
// underlying returns a Pointer that is one of a Struct, a List, or an
// Interface.
underlying() Pointer
}
// IsValid reports whether p is non-nil and valid.
func IsValid(p Pointer) bool {
return p != nil && p.Segment() != nil
}
// HasData returns true if the pointer is valid and has non-zero size.
func HasData(p Pointer) bool {
return IsValid(p) && p.HasData()
}
// PointerDefault returns p if it is valid, otherwise it unmarshals def.
func PointerDefault(p Pointer, def []byte) (Pointer, error) {
if !IsValid(p) {
return unmarshalDefault(def)
}
return p, nil
}
func unmarshalDefault(def []byte) (Pointer, error) {
msg, err := Unmarshal(def)
if err != nil {
return nil, err
}
p, err := msg.Root()
if err != nil {
return nil, err
}
return p, nil
}
// pointerAddress returns the pointer's address.
// It panics if p's underlying pointer is not a valid Struct or List.
func pointerAddress(p Pointer) Address {
type addresser interface {
Address() Address
}
a := p.underlying().(addresser)
return a.Address()
}