codes.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright The OpenTelemetry 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 codes // import "go.opentelemetry.io/otel/codes"
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "strconv"
  19. )
  20. const (
  21. // Unset is the default status code.
  22. Unset Code = 0
  23. // Error indicates the operation contains an error.
  24. Error Code = 1
  25. // Ok indicates operation has been validated by an Application developers
  26. // or Operator to have completed successfully, or contain no error.
  27. Ok Code = 2
  28. maxCode = 3
  29. )
  30. // Code is an 32-bit representation of a status state.
  31. type Code uint32
  32. var codeToStr = map[Code]string{
  33. Unset: "Unset",
  34. Error: "Error",
  35. Ok: "Ok",
  36. }
  37. var strToCode = map[string]Code{
  38. `"Unset"`: Unset,
  39. `"Error"`: Error,
  40. `"Ok"`: Ok,
  41. }
  42. // String returns the Code as a string.
  43. func (c Code) String() string {
  44. return codeToStr[c]
  45. }
  46. // UnmarshalJSON unmarshals b into the Code.
  47. //
  48. // This is based on the functionality in the gRPC codes package:
  49. // https://github.com/grpc/grpc-go/blob/bb64fee312b46ebee26be43364a7a966033521b1/codes/codes.go#L218-L244
  50. func (c *Code) UnmarshalJSON(b []byte) error {
  51. // From json.Unmarshaler: By convention, to approximate the behavior of
  52. // Unmarshal itself, Unmarshalers implement UnmarshalJSON([]byte("null")) as
  53. // a no-op.
  54. if string(b) == "null" {
  55. return nil
  56. }
  57. if c == nil {
  58. return fmt.Errorf("nil receiver passed to UnmarshalJSON")
  59. }
  60. var x interface{}
  61. if err := json.Unmarshal(b, &x); err != nil {
  62. return err
  63. }
  64. switch x.(type) {
  65. case string:
  66. if jc, ok := strToCode[string(b)]; ok {
  67. *c = jc
  68. return nil
  69. }
  70. return fmt.Errorf("invalid code: %q", string(b))
  71. case float64:
  72. if ci, err := strconv.ParseUint(string(b), 10, 32); err == nil {
  73. if ci >= maxCode {
  74. return fmt.Errorf("invalid code: %q", ci)
  75. }
  76. *c = Code(ci)
  77. return nil
  78. }
  79. return fmt.Errorf("invalid code: %q", string(b))
  80. default:
  81. return fmt.Errorf("invalid code: %q", string(b))
  82. }
  83. }
  84. // MarshalJSON returns c as the JSON encoding of c.
  85. func (c *Code) MarshalJSON() ([]byte, error) {
  86. if c == nil {
  87. return []byte("null"), nil
  88. }
  89. str, ok := codeToStr[*c]
  90. if !ok {
  91. return nil, fmt.Errorf("invalid code: %d", *c)
  92. }
  93. return []byte(fmt.Sprintf("%q", str)), nil
  94. }