Skip to content
Open
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
4 changes: 4 additions & 0 deletions binding/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,7 @@ func (b *Binding) bindJSON(pointer interface{}, bodyBytes []byte) error {
func (b *Binding) ResetJSONUnmarshaler(fn JSONUnmarshaler) {
b.jsonUnmarshalFunc = fn
}

func (b *Binding) ResetValidator(vd *validator.Validator) {
b.vd = vd
}
16 changes: 14 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/bytedance/go-tagexpr/v2

go 1.14
go 1.22

toolchain go1.22.2

require (
github.com/andeya/ameda v1.5.3
Expand All @@ -10,5 +12,15 @@ require (
github.com/stretchr/testify v1.7.5
github.com/tidwall/match v1.1.1
github.com/tidwall/pretty v1.2.0
google.golang.org/protobuf v1.27.1
google.golang.org/protobuf v1.36.6
)

require (
github.com/golang/protobuf v1.5.0 // indirect
github.com/google/go-cmp v0.5.5 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.4.0 // indirect
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
18 changes: 18 additions & 0 deletions tagexpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,25 @@ type (
// VM struct tag expression interpreter
type VM struct {
tagName string
checkPublicOnly bool
structJar map[uintptr]*structVM
rw sync.RWMutex
}

type TagExprOption func(*VM)

func WithTagName(tagName string) TagExprOption {
return func(vm *VM) {
vm.tagName = tagName
}
}

func WithCheckPublicOnly() TagExprOption {
return func(vm *VM) {
vm.checkPublicOnly = true
}
}

// structVM tag expression set of struct
type structVM struct {
vm *VM
Expand Down Expand Up @@ -291,6 +306,9 @@ func (vm *VM) registerStructLocked(structType reflect.Type) (*structVM, error) {
var sub *structVM
for i := 0; i < numField; i++ {
structField = structType.Field(i)
if vm.checkPublicOnly && structField.PkgPath != "" {
continue // skip private fields
}
field, ok, err := s.newFieldVM(structField)
if err != nil {
s.err = err
Expand Down
222 changes: 222 additions & 0 deletions test_data/demo.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ func New(tagName string) *Validator {
return v
}

var (
WithTagName = tagexpr.WithTagName
WithCheckPublicOnly = tagexpr.WithCheckPublicOnly
)

func NewValidator(opts ...tagexpr.TagExprOption) *Validator {
vm := tagexpr.New("")
for _, opt := range opts {
opt(vm)
}

v := &Validator{
vm: vm,
errFactory: defaultErrorFactory,
}
return v
}

// VM returns the struct tag expression interpreter.
func (v *Validator) VM() *tagexpr.VM {
return v.vm
Expand Down
Loading