Skip to content

Commit

Permalink
ref(persist): improve marshal with comparison, request and result (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Oct 8, 2024
1 parent 51dd5d5 commit 988f1a6
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 155 deletions.
18 changes: 15 additions & 3 deletions internal/persist/equals.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"github.com/snivilised/traverse/pref"
)

const (
Anon = "ANON"
)

type UnequalValueError[T any] struct {
Field string
Value T
Expand Down Expand Up @@ -42,13 +46,21 @@ func (UnequalPtrError[T, O]) Unwrap() error {
return locale.ErrUnEqualConversion
}

// Equals compare the pref.Options instance to the derived json instance json.Options.
func Equals(comparison *Comparison) error {
return equalOptions(comparison.O, comparison.JO)
}

// equalOptions compare the pref.Options instance to the derived json instance json.Options.
// We can't use DeepEquals on the structs, because even though the structs
// may have the same members, DeepEqual will still fail because the host struct is
// different; eg: pref.NavigationBehaviours and json.NavigationBehaviours contain
// the same members, but they are different structs; which means comparison has to be
// done manually.
func Equals(o *pref.Options, jo *json.Options) error {
// done manually. equalOptions is only required because we have a custom mapping between
// pref.Options and json.Options, in the form of ToJSON/FromJSON
//
// We do need to apply the same technique to Active state, because there is no json
// version of ActiveState, so there is no custom functionality we need to check.
func equalOptions(o *pref.Options, jo *json.Options) error {
if o == nil {
return fmt.Errorf("pref.Options %w",
UnequalPtrError[pref.Options, json.Options]{
Expand Down
10 changes: 8 additions & 2 deletions internal/persist/json-matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ func (m *MarshalJSONMatcher) Match(actual interface{}) (bool, error) {
return false, fmt.Errorf("❌ matcher expected a *json.Options instance (%T)", jo)
}

if err := persist.Equals(m.o, jo); err != nil {
if err := persist.Equals(&persist.Comparison{
O: m.o,
JO: jo,
}); err != nil {
m.err = err
return false, err
}
Expand Down Expand Up @@ -62,7 +65,10 @@ func (m *UnMarshalJSONMatcher) Match(actual interface{}) (bool, error) {
return false, fmt.Errorf("❌ matcher expected a *pref.Options instance (%T)", o)
}

if err := persist.Equals(o, m.jo); err != nil {
if err := persist.Equals(&persist.Comparison{
O: o,
JO: m.jo,
}); err != nil {
m.err = err
return false, err
}
Expand Down
Loading

0 comments on commit 988f1a6

Please sign in to comment.