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

reconciler: Fix Status JSON marshalling #63

Merged
merged 1 commit into from
Oct 31, 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
51 changes: 51 additions & 0 deletions reconciler/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,57 @@ func TestStatusString(t *testing.T) {
assert.Regexp(t, `Error: hey I'm an error \([0-9]+\.[0-9]+.+s ago\)`, s.String())
}

func TestStatusJSON(t *testing.T) {
testCases := []struct {
s Status
expected string
}{
{
Status{
Kind: StatusKindDone,
UpdatedAt: time.Unix(1, 0).UTC(),
Error: "",
},
`{"kind":"Done","updated-at":"1970-01-01T00:00:01Z"}`,
},
{
Status{
Kind: StatusKindPending,
UpdatedAt: time.Unix(2, 0).UTC(),
Error: "",
},
`{"kind":"Pending","updated-at":"1970-01-01T00:00:02Z"}`,
},
{
Status{
Kind: StatusKindError,
UpdatedAt: time.Unix(3, 0).UTC(),
Error: "some-error",
},
`{"kind":"Error","updated-at":"1970-01-01T00:00:03Z","error":"some-error"}`,
},
{
Status{
Kind: StatusKindRefreshing,
UpdatedAt: time.Unix(4, 0).UTC(),
Error: "",
},
`{"kind":"Refreshing","updated-at":"1970-01-01T00:00:04Z"}`,
},
}

for _, tc := range testCases {
b, err := json.Marshal(tc.s)
assert.NoError(t, err, "Marshal")
assert.Equal(t, tc.expected, string(b))

var s Status
assert.NoError(t, json.Unmarshal(b, &s), "Unmarshal")
assert.Equal(t, tc.s, s)
}

}

func sanitizeAgo(s string) string {
r := regexp.MustCompile(`\(.* ago\)`)
return string(r.ReplaceAll([]byte(s), []byte("(??? ago)")))
Expand Down
40 changes: 3 additions & 37 deletions reconciler/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/cilium/statedb"
"github.com/cilium/statedb/index"
"github.com/cilium/statedb/internal"
"gopkg.in/yaml.v3"
)

type Reconciler[Obj any] interface {
Expand Down Expand Up @@ -132,9 +131,9 @@ func (s StatusKind) Key() index.Key {
// the reconciler. Object may have multiple reconcilers and
// multiple reconciliation statuses.
type Status struct {
Kind StatusKind
UpdatedAt time.Time
Error string
Kind StatusKind `json:"kind" yaml:"kind"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you even need these struct tags?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily. My thinking is that this makes the struct nicer to use in scripttest.

UpdatedAt time.Time `json:"updated-at" yaml:"updated-at"`
Error string `json:"error,omitempty" yaml:"error,omitempty"`

// id is a unique identifier for a pending object.
// The reconciler uses this to compare whether the object
Expand All @@ -144,39 +143,6 @@ type Status struct {
id uint64
}

// statusJSON defines the JSON/YAML format for [Status]. Separate to
// [Status] to allow custom unmarshalling that fills in [id].
joamaki marked this conversation as resolved.
Show resolved Hide resolved
type statusJSON struct {
Kind string `json:"kind" yaml:"kind"`
UpdatedAt time.Time `json:"updated-at" yaml:"updated-at"`
Error string `json:"error,omitempty" yaml:"error,omitempty"`
}

func (sj *statusJSON) fill(s *Status) {
s.Kind = StatusKind(sj.Kind)
s.UpdatedAt = sj.UpdatedAt
s.Error = sj.Error
s.id = nextID()
}

func (s *Status) UnmarshalYAML(value *yaml.Node) error {
var sj statusJSON
if err := value.Decode(&sj); err != nil {
return err
}
sj.fill(s)
return nil
}

func (s *Status) UnmarshalJSON(data []byte) error {
var sj statusJSON
if err := json.Unmarshal(data, &sj); err != nil {
return err
}
sj.fill(s)
return nil
}

func (s Status) IsPendingOrRefreshing() bool {
return s.Kind == StatusKindPending || s.Kind == StatusKindRefreshing
}
Expand Down
Loading