github.com/aristanetworks/goarista@v0.0.0-20240514173732-cca2755bbd44/key/pointer.go (about) 1 // Copyright (c) 2018 Arista Networks, Inc. 2 // Use of this source code is governed by the Apache License 2.0 3 // that can be found in the COPYING file. 4 5 package key 6 7 import ( 8 "fmt" 9 ) 10 11 // Pointer is a pointer to a path. 12 type Pointer interface { 13 Pointer() Path 14 } 15 16 // NewPointer creates a new pointer to a path. 17 func NewPointer(path Path) Pointer { 18 return pointer(path) 19 } 20 21 // This is the type returned by pointerKey.Key. Returning this is a 22 // lot faster than having pointerKey implement Pointer, since it is 23 // a sliceKey and thus would require reconstructing a Path from 24 // []interface{} any time the Pointer method is called. 25 type pointer Path 26 27 func (ptr pointer) Pointer() Path { 28 return Path(ptr) 29 } 30 31 func (ptr pointer) String() string { 32 return "{" + ptr.Pointer().String() + "}" 33 } 34 35 func (ptr pointer) MarshalJSON() ([]byte, error) { 36 return []byte(fmt.Sprintf(`{"_ptr":%q}`, ptr.Pointer().String())), nil 37 } 38 39 func (ptr pointer) Equal(other interface{}) bool { 40 o, ok := other.(Pointer) 41 return ok && pointerEqual(ptr, o) 42 } 43 44 func pointerEqual(a, b Pointer) bool { 45 return pathEqual(a.Pointer(), b.Pointer()) 46 }