Skip to content

Commit

Permalink
handler: remove NewStrict and add SetStrict
Browse files Browse the repository at this point in the history
Allow the caller to choose whether strict field checking is desired.
The default for NewPos and NewStruct remains true.

- Remove the NewStrict constructor.
- Remove a now-obsolete regression test for NewStrict.
- Update tests for NewStrict to use Check + SetStrict(true).
  • Loading branch information
creachadair committed Apr 24, 2022
1 parent 3e34d29 commit e482ade
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
18 changes: 7 additions & 11 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,6 @@ func New(fn interface{}) Func {
return fi.Wrap()
}

// NewStrict acts as New, but enforces strict field checking on an argument of
// struct type.
func NewStrict(fn interface{}) Func {
fi, err := Check(fn)
if err != nil {
panic(err)
}
fi.strictFields = true
return fi.Wrap()
}

var (
ctxType = reflect.TypeOf((*context.Context)(nil)).Elem() // type context.Context
errType = reflect.TypeOf((*error)(nil)).Elem() // type error
Expand All @@ -127,6 +116,13 @@ type FuncInfo struct {
fn interface{} // the original function value
}

// SetStrict sets the flag on fi that determines whether the wrapper it
// generates will enforce strict field checking. If set true, the wrapper will
// report an error when unmarshaling an object into a struct if the object
// contains fields unknown by the struct. Strict field checking has no effect
// for non-struct arguments.
func (fi *FuncInfo) SetStrict(strict bool) { fi.strictFields = strict }

// Wrap adapts the function represented by fi in a Func that satisfies the
// jrpc2.Handler interface. The wrapped function can obtain the *jrpc2.Request
// value from its context argument using the jrpc2.InboundRequest helper.
Expand Down
20 changes: 7 additions & 13 deletions handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,16 @@ func TestStruct(t *testing.T) {
}
}

func TestNewStrict(t *testing.T) {
func TestFuncInfo_SetStrict(t *testing.T) {
type arg struct {
A, B string
}
fn := handler.NewStrict(func(ctx context.Context, arg *arg) error { return nil })
fi, err := handler.Check(func(ctx context.Context, arg *arg) error { return nil })
if err != nil {
t.Fatalf("Check failed: %v", err)
}
fi.SetStrict(true)
fn := fi.Wrap()

req := testutil.MustParseRequest(t, `{
"jsonrpc": "2.0",
Expand All @@ -225,17 +230,6 @@ func TestNewStrict(t *testing.T) {
}
}

// Verify that a handler with no argument type does not panic attempting to
// enforce strict field checking.
func TestNewStrict_argumentRegression(t *testing.T) {
defer func() {
if x := recover(); x != nil {
t.Fatalf("NewStrict panic: %v", x)
}
}()
handler.NewStrict(func(context.Context) error { return nil })
}

// Verify that the handling of pointer-typed arguments does not incorrectly
// introduce another pointer indirection.
func TestNew_pointerRegression(t *testing.T) {
Expand Down

0 comments on commit e482ade

Please sign in to comment.