Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds catalog zone support #22

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions apis/zones/types_zonekind.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const (
ZoneKindNative ZoneKind = iota
ZoneKindMaster
ZoneKindSlave
ZoneKindProducer
ZoneKindConsumer
)

func (k ZoneKind) MarshalJSON() ([]byte, error) {
Expand All @@ -19,6 +21,10 @@ func (k ZoneKind) MarshalJSON() ([]byte, error) {
return []byte(`"Master"`), nil
case ZoneKindSlave:
return []byte(`"Slave"`), nil
case ZoneKindProducer:
return []byte(`"Producer"`), nil
case ZoneKindConsumer:
return []byte(`"Consumer"`), nil
default:
return nil, fmt.Errorf("unsupported zone kind: %d", k)
}
Expand All @@ -32,6 +38,10 @@ func (k *ZoneKind) UnmarshalJSON(input []byte) error {
*k = ZoneKindMaster
case `"Slave"`:
*k = ZoneKindSlave
case `"Producer"`:
*k = ZoneKindProducer
case `"Consumer"`:
*k = ZoneKindConsumer
default:
return fmt.Errorf("unsupported zone kind: %s", string(input))
}
Expand Down
69 changes: 69 additions & 0 deletions apis/zones/types_zonekind_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package zones

import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
func TestZoneKindSerializesCorrectly(t *testing.T) {
data := []struct {
v ZoneKind
e string
}{
{ZoneKindNative, `"Native"`},
{ZoneKindMaster, `"Master"`},
{ZoneKindSlave, `"Slave"`},
{ZoneKindProducer, `"Producer"`},
{ZoneKindConsumer, `"Consumer"`},
}

for i := range data {
t.Run(fmt.Sprintf("serializes to %s", data[i].e), func(t *testing.T) {
j, err := json.Marshal(data[i].v)

assert.Nil(t, err)
assert.Equal(t, data[i].e, string(j))
})
}
}

func TestZoneTypeSerializationReturnErrorOnUnknownValue(t *testing.T) {
var v ZoneKind = 123

_, err := json.Marshal(v)
assert.NotNil(t, err)
}

func TestZoneKindUnserializesCorrectly(t *testing.T) {
data := []struct {
v ZoneKind
e string
}{
{ZoneKindNative, `"Native"`},
{ZoneKindMaster, `"Master"`},
{ZoneKindSlave, `"Slave"`},
{ZoneKindProducer, `"Producer"`},
{ZoneKindConsumer, `"Consumer"`},
}

for i := range data {
t.Run(fmt.Sprintf("serializes to %s", data[i].e), func(t *testing.T) {
var out ZoneKind

err := json.Unmarshal([]byte(data[i].e), &out)

assert.Nil(t, err)
assert.Equal(t, data[i].v, out)
})
}
}

func TestZoneKindUnserializationReturnErrorOnUnknownValue(t *testing.T) {
e := []byte(`"FOO"`)

var out ZoneKind

err := json.Unmarshal(e, &out)
assert.NotNil(t, err)
}
Loading