Skip to content

Commit

Permalink
Add unmarshaling code and tests
Browse files Browse the repository at this point in the history
Signed-off-by: volmedo <[email protected]>
  • Loading branch information
volmedo committed Oct 21, 2019
1 parent a8850d6 commit 0da9d6d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 22 deletions.
49 changes: 49 additions & 0 deletions pkg/apis/jaegertracing/v1/deployment_strategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package v1

import (
"errors"
"strings"
)

// DeploymentStrategy represents the possible values for deployment strategies
// +k8s:openapi-gen=true
type DeploymentStrategy string

const (
// DeploymentStrategyDeprecatedAllInOne represents the (deprecated) 'all-in-one' deployment strategy
// +k8s:openapi-gen=true
DeploymentStrategyDeprecatedAllInOne DeploymentStrategy = "all-in-one"

// DeploymentStrategyAllInOne represents the 'allInOne' deployment strategy (default)
// +k8s:openapi-gen=true
DeploymentStrategyAllInOne DeploymentStrategy = "allinone"

// DeploymentStrategyStreaming represents the 'streaming' deployment strategy
// +k8s:openapi-gen=true
DeploymentStrategyStreaming DeploymentStrategy = "streaming"

// DeploymentStrategyProduction represents the 'production' deployment strategy
// +k8s:openapi-gen=true
DeploymentStrategyProduction DeploymentStrategy = "production"
)

// UnmarshalText implements encoding.TextUnmarshaler to ensure that JSON strings
// in jaeger specs values are interpreted in a case-insensitive manner
func (ds *DeploymentStrategy) UnmarshalText(text []byte) error {
if ds == nil {
return errors.New("DeploymentStrategy: UnmarshalText on nil pointer")
}

switch strings.ToLower(string(text)) {
default:
*ds = DeploymentStrategyAllInOne
case string(DeploymentStrategyDeprecatedAllInOne):
*ds = DeploymentStrategyDeprecatedAllInOne
case string(DeploymentStrategyStreaming):
*ds = DeploymentStrategyStreaming
case string(DeploymentStrategyProduction):
*ds = DeploymentStrategyProduction
}

return nil
}
35 changes: 35 additions & 0 deletions pkg/apis/jaegertracing/v1/deployment_strategy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package v1

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/assert"
)

func TestUnmarshalJSON(t *testing.T) {
tcs := map[string]struct {
json string
expected DeploymentStrategy
}{
"allInOne": {json: `"allInOne"`, expected: DeploymentStrategyAllInOne},
"streaming": {json: `"streaming"`, expected: DeploymentStrategyStreaming},
"production": {json: `"production"`, expected: DeploymentStrategyProduction},
"all-in-one": {json: `"all-in-one"`, expected: DeploymentStrategyDeprecatedAllInOne},
"ALLinONE": {json: `"ALLinONE"`, expected: DeploymentStrategyAllInOne},
"StReAmInG": {json: `"StReAmInG"`, expected: DeploymentStrategyStreaming},
"Production": {json: `"Production"`, expected: DeploymentStrategyProduction},
"All-IN-One": {json: `"All-IN-One"`, expected: DeploymentStrategyDeprecatedAllInOne},
"random value": {json: `"random value"`, expected: DeploymentStrategyAllInOne},
"empty string": {json: `""`, expected: DeploymentStrategyAllInOne},
}

for name, tc := range tcs {
t.Run(name, func(t *testing.T) {
ds := DeploymentStrategy("")
err := json.Unmarshal([]byte(tc.json), &ds)
assert.NoError(t, err)
assert.Equal(t, tc.expected, ds)
})
}
}
22 changes: 0 additions & 22 deletions pkg/apis/jaegertracing/v1/jaeger_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,6 @@ const (
IngressSecurityOAuthProxy IngressSecurityType = "oauth-proxy"
)

// DeploymentStrategy represents the possible values for deployment strategies
// +k8s:openapi-gen=true
type DeploymentStrategy string

const (
// DeploymentStrategyDeprecatedAllInOne represents the (deprecated) 'all-in-one' deployment strategy
// +k8s:openapi-gen=true
DeploymentStrategyDeprecatedAllInOne DeploymentStrategy = "all-in-one"

// DeploymentStrategyAllInOne represents the 'allInOne' deployment strategy (default)
// +k8s:openapi-gen=true
DeploymentStrategyAllInOne DeploymentStrategy = "allinone"

// DeploymentStrategyStreaming represents the 'streaming' deployment strategy
// +k8s:openapi-gen=true
DeploymentStrategyStreaming DeploymentStrategy = "streaming"

// DeploymentStrategyProduction represents the 'production' deployment strategy
// +k8s:openapi-gen=true
DeploymentStrategyProduction DeploymentStrategy = "production"
)

// JaegerSpec defines the desired state of Jaeger
// +k8s:openapi-gen=true
type JaegerSpec struct {
Expand Down

0 comments on commit 0da9d6d

Please sign in to comment.