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

feat: add stringer into rule coder #51

Merged
merged 2 commits into from
Jul 29, 2023
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
5 changes: 5 additions & 0 deletions codes.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package goval

import "fmt"

type ruleCode int

func (r ruleCode) Equal(other RuleCoder) bool {
v, ok := other.(ruleCode)
return ok && r == v
}

func (r ruleCode) String() string { return fmt.Sprintf("%d", r) }

type RuleCoder interface {
Equal(other RuleCoder) bool
fmt.Stringer
}

const (
Expand Down
4 changes: 4 additions & 0 deletions codes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ func (c customTypeButSameBase) Equal(other RuleCoder) bool {
return ok && v == c
}

func (c customTypeButSameBase) String() string { return ruleCode(c).String() }

type customTypeButSameRootBase int

func (c customTypeButSameRootBase) Equal(other RuleCoder) bool {
v, ok := other.(customTypeButSameRootBase)
return ok && v == c
}

func (c customTypeButSameRootBase) String() string { return ruleCode(c).String() }

func TestRuleCode_Equal(t *testing.T) {
tests := []struct {
desc string
Expand Down
5 changes: 4 additions & 1 deletion errtrans/errtrans_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package errtrans

import (
"context"
"github.com/pkg-id/goval"
"strings"
"testing"

"github.com/pkg-id/goval"
)

type ruleCode bool
Expand All @@ -13,6 +14,8 @@ func (r ruleCode) Equal(_ goval.RuleCoder) bool {
return bool(r)
}

func (r ruleCode) String() string { return "ruleCode" }

func TestTranslator_Translate(t *testing.T) {
ctx := context.Background()
bundle, _ := DefaultBundle()
Expand Down
2 changes: 1 addition & 1 deletion goval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestExecute(t *testing.T) {
goval.Bind[int](8, goval.Number[int]().Required().With(customValidator)),
)

if err != internalError {
if !errors.Is(err, internalError) {
t.Fatalf("expect validation error is discarded and internal error is returned; but got %v", err)
}
})
Expand Down
Loading