pointer.go 916 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. package key
  5. import (
  6. "fmt"
  7. )
  8. // Pointer is a pointer to a path.
  9. type Pointer interface {
  10. Pointer() Path
  11. }
  12. // NewPointer creates a new pointer to a path.
  13. func NewPointer(path Path) Pointer {
  14. return pointer(path)
  15. }
  16. // This is the type returned by pointerKey.Key. Returning this is a
  17. // lot faster than having pointerKey implement Pointer, since it is
  18. // a compositeKey and thus would require reconstructing a Path from
  19. // []interface{} any time the Pointer method is called.
  20. type pointer Path
  21. func (ptr pointer) Pointer() Path {
  22. return Path(ptr)
  23. }
  24. func (ptr pointer) String() string {
  25. return "{" + ptr.Pointer().String() + "}"
  26. }
  27. func (ptr pointer) MarshalJSON() ([]byte, error) {
  28. return []byte(fmt.Sprintf(`{"_ptr":%q}`, ptr.Pointer().String())), nil
  29. }