项目作者: moznion

项目描述 :
A simple parser for golang's custom tags
高级语言: Go
项目地址: git://github.com/moznion/go-struct-custom-tag-parser.git
创建时间: 2019-01-01T16:05:44Z
项目社区:https://github.com/moznion/go-struct-custom-tag-parser

开源协议:MIT License

下载


go-struct-custom-tag-parser

Build Status GoDoc

A simple parser for golang’s struct custom tags.

STOP!!!!

You don’t have to use this library. You should use reflect.StructTag instead of this.

This library’s processing time is slower than reflect’s one.

  1. goos: darwin
  2. goarch: amd64
  3. pkg: github.com/moznion/go-struct-custom-tag-parser/author
  4. BenchmarkParser-4 1000000 1932 ns/op 848 B/op 11 allocs/op
  5. BenchmarkReflect-4 10000000 140 ns/op 0 B/op 0 allocs/op
  6. PASS
  7. ok github.com/moznion/go-struct-custom-tag-parser/author 4.484s

(benchmark code is here: author/parser_bench_test.go)

There is only one reason for using this library, this has a feature to check the syntax of custom tag (in strict mode, Parse() returns an error when a custom tag is an invalid syntax).

Synopsis

with strict mode

It raises an error when an unacceptable custom tag is given on parsing.

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "github.com/moznion/go-struct-custom-tag-parser"
  6. )
  7. func main() {
  8. result, err := tagparser.Parse(`foo:"bar" buz:"qux,foobar"`, true)
  9. if err != nil {
  10. log.Fatalf("unexpected error has come: %s", err)
  11. }
  12. fmt.Printf("%v\n", result) // => map[foo:bar buz:qux,foobar]
  13. }

with no strict mode

It immediately returns the processed results until just before the invalid custom tag syntax. It never raises any error.

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/moznion/go-struct-custom-tag-parser"
  5. )
  6. func main() {
  7. result, _ := tagparser.Parse(`foo:"bar" buz:"qux,foobar"`, false)
  8. fmt.Printf("%v\n", result) // => map[foo:bar buz:qux,foobar]
  9. }

License

  1. The MIT License (MIT)
  2. Copyright © 2019 moznion, http://moznion.net/ <moznion@gmail.com>
  3. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  4. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  5. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.