https://github.com/bytedance/go-tagexpr

An interesting go struct tag expression syntax for field validation, etc.

https://github.com/bytedance/go-tagexpr

Science Score: 13.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (9.6%) to scientific vocabulary

Keywords

binding dsl expression go struct-tag validator
Last synced: 5 months ago · JSON representation

Repository

An interesting go struct tag expression syntax for field validation, etc.

Basic Info
  • Host: GitHub
  • Owner: bytedance
  • License: apache-2.0
  • Language: Go
  • Default Branch: master
  • Size: 468 KB
Statistics
  • Stars: 1,704
  • Watchers: 29
  • Forks: 141
  • Open Issues: 27
  • Releases: 72
Topics
binding dsl expression go struct-tag validator
Created about 7 years ago · Last pushed almost 2 years ago
Metadata Files
Readme License

README.md

go-tagexpr report card GoDoc

An interesting go struct tag expression syntax for field validation, etc.

Usage

  • Validator: A powerful validator that supports struct tag expression

  • Binding: A powerful HTTP request parameters binder that supports struct tag expression

Feature

  • Support for a variety of common operator
  • Support for accessing arrays, slices, members of the dictionary
  • Support access to any field in the current structure
  • Support access to nested fields, non-exported fields, etc.
  • Support variable
  • Support registers function expression
  • Built-in len, sprintf, regexp functions
  • Support single mode and multiple mode to define expression
  • Parameter check subpackage
  • Use offset pointers to directly take values, better performance
  • Required go version ≥1.9

Example

```go package tagexpr_test

import ( "fmt"

tagexpr "github.com/bytedance/go-tagexpr/v2"

)

func Example() { type T struct { A int tagexpr:"$<0||$>=100" B string tagexpr:"len($)>1 && regexp('^\\w*$')" C bool tagexpr:"expr1:(f.g)$>0 && $; expr2:'C must be true when T.f.g>0'" d []string tagexpr:"@:len($)>0 && $[0]=='D'; msg:sprintf('invalid d: %v',$)" e map[string]int tagexpr:"len($)==$['len']" e2 map[string]*int tagexpr:"len($)==$['len']" f struct { g int tagexpr:"$" } h int tagexpr:"$>minVal" }

vm := tagexpr.New("tagexpr")
t := &T{
    A:  107,
    B:  "abc",
    C:  true,
    d:  []string{"x", "y"},
    e:  map[string]int{"len": 1},
    e2: map[string]*int{"len": new(int)},
    f: struct {
        g int `tagexpr:"$"`
    }{1},
    h: 10,
}

tagExpr, err := vm.Run(t)
if err != nil {
    panic(err)
}

fmt.Println(tagExpr.Eval("A"))
fmt.Println(tagExpr.Eval("B"))
fmt.Println(tagExpr.Eval("C@expr1"))
fmt.Println(tagExpr.Eval("C@expr2"))
if !tagExpr.Eval("d").(bool) {
    fmt.Println(tagExpr.Eval("d@msg"))
}
fmt.Println(tagExpr.Eval("e"))
fmt.Println(tagExpr.Eval("e2"))
fmt.Println(tagExpr.Eval("f.g"))
fmt.Println(tagExpr.EvalWithEnv("h", map[string]interface{}{"minVal": 9}))
fmt.Println(tagExpr.EvalWithEnv("h", map[string]interface{}{"minVal": 11}))

// Output:
// true
// true
// true
// C must be true when T.f.g>0
// invalid d: [x y]
// true
// false
// 1
// true
// false

} ```

Syntax

Struct tag syntax spec:

