github.com/interconnectedcloud/qdr-operator@v0.0.0-20210826174505-576d2b33dac7/test/e2e/framework/qdrmanagement/entities/common/direction.go (about)

     1  package common
     2  
     3  import (
     4  	"encoding/json"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  type DirectionType int
    10  
    11  const (
    12  	DirectionTypeIn DirectionType = iota
    13  	DirectionTypeOut
    14  )
    15  
    16  // UnmarshalJSON returns the appropriate DirectionType for parsed string
    17  func (d *DirectionType) UnmarshalJSON(b []byte) error {
    18  	var s string
    19  
    20  	if len(b) == 0 {
    21  		return nil
    22  	}
    23  	if b[0] != '"' {
    24  		b = []byte(strconv.Quote(string(b)))
    25  	}
    26  	if err := json.Unmarshal(b, &s); err != nil {
    27  		return err
    28  	}
    29  	switch strings.ToLower(s) {
    30  	case "in":
    31  		*d = DirectionTypeIn
    32  	case "out":
    33  		*d = DirectionTypeOut
    34  	}
    35  	return nil
    36  }
    37  
    38  // MarshalJSON returns the string representation of DirectionType
    39  func (d DirectionType) MarshalJSON() ([]byte, error) {
    40  	var s string
    41  	switch d {
    42  	case DirectionTypeIn:
    43  		s = "in"
    44  	case DirectionTypeOut:
    45  		s = "out"
    46  	}
    47  	return json.Marshal(s)
    48  }