gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

codes.go (2973B)


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