type T struct { // Single model Field1 T1 `tagName:"expression"` // Multiple model Field2 T2 `tagName:"exprName:expression; [exprName2:expression2;]..."` // Omit it Field3 T3 `tagName:"-"` // Omit it when it is nil Field4 T4 `tagName:"?"` ... }

NOTE: The exprName under the same struct field cannot be the same!

|Operator or Operand|Explain| |-----|---------| |true false|boolean| |0 0.0|float64 "0"| |''|String| |\\'| Escape ' delims in string| |\"| Escape " delims in string| |nil|nil, undefined| |!|not| |+|Digital addition or string splicing| |-|Digital subtraction or negative| |*|Digital multiplication| |/|Digital division| |%|division remainder, as: float64(int64(a)%int64(b))| |==|eq| |!=|ne| |>|gt| |>=|ge| |<|lt| |<=|le| |&&|Logic and| |\|\||Logic or| |()|Expression group| |(X)$|Struct field value named X| |(X.Y)$|Struct field value named X.Y| |$|Shorthand for (X)$, omit (X) to indicate current struct field value| |(X)$['A']|Map value with key A or struct A sub-field in the struct field X| |(X)$[0]|The 0th element or sub-field of the struct field X(type: map, slice, array, struct)| |len((X)$)|Built-in function len, the length of struct field X| |mblen((X)$)|the length of string field X (character number)| |regexp('^\\w*$', (X)$)|Regular match the struct field X, return boolean| |regexp('^\\w*$')|Regular match the current struct field, return boolean| |sprintf('X value: %v', (X)$)|fmt.Sprintf, format the value of struct field X| |range(KvExpr, forEachExpr)|Iterate over an array, slice, or dictionary
- #k is the element key var
- #v is the element value var
- ## is the number of elements
- e.g. example| |in((X)$, enum_1, ...enum_n)|Check if the first parameter is one of the enumerated parameters|

Operator priority(high -> low):

  • () ! bool float64 string nil
  • * / %
  • + -
  • < <= > >=
  • == !=
  • &&
  • ||

Field Selector

field_lv1.field_lv2...field_lvn

Expression Selector

  • If expression is single model or exprName is @:

field_lv1.field_lv2...field_lvn

  • If expression is multiple model and exprName is not @:

field_lv1.field_lv2...field_lvn@exprName

Benchmark

goos: darwin goarch: amd64 pkg: github.com/bytedance/go-tagexpr BenchmarkTagExpr-4 10000000 148 ns/op 32 B/op 3 allocs/op BenchmarkReflect-4 10000000 182 ns/op 16 B/op 2 allocs/op PASS

Go to test code

Owner

  • Name: Bytedance Inc.
  • Login: bytedance
  • Kind: organization
  • Location: Singapore

GitHub Events

Total
  • Watch event: 75
  • Issue comment event: 1
  • Pull request event: 1
Last Year
  • Watch event: 75
  • Issue comment event: 1
  • Pull request event: 1

Committers

Last synced: 9 months ago

All Time
  • Total Commits: 376
  • Total Committers: 16
  • Avg Commits per committer: 23.5
  • Development Distribution Score (DDS): 0.144
Past Year
  • Commits: 0
  • Committers: 0
  • Avg Commits per committer: 0.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
henrylee2cn h****n@g****m 322
Wang Yichao y****g@b****m 17
guyinyou g****u@b****m 10
dugenkui d****k@f****m 6
chyroc c****c@q****m 4
fgy f****5@b****m 4
zhaixiaolei.leo z****o@b****m 3
xielong l****u@g****m 2
0xflotus 0****s@g****m 1
HuanGong g****v@g****m 1
cui fliter i****g@g****m 1
zhouweining 3****3@q****m 1
ChenYao c****x@b****m 1
liutong l****c@b****m 1
minhchen m****n@w****n 1
yangsongbao y****o@b****m 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 9 months ago

All Time
  • Total issues: 46
  • Total pull requests: 38
  • Average time to close issues: about 1 month
  • Average time to close pull requests: 6 days
  • Total issue authors: 38
  • Total pull request authors: 21
  • Average comments per issue: 1.33
  • Average comments per pull request: 0.47
  • Merged pull requests: 25
  • Bot issues: 0
  • Bot pull requests: 2
Past Year
  • Issues: 3
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 3
  • Pull request authors: 0
  • Average comments per issue: 0.67
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • WangBeyond (3)
  • 21888 (2)
  • Duslia (2)
  • XIELongDragon (2)
  • huahuayu (2)
  • tylitianrui (2)
  • ericuni (2)
  • xycui (1)
  • KunWang2 (1)
  • CCS-CloudServices (1)
  • lixiangchen-zh (1)
  • zeeshen (1)
  • haoxins (1)
  • qjpcpu (1)
  • ealyn (1)
Pull Request Authors
  • FGYFFFF (5)
  • chyroc (4)
  • dugenkui03 (4)
  • fazledyn-or (4)
  • WangBeyond (3)
  • BooksLiu (3)
  • Leo-stone-dot (2)
  • dependabot[bot] (2)
  • XIELongDragon (2)
  • mougeCM (1)
  • jie-huang (1)
  • remrain (1)
  • sikasjc (1)
  • guyinyou (1)
  • andeya (1)
Top Labels
Issue Labels
Pull Request Labels
dependencies (2)

Packages

  • Total packages: 4
  • Total downloads: unknown
  • Total docker downloads: 1,393
  • Total dependent packages: 483
    (may contain duplicates)
  • Total dependent repositories: 249
    (may contain duplicates)
  • Total versions: 172
proxy.golang.org: github.com/bytedance/go-tagexpr/v2

Package tagexpr is an interesting go struct tag expression syntax for field validation, etc. Copyright 2019 Bytedance Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

  • Versions: 52
  • Dependent Packages: 472
  • Dependent Repositories: 242
  • Docker Downloads: 1,393
Rankings
Dependent packages count: 0.3%
Dependent repos count: 0.4%
Average: 1.2%
Docker downloads count: 1.5%
Stargazers count: 1.7%
Forks count: 2.3%
Last synced: 6 months ago
proxy.golang.org: github.com/bytedance/go-tagexpr

Package tagexpr is an interesting go struct tag expression syntax for field validation, etc. Copyright 2019 Bytedance Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

  • Versions: 48
  • Dependent Packages: 11
  • Dependent Repositories: 7
Rankings
Dependent packages count: 1.6%
Stargazers count: 1.8%
Average: 1.9%
Dependent repos count: 1.9%
Forks count: 2.4%
Last synced: 5 months ago
proxy.golang.org: github.com/ByteDance/go-tagexpr

Package tagexpr is an interesting go struct tag expression syntax for field validation, etc. Copyright 2019 Bytedance Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

  • Versions: 20
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Stargazers count: 1.7%
Forks count: 2.3%
Average: 4.0%
Dependent packages count: 5.7%
Dependent repos count: 6.1%
Last synced: 6 months ago
proxy.golang.org: github.com/ByteDance/go-tagexpr/v2
  • Versions: 52
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 5.7%
Average: 5.9%
Dependent repos count: 6.1%
Last synced: 6 months ago

Dependencies

go.mod go
  • github.com/andeya/goutil v0.0.0-20220704075712-42f2ec55fe8d
  • github.com/davecgh/go-spew v1.1.1
  • github.com/henrylee2cn/ameda v1.4.10
  • github.com/nyaruka/phonenumbers v1.0.55
  • github.com/stretchr/testify v1.7.2
  • github.com/tidwall/gjson v1.9.3
  • google.golang.org/protobuf v1.27.1
go.sum go
  • github.com/andeya/goutil v0.0.0-20220704075712-42f2ec55fe8d
  • github.com/davecgh/go-spew v1.1.0
  • github.com/davecgh/go-spew v1.1.1
  • github.com/golang/protobuf v1.3.2
  • github.com/golang/protobuf v1.5.0
  • github.com/google/go-cmp v0.5.5
  • github.com/henrylee2cn/ameda v1.4.10
  • github.com/nyaruka/phonenumbers v1.0.55
  • github.com/pmezard/go-difflib v1.0.0
  • github.com/stretchr/objx v0.1.0
  • github.com/stretchr/testify v1.4.0
  • github.com/stretchr/testify v1.7.2
  • github.com/tidwall/gjson v1.9.3
  • github.com/tidwall/match v1.1.1
  • github.com/tidwall/pretty v1.2.0
  • golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
  • google.golang.org/protobuf v1.26.0-rc.1
  • google.golang.org/protobuf v1.27.1
  • gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405
  • gopkg.in/yaml.v2 v2.2.2
  • gopkg.in/yaml.v3 v3.0.1