span_id.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright 2022 The OpenZipkin Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package model
  15. import (
  16. "fmt"
  17. "strconv"
  18. )
  19. // ID type
  20. type ID uint64
  21. // String outputs the 64-bit ID as hex string.
  22. func (i ID) String() string {
  23. return fmt.Sprintf("%016x", uint64(i))
  24. }
  25. // MarshalJSON serializes an ID type (SpanID, ParentSpanID) to HEX.
  26. func (i ID) MarshalJSON() ([]byte, error) {
  27. return []byte(fmt.Sprintf("%q", i.String())), nil
  28. }
  29. // UnmarshalJSON deserializes an ID type (SpanID, ParentSpanID) from HEX.
  30. func (i *ID) UnmarshalJSON(b []byte) (err error) {
  31. var id uint64
  32. if len(b) < 3 {
  33. return nil
  34. }
  35. id, err = strconv.ParseUint(string(b[1:len(b)-1]), 16, 64)
  36. *i = ID(id)
  37. return err
  38. }