From e1eb3640563ae231845fd2beb087a6e14055c488 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 7 Apr 2022 11:08:34 +0000 Subject: [PATCH] Bump github.com/Luzifer/rconfig from 1.1.0 to 2.2.0+incompatible Bumps [github.com/Luzifer/rconfig](https://github.com/Luzifer/rconfig) from 1.1.0 to 2.2.0+incompatible. - [Release notes](https://github.com/Luzifer/rconfig/releases) - [Changelog](https://github.com/Luzifer/rconfig/blob/master/History.md) - [Commits](https://github.com/Luzifer/rconfig/compare/v1.1.0...v2.2.0) --- updated-dependencies: - dependency-name: github.com/Luzifer/rconfig dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] --- go.mod | 4 +- go.sum | 6 +- vendor/github.com/Luzifer/rconfig/.travis.yml | 8 +- vendor/github.com/Luzifer/rconfig/History.md | 17 + vendor/github.com/Luzifer/rconfig/LICENSE | 209 +++++++- vendor/github.com/Luzifer/rconfig/README.md | 58 +-- vendor/github.com/Luzifer/rconfig/autoenv.go | 64 +++ vendor/github.com/Luzifer/rconfig/config.go | 150 +++++- vendor/gopkg.in/validator.v2/.gitignore | 23 + vendor/gopkg.in/validator.v2/.travis.yml | 11 + vendor/gopkg.in/validator.v2/LICENSE | 201 ++++++++ vendor/gopkg.in/validator.v2/README.md | 190 +++++++ vendor/gopkg.in/validator.v2/builtins.go | 289 +++++++++++ vendor/gopkg.in/validator.v2/doc.go | 269 ++++++++++ vendor/gopkg.in/validator.v2/validator.go | 480 ++++++++++++++++++ vendor/modules.txt | 7 +- 16 files changed, 1919 insertions(+), 67 deletions(-) create mode 100644 vendor/github.com/Luzifer/rconfig/autoenv.go create mode 100644 vendor/gopkg.in/validator.v2/.gitignore create mode 100644 vendor/gopkg.in/validator.v2/.travis.yml create mode 100644 vendor/gopkg.in/validator.v2/LICENSE create mode 100644 vendor/gopkg.in/validator.v2/README.md create mode 100644 vendor/gopkg.in/validator.v2/builtins.go create mode 100644 vendor/gopkg.in/validator.v2/doc.go create mode 100644 vendor/gopkg.in/validator.v2/validator.go diff --git a/go.mod b/go.mod index 21329cb..f5fea0d 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/Jimdo/vault-unseal go 1.17 require ( - github.com/Luzifer/rconfig v1.1.0 + github.com/Luzifer/rconfig v2.2.0+incompatible golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd ) @@ -11,6 +11,6 @@ require ( github.com/onsi/ginkgo v1.16.5 // indirect github.com/onsi/gomega v1.18.1 // indirect github.com/spf13/pflag v0.0.0-20150923202226-b084184666e0 // indirect - gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect + gopkg.in/validator.v2 v2.0.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index 0761043..5057ff1 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/Luzifer/rconfig v1.1.0 h1:BH/WD3B4Vzb5SkxqjGmvYAm0hRwBeoOTeBSJ90R3Zps= -github.com/Luzifer/rconfig v1.1.0/go.mod h1:9pet6z2+mm/UAB0jF/rf0s62USfHNolzgR6Q4KpsJI0= +github.com/Luzifer/rconfig v2.2.0+incompatible h1:Kle3+rshPM7LxciOheaR4EfHUzibkDDGws04sefQ5m8= +github.com/Luzifer/rconfig v2.2.0+incompatible/go.mod h1:9pet6z2+mm/UAB0jF/rf0s62USfHNolzgR6Q4KpsJI0= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= @@ -108,6 +108,8 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/validator.v2 v2.0.1 h1:xF0KWyGWXm/LM2G1TrEjqOu4pa6coO9AlWSf3msVfDY= +gopkg.in/validator.v2 v2.0.1/go.mod h1:lIUZBlB3Im4s/eYp39Ry/wkR02yOPhZ9IwIRBjuPuG8= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/vendor/github.com/Luzifer/rconfig/.travis.yml b/vendor/github.com/Luzifer/rconfig/.travis.yml index 520bedf..129613f 100644 --- a/vendor/github.com/Luzifer/rconfig/.travis.yml +++ b/vendor/github.com/Luzifer/rconfig/.travis.yml @@ -1,8 +1,12 @@ language: go go: - - 1.4 - - 1.5 + - 1.6.x + - 1.7.x + - 1.8.x + - 1.9.x + - 1.10.x + - 1.11.x - tip script: go test -v -race -cover ./... diff --git a/vendor/github.com/Luzifer/rconfig/History.md b/vendor/github.com/Luzifer/rconfig/History.md index 8bc33a6..589e1dc 100644 --- a/vendor/github.com/Luzifer/rconfig/History.md +++ b/vendor/github.com/Luzifer/rconfig/History.md @@ -1,3 +1,20 @@ +# 2.2.0 / 2018-09-18 + + * Add support for time.Time flags + +# 2.1.0 / 2018-08-02 + + * Add AutoEnv feature + +# 2.0.0 / 2018-08-02 + + * Breaking: Ensure an empty default string does not yield a slice with 1 element + Though this is a just a tiny change it does change the default behaviour, so I'm marking this as a breaking change. You should ensure your code is fine with the changes. + +# 1.2.0 / 2017-06-19 + + * Add ParseAndValidate method + # 1.1.0 / 2016-06-28 * Support time.Duration config parameters diff --git a/vendor/github.com/Luzifer/rconfig/LICENSE b/vendor/github.com/Luzifer/rconfig/LICENSE index 4fde5d2..9d74c44 100644 --- a/vendor/github.com/Luzifer/rconfig/LICENSE +++ b/vendor/github.com/Luzifer/rconfig/LICENSE @@ -1,13 +1,202 @@ -Copyright 2015 Knut Ahlers + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ -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 + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - http://www.apache.org/licenses/LICENSE-2.0 + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2015- Knut Ahlers + + 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 + + http://www.apache.org/licenses/LICENSE-2.0 + + 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. -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. diff --git a/vendor/github.com/Luzifer/rconfig/README.md b/vendor/github.com/Luzifer/rconfig/README.md index 67fbf87..3aa5d1c 100644 --- a/vendor/github.com/Luzifer/rconfig/README.md +++ b/vendor/github.com/Luzifer/rconfig/README.md @@ -1,7 +1,8 @@ [![Build Status](https://travis-ci.org/Luzifer/rconfig.svg?branch=master)](https://travis-ci.org/Luzifer/rconfig) -[![License: Apache v2.0](https://badge.luzifer.io/v1/badge?color=5d79b5&title=license&text=Apache+v2.0)](http://www.apache.org/licenses/LICENSE-2.0) -[![Documentation](https://badge.luzifer.io/v1/badge?title=godoc&text=reference)](https://godoc.org/github.com/Luzifer/rconfig) -[![Go Report](http://goreportcard.com/badge/Luzifer/rconfig)](http://goreportcard.com/report/Luzifer/rconfig) +[![Go Report Card](https://goreportcard.com/badge/github.com/Luzifer/rconfig)](https://goreportcard.com/report/github.com/Luzifer/rconfig) +[![Documentation](https://badges.fyi/static/godoc/reference/5272B4)](https://godoc.org/github.com/Luzifer/rconfig) +![](https://badges.fyi/github/license/Luzifer/rconfig) +[![](https://badges.fyi/github/latest-tag/Luzifer/rconfig)](https://gopkg.in/Luzifer/rconfig.v2) ## Description @@ -18,7 +19,7 @@ go get -u github.com/Luzifer/rconfig OR fetch a specific version: ``` -go get -u gopkg.in/luzifer/rconfig.v1 +go get -u gopkg.in/luzifer/rconfig.v2 ``` Run tests by running: @@ -29,34 +30,31 @@ go test -v -race -cover github.com/Luzifer/rconfig ## Usage -As a first step define a struct holding your configuration: +A very simple usecase is to just configure a struct inside the vars section of your `main.go` and to parse the commandline flags from the `main()` function: ```go -type config struct { - Username string `default:"unknown" flag:"user" description:"Your name"` - Details struct { - Age int `default:"25" flag:"age" env:"age" description:"Your age"` - } -} -``` +package main + +import ( + "fmt" + "github.com/Luzifer/rconfig" +) + +var ( + cfg = struct { + Username string `default:"unknown" flag:"user" description:"Your name"` + Details struct { + Age int `default:"25" flag:"age" env:"age" description:"Your age"` + } + }{} +) -Next create an instance of that struct and let `rconfig` fill that config: - -```go -var cfg config -func init() { - cfg = config{} +func main() { rconfig.Parse(&cfg) -} -``` -You're ready to access your configuration: - -```go -func main() { fmt.Printf("Hello %s, happy birthday for your %dth birthday.", - cfg.Username, - cfg.Details.Age) + cfg.Username, + cfg.Details.Age) } ``` @@ -72,18 +70,14 @@ The order of the directives (lower number = higher precedence): 1. `default` tag in the struct ```go -type config struct { +var cfg = struct { Username string `vardefault:"username" flag:"username" description:"Your username"` } -var cfg = config{} - -func init() { +func main() { rconfig.SetVariableDefaults(rconfig.VarDefaultsFromYAMLFile("~/.myapp.yml")) rconfig.Parse(&cfg) -} -func main() { fmt.Printf("Username = %s", cfg.Username) // Output: Username = luzifer } diff --git a/vendor/github.com/Luzifer/rconfig/autoenv.go b/vendor/github.com/Luzifer/rconfig/autoenv.go new file mode 100644 index 0000000..70ab679 --- /dev/null +++ b/vendor/github.com/Luzifer/rconfig/autoenv.go @@ -0,0 +1,64 @@ +package rconfig + +import "strings" + +type characterClass [2]rune + +func (c characterClass) Contains(r rune) bool { + return c[0] <= r && c[1] >= r +} + +type characterClasses []characterClass + +func (c characterClasses) Contains(r rune) bool { + for _, cc := range c { + if cc.Contains(r) { + return true + } + } + return false +} + +var ( + charGroupUpperLetter = characterClass{'A', 'Z'} + charGroupLowerLetter = characterClass{'a', 'z'} + charGroupNumber = characterClass{'0', '9'} + charGroupLowerNumber = characterClasses{charGroupLowerLetter, charGroupNumber} +) + +func deriveEnvVarName(s string) string { + var ( + words []string + word []rune + ) + + for _, l := range s { + switch { + case charGroupUpperLetter.Contains(l): + if len(word) > 0 && charGroupLowerNumber.Contains(word[len(word)-1]) { + words = append(words, string(word)) + word = []rune{} + } + word = append(word, l) + + case charGroupLowerLetter.Contains(l): + if len(word) > 1 && charGroupUpperLetter.Contains(word[len(word)-1]) { + words = append(words, string(word[0:len(word)-1])) + word = word[len(word)-1:] + } + word = append(word, l) + + case charGroupNumber.Contains(l): + word = append(word, l) + + default: + if len(word) > 0 { + words = append(words, string(word)) + } + word = []rune{} + } + } + words = append(words, string(word)) + + return strings.ToUpper(strings.Join(words, "_")) +} diff --git a/vendor/github.com/Luzifer/rconfig/config.go b/vendor/github.com/Luzifer/rconfig/config.go index dd37238..7df4554 100644 --- a/vendor/github.com/Luzifer/rconfig/config.go +++ b/vendor/github.com/Luzifer/rconfig/config.go @@ -13,11 +13,28 @@ import ( "time" "github.com/spf13/pflag" + validator "gopkg.in/validator.v2" ) +type afterFunc func() error + var ( + autoEnv bool fs *pflag.FlagSet variableDefaults map[string]string + + timeParserFormats = []string{ + // Default constants + time.RFC3339Nano, time.RFC3339, + time.RFC1123Z, time.RFC1123, + time.RFC822Z, time.RFC822, + time.RFC850, time.RubyDate, time.UnixDate, time.ANSIC, + "2006-01-02 15:04:05.999999999 -0700 MST", + // More uncommon time formats + "2006-01-02 15:04:05", "2006-01-02 15:04:05Z07:00", // Simplified ISO time format + "01/02/2006 15:04:05", "01/02/2006 15:04:05Z07:00", // US time format + "02.01.2006 15:04:05", "02.01.2006 15:04:05Z07:00", // DE time format + } ) func init() { @@ -45,11 +62,32 @@ func Parse(config interface{}) error { return parse(config, nil) } +// ParseAndValidate works exactly like Parse but implements an additional run of +// the go-validator package on the configuration struct. Therefore additonal struct +// tags are supported like described in the readme file of the go-validator package: +// +// https://github.com/go-validator/validator/tree/v2#usage +func ParseAndValidate(config interface{}) error { + return parseAndValidate(config, nil) +} + // Args returns the non-flag command-line arguments. func Args() []string { return fs.Args() } +// AddTimeParserFormats adds custom formats to parse time.Time fields +func AddTimeParserFormats(f ...string) { + timeParserFormats = append(timeParserFormats, f...) +} + +// AutoEnv enables or disables automated env variable guessing. If no `env` struct +// tag was set and AutoEnv is enabled the env variable name is derived from the +// name of the field: `MyFieldName` will get `MY_FIELD_NAME` +func AutoEnv(enable bool) { + autoEnv = enable +} + // Usage prints a basic usage with the corresponding defaults for the flags to // os.Stdout. The defaults are derived from the `default` struct-tag and the ENV. func Usage() { @@ -65,28 +103,51 @@ func SetVariableDefaults(defaults map[string]string) { variableDefaults = defaults } +func parseAndValidate(in interface{}, args []string) error { + if err := parse(in, args); err != nil { + return err + } + + return validator.Validate(in) +} + func parse(in interface{}, args []string) error { if args == nil { args = os.Args } fs = pflag.NewFlagSet(os.Args[0], pflag.ExitOnError) - if err := execTags(in, fs); err != nil { + afterFuncs, err := execTags(in, fs) + if err != nil { return err } - return fs.Parse(args) + if err := fs.Parse(args); err != nil { + return err + } + + if afterFuncs != nil { + for _, f := range afterFuncs { + if err := f(); err != nil { + return err + } + } + } + + return nil } -func execTags(in interface{}, fs *pflag.FlagSet) error { +func execTags(in interface{}, fs *pflag.FlagSet) ([]afterFunc, error) { if reflect.TypeOf(in).Kind() != reflect.Ptr { - return errors.New("Calling parser with non-pointer") + return nil, errors.New("Calling parser with non-pointer") } if reflect.ValueOf(in).Elem().Kind() != reflect.Struct { - return errors.New("Calling parser with pointer to non-struct") + return nil, errors.New("Calling parser with pointer to non-struct") } + afterFuncs := []afterFunc{} + st := reflect.ValueOf(in).Elem() for i := 0; i < st.NumField(); i++ { valField := st.Field(i) @@ -98,7 +159,7 @@ func execTags(in interface{}, fs *pflag.FlagSet) error { } value := varDefault(typeField.Tag.Get("vardefault"), typeField.Tag.Get("default")) - value = envDefault(typeField.Tag.Get("env"), value) + value = envDefault(typeField, value) parts := strings.Split(typeField.Tag.Get("flag"), ",") switch typeField.Type { @@ -108,7 +169,7 @@ func execTags(in interface{}, fs *pflag.FlagSet) error { if value == "" { v = time.Duration(0) } else { - return err + return nil, err } } @@ -122,6 +183,53 @@ func execTags(in interface{}, fs *pflag.FlagSet) error { valField.Set(reflect.ValueOf(v)) } continue + + case reflect.TypeOf(time.Time{}): + var sVar string + + if typeField.Tag.Get("flag") != "" { + if len(parts) == 1 { + fs.StringVar(&sVar, parts[0], value, typeField.Tag.Get("description")) + } else { + fs.StringVarP(&sVar, parts[0], parts[1], value, typeField.Tag.Get("description")) + } + } else { + sVar = value + } + + afterFuncs = append(afterFuncs, func(valField reflect.Value, sVar *string) func() error { + return func() error { + if *sVar == "" { + // No time, no problem + return nil + } + + // Check whether we could have a timestamp + if ts, err := strconv.ParseInt(*sVar, 10, 64); err == nil { + t := time.Unix(ts, 0) + valField.Set(reflect.ValueOf(t)) + return nil + } + + // We haven't so lets walk through possible time formats + matched := false + for _, tf := range timeParserFormats { + if t, err := time.Parse(tf, *sVar); err == nil { + matched = true + valField.Set(reflect.ValueOf(t)) + return nil + } + } + + if !matched { + return fmt.Errorf("Value %q did not match expected time formats", *sVar) + } + + return nil + } + }(valField, &sVar)) + + continue } switch typeField.Type.Kind() { @@ -154,7 +262,7 @@ func execTags(in interface{}, fs *pflag.FlagSet) error { if value == "" { vt = 0 } else { - return err + return nil, err } } if typeField.Tag.Get("flag") != "" { @@ -169,7 +277,7 @@ func execTags(in interface{}, fs *pflag.FlagSet) error { if value == "" { vt = 0 } else { - return err + return nil, err } } if typeField.Tag.Get("flag") != "" { @@ -184,7 +292,7 @@ func execTags(in interface{}, fs *pflag.FlagSet) error { if value == "" { vt = 0.0 } else { - return err + return nil, err } } if typeField.Tag.Get("flag") != "" { @@ -194,9 +302,11 @@ func execTags(in interface{}, fs *pflag.FlagSet) error { } case reflect.Struct: - if err := execTags(valField.Addr().Interface(), fs); err != nil { - return err + afs, err := execTags(valField.Addr().Interface(), fs) + if err != nil { + return nil, err } + afterFuncs = append(afterFuncs, afs...) case reflect.Slice: switch typeField.Type.Elem().Kind() { @@ -205,7 +315,7 @@ func execTags(in interface{}, fs *pflag.FlagSet) error { for _, v := range strings.Split(value, ",") { it, err := strconv.ParseInt(strings.TrimSpace(v), 10, 64) if err != nil { - return err + return nil, err } def = append(def, int(it)) } @@ -219,7 +329,10 @@ func execTags(in interface{}, fs *pflag.FlagSet) error { if len(del) == 0 { del = "," } - def := strings.Split(value, del) + var def = []string{} + if value != "" { + def = strings.Split(value, del) + } if len(parts) == 1 { fs.StringSliceVar(valField.Addr().Interface().(*[]string), parts[0], def, typeField.Tag.Get("description")) } else { @@ -229,7 +342,7 @@ func execTags(in interface{}, fs *pflag.FlagSet) error { } } - return nil + return afterFuncs, nil } func registerFlagFloat(t reflect.Kind, fs *pflag.FlagSet, field interface{}, parts []string, vt float64, desc string) { @@ -313,9 +426,14 @@ func registerFlagUint(t reflect.Kind, fs *pflag.FlagSet, field interface{}, part } } -func envDefault(env, def string) string { +func envDefault(field reflect.StructField, def string) string { value := def + env := field.Tag.Get("env") + if env == "" && autoEnv { + env = deriveEnvVarName(field.Name) + } + if env != "" { if e := os.Getenv(env); e != "" { value = e diff --git a/vendor/gopkg.in/validator.v2/.gitignore b/vendor/gopkg.in/validator.v2/.gitignore new file mode 100644 index 0000000..8365624 --- /dev/null +++ b/vendor/gopkg.in/validator.v2/.gitignore @@ -0,0 +1,23 @@ +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe +*.test diff --git a/vendor/gopkg.in/validator.v2/.travis.yml b/vendor/gopkg.in/validator.v2/.travis.yml new file mode 100644 index 0000000..64eea43 --- /dev/null +++ b/vendor/gopkg.in/validator.v2/.travis.yml @@ -0,0 +1,11 @@ +language: go +go: + - "1.10" + - 1.11 + - 1.12 + - tip +go_import_path: gopkg.in/validator.v2 +script: + - go test -race -v -bench=. +notifications: + email: false diff --git a/vendor/gopkg.in/validator.v2/LICENSE b/vendor/gopkg.in/validator.v2/LICENSE new file mode 100644 index 0000000..ad410e1 --- /dev/null +++ b/vendor/gopkg.in/validator.v2/LICENSE @@ -0,0 +1,201 @@ +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + 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 + + http://www.apache.org/licenses/LICENSE-2.0 + + 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. \ No newline at end of file diff --git a/vendor/gopkg.in/validator.v2/README.md b/vendor/gopkg.in/validator.v2/README.md new file mode 100644 index 0000000..ef9ada9 --- /dev/null +++ b/vendor/gopkg.in/validator.v2/README.md @@ -0,0 +1,190 @@ +# Package validator + +Package validator implements variable validations + +# Installation + +Just use go get. + +```bash +go get gopkg.in/validator.v2 +``` + +And then just import the package into your own code. + +```go +import ( + "gopkg.in/validator.v2" +) +``` + +# Usage + +Please see http://godoc.org/gopkg.in/validator.v2 for detailed usage docs. +A simple example would be. + +```go +type NewUserRequest struct { + Username string `validate:"min=3,max=40,regexp=^[a-zA-Z]*$"` + Name string `validate:"nonzero"` + Age int `validate:"min=21"` + Password string `validate:"min=8"` +} + +nur := NewUserRequest{Username: "something", Age: 20} +if errs := validator.Validate(nur); errs != nil { + // values not valid, deal with errors here +} +``` + +Builtin validators + +Here is the list of validators buildin in the package. Validators buildin +will check the element pointed to if the value to check is a pointer. +The `nil` pointer is treated as a valid value by validators buildin other +than `nonzero`, so you should to use `nonzero` if you don't want to +accept a `nil` pointer. + +``` +len + For numeric numbers, len will simply make sure that the + value is equal to the parameter given. For strings, it + checks that the string length is exactly that number of + characters. For slices, arrays, and maps, validates the + number of items. (Usage: len=10) + +max + For numeric numbers, max will simply make sure that the + value is lesser or equal to the parameter given. For strings, + it checks that the string length is at most that number of + characters. For slices, arrays, and maps, validates the + number of items. (Usage: max=10) + +min + For numeric numbers, min will simply make sure that the value + is greater or equal to the parameter given. For strings, it + checks that the string length is at least that number of + characters. For slices, arrays, and maps, validates the + number of items. (Usage: min=10) + +nonzero + This validates that the value is not zero. The appropriate + zero value is given by the Go spec (e.g. for int it's 0, for + string it's "", for pointers is nil, etc.) For structs, it + will not check to see if the struct itself has all zero + values, instead use a pointer or put nonzero on the struct's + keys that you care about. For pointers, the pointer's value + is used to test for nonzero in addition to the pointer itself + not being nil. To just check for not being nil, use `nonnil`. + (Usage: nonzero) + +regexp + Only valid for string types, it will validate that the + value matches the regular expression provided as parameter. + Commas need to be escaped with 2 backslashes `\\`. + (Usage: regexp=^a.*b$) + +nonnil + Validates that the given value is not nil. (Usage: nonnil) +``` + +Custom validators + +It is possible to define custom validators by using SetValidationFunc. +First, one needs to create a validation function. + +```go +// Very simple validator +func notZZ(v interface{}, param string) error { + st := reflect.ValueOf(v) + if st.Kind() != reflect.String { + return errors.New("notZZ only validates strings") + } + if st.String() == "ZZ" { + return errors.New("value cannot be ZZ") + } + return nil +} +``` + +Then one needs to add it to the list of validators and give it a "tag" +name. + +```go +validator.SetValidationFunc("notzz", notZZ) +``` + +Then it is possible to use the notzz validation tag. This will print +"Field A error: value cannot be ZZ" + +```go +type T struct { + A string `validate:"nonzero,notzz"` +} +t := T{"ZZ"} +if errs := validator.Validate(t); errs != nil { + fmt.Printf("Field A error: %s\n", errs["A"][0]) +} +``` + +You can also have multiple sets of validator rules with SetTag(). + +```go +type T struct { + A int `foo:"nonzero" bar:"min=10"` +} +t := T{5} +SetTag("foo") +validator.Validate(t) // valid as it's nonzero +SetTag("bar") +validator.Validate(t) // invalid as it's less than 10 +``` + +SetTag is probably better used with multiple validators. + +```go +fooValidator := validator.NewValidator() +fooValidator.SetTag("foo") +barValidator := validator.NewValidator() +barValidator.SetTag("bar") +fooValidator.Validate(t) +barValidator.Validate(t) +``` + +This keeps the default validator's tag clean. Again, please refer to +godocs for a lot of more examples and different uses. + +# Pull requests policy + +tl;dr. Contributions are welcome. + +The repository is organized in version branches. Pull requests to, say, the +`v2` branch that break API compatibility will not be accepted. It is okay to +break the API in master, _not in the branches_. + +As for validation functions, the preference is to keep the main code simple +and add most new functions to the validator-contrib repository. + +https://github.com/go-validator/validator-contrib + +For improvements and/or fixes to the builtin validation functions, please +make sure the behaviour will not break existing functionality in the branches. +If you see a case where the functionality of the builtin will change +significantly, please send a pull request against `master`. We can discuss then +whether the changes should be incorporated in the version branches as well. + +# License + +Copyright 2014 Roberto Teixeira + +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 + +http://www.apache.org/licenses/LICENSE-2.0 + +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. diff --git a/vendor/gopkg.in/validator.v2/builtins.go b/vendor/gopkg.in/validator.v2/builtins.go new file mode 100644 index 0000000..6fdb92b --- /dev/null +++ b/vendor/gopkg.in/validator.v2/builtins.go @@ -0,0 +1,289 @@ +// Package validator implements value validations +// +// Copyright 2014 Roberto Teixeira +// +// 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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +package validator + +import ( + "reflect" + "regexp" + "strconv" + "unicode/utf8" +) + +// nonzero tests whether a variable value non-zero +// as defined by the golang spec. +func nonzero(v interface{}, param string) error { + st := reflect.ValueOf(v) + var valid bool + switch st.Kind() { + case reflect.String: + valid = utf8.RuneCountInString(st.String()) != 0 + case reflect.Ptr, reflect.Interface: + valid = !st.IsNil() + case reflect.Slice, reflect.Map, reflect.Array: + valid = st.Len() != 0 + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + valid = st.Int() != 0 + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + valid = st.Uint() != 0 + case reflect.Float32, reflect.Float64: + valid = st.Float() != 0 + case reflect.Bool: + valid = st.Bool() + case reflect.Invalid: + valid = false // always invalid + case reflect.Struct: + valid = true // always valid since only nil pointers are empty + default: + return ErrUnsupported + } + + if !valid { + return ErrZeroValue + } + return nil +} + +// length tests whether a variable's length is equal to a given +// value. For strings it tests the number of characters whereas +// for maps and slices it tests the number of items. +func length(v interface{}, param string) error { + st := reflect.ValueOf(v) + var valid bool + if st.Kind() == reflect.Ptr { + if st.IsNil() { + return nil + } + st = st.Elem() + } + switch st.Kind() { + case reflect.String: + p, err := asInt(param) + if err != nil { + return ErrBadParameter + } + valid = int64(utf8.RuneCountInString(st.String())) == p + case reflect.Slice, reflect.Map, reflect.Array: + p, err := asInt(param) + if err != nil { + return ErrBadParameter + } + valid = int64(st.Len()) == p + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + p, err := asInt(param) + if err != nil { + return ErrBadParameter + } + valid = st.Int() == p + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + p, err := asUint(param) + if err != nil { + return ErrBadParameter + } + valid = st.Uint() == p + case reflect.Float32, reflect.Float64: + p, err := asFloat(param) + if err != nil { + return ErrBadParameter + } + valid = st.Float() == p + default: + return ErrUnsupported + } + if !valid { + return ErrLen + } + return nil +} + +// min tests whether a variable value is larger or equal to a given +// number. For number types, it's a simple lesser-than test; for +// strings it tests the number of characters whereas for maps +// and slices it tests the number of items. +func min(v interface{}, param string) error { + st := reflect.ValueOf(v) + invalid := false + if st.Kind() == reflect.Ptr { + if st.IsNil() { + return nil + } + st = st.Elem() + } + switch st.Kind() { + case reflect.String: + p, err := asInt(param) + if err != nil { + return ErrBadParameter + } + invalid = int64(utf8.RuneCountInString(st.String())) < p + case reflect.Slice, reflect.Map, reflect.Array: + p, err := asInt(param) + if err != nil { + return ErrBadParameter + } + invalid = int64(st.Len()) < p + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + p, err := asInt(param) + if err != nil { + return ErrBadParameter + } + invalid = st.Int() < p + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + p, err := asUint(param) + if err != nil { + return ErrBadParameter + } + invalid = st.Uint() < p + case reflect.Float32, reflect.Float64: + p, err := asFloat(param) + if err != nil { + return ErrBadParameter + } + invalid = st.Float() < p + default: + return ErrUnsupported + } + if invalid { + return ErrMin + } + return nil +} + +// max tests whether a variable value is lesser than a given +// value. For numbers, it's a simple lesser-than test; for +// strings it tests the number of characters whereas for maps +// and slices it tests the number of items. +func max(v interface{}, param string) error { + st := reflect.ValueOf(v) + var invalid bool + if st.Kind() == reflect.Ptr { + if st.IsNil() { + return nil + } + st = st.Elem() + } + switch st.Kind() { + case reflect.String: + p, err := asInt(param) + if err != nil { + return ErrBadParameter + } + invalid = int64(utf8.RuneCountInString(st.String())) > p + case reflect.Slice, reflect.Map, reflect.Array: + p, err := asInt(param) + if err != nil { + return ErrBadParameter + } + invalid = int64(st.Len()) > p + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + p, err := asInt(param) + if err != nil { + return ErrBadParameter + } + invalid = st.Int() > p + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + p, err := asUint(param) + if err != nil { + return ErrBadParameter + } + invalid = st.Uint() > p + case reflect.Float32, reflect.Float64: + p, err := asFloat(param) + if err != nil { + return ErrBadParameter + } + invalid = st.Float() > p + default: + return ErrUnsupported + } + if invalid { + return ErrMax + } + return nil +} + +// regex is the builtin validation function that checks +// whether the string variable matches a regular expression +func regex(v interface{}, param string) error { + rv := reflect.ValueOf(v) + if rv.Kind() == reflect.Ptr { + if rv.IsNil() { + return nil + } + rv = rv.Elem() + } + if rv.Kind() != reflect.String { + return ErrUnsupported + } + s := rv.String() + re, err := regexp.Compile(param) + if err != nil { + return ErrBadParameter + } + + if !re.MatchString(s) { + return ErrRegexp + } + return nil +} + +// asInt returns the parameter as a int64 +// or panics if it can't convert +func asInt(param string) (int64, error) { + i, err := strconv.ParseInt(param, 0, 64) + if err != nil { + return 0, ErrBadParameter + } + return i, nil +} + +// asUint returns the parameter as a uint64 +// or panics if it can't convert +func asUint(param string) (uint64, error) { + i, err := strconv.ParseUint(param, 0, 64) + if err != nil { + return 0, ErrBadParameter + } + return i, nil +} + +// asFloat returns the parameter as a float64 +// or panics if it can't convert +func asFloat(param string) (float64, error) { + i, err := strconv.ParseFloat(param, 64) + if err != nil { + return 0.0, ErrBadParameter + } + return i, nil +} + +// nonnil validates that the given pointer is not nil +func nonnil(v interface{}, param string) error { + st := reflect.ValueOf(v) + // if we got a non-pointer then we most likely got + // the value for a pointer field, either way, its not + // nil + switch st.Kind() { + case reflect.Ptr, reflect.Interface, reflect.Func: + if st.IsNil() { + return ErrZeroValue + } + case reflect.Invalid: + // the only way its invalid is if its an interface that's nil + return ErrZeroValue + } + return nil +} diff --git a/vendor/gopkg.in/validator.v2/doc.go b/vendor/gopkg.in/validator.v2/doc.go new file mode 100644 index 0000000..4ffedd0 --- /dev/null +++ b/vendor/gopkg.in/validator.v2/doc.go @@ -0,0 +1,269 @@ +// Package validator implements value validations +// +// Copyright 2014 Roberto Teixeira +// +// 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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +/* +Package validator implements value validations based on struct tags. + +In code it is often necessary to validate that a given value is valid before +using it for something. A typical example might be something like this. + + if age < 18 { + return error.New("age cannot be under 18") + } + +This is a simple enough example, but it can get significantly more complex, +especially when dealing with structs. + + l := len(strings.Trim(s.Username)) + if l < 3 || l > 40 || !regexp.MatchString("^[a-zA-Z]$", s.Username) || s.Age < 18 || s.Password { + return errors.New("Invalid request") + } + +You get the idea. Package validator allows one to define valid values as +struct tags when defining a new struct type. + + type NewUserRequest struct { + Username string `validate:"min=3,max=40,regexp=^[a-zA-Z]*$"` + Name string `validate:"nonzero"` + Age int `validate:"min=18"` + Password string `validate:"min=8"` + } + +Then validating a variable of type NewUserRequest becomes trivial. + + nur := NewUserRequest{Username: "something", ...} + if errs := validator.Validate(nur); errs != nil { + // do something + } + +Builtin validator functions + +Here is the list of validator functions builtin in the package. + + len + For numeric numbers, len will simply make sure that the value is + equal to the parameter given. For strings, it checks that + the string length is exactly that number of characters. For slices, + arrays, and maps, validates the number of items. (Usage: len=10) + + max + For numeric numbers, max will simply make sure that the value is + lesser or equal to the parameter given. For strings, it checks that + the string length is at most that number of characters. For slices, + arrays, and maps, validates the number of items. (Usage: max=10) + + min + For numeric numbers, min will simply make sure that the value is + greater or equal to the parameter given. For strings, it checks that + the string length is at least that number of characters. For slices, + arrays, and maps, validates the number of items. (Usage: min=10) + + nonzero + This validates that the value is not zero. The appropriate zero value + is given by the Go spec (e.g. for int it's 0, for string it's "", for + pointers is nil, etc.). For pointers, the pointer's value is used to + test for nonzero in addition to the pointer itself not being nil. To + just check for not being nil, use nonnil. Usage: nonzero + + regexp + Only valid for string types, it will validate that the value matches + the regular expression provided as parameter. (Usage: regexp=^a.*b$) + + nonnil + Validates that the given value is not nil. Usage: nonnil + +Note that there are no tests to prevent conflicting validator parameters. For +instance, these fields will never be valid. + + ... + A int `validate:"max=0,min=1"` + B string `validate:"len=10,regexp=^$" + ... + +Custom validation functions + +It is possible to define custom validation functions by using SetValidationFunc. +First, one needs to create a validation function. + + // Very simple validation func + func notZZ(v interface{}, param string) error { + st := reflect.ValueOf(v) + if st.Kind() != reflect.String { + return validate.ErrUnsupported + } + if st.String() == "ZZ" { + return errors.New("value cannot be ZZ") + } + return nil + } + +Then one needs to add it to the list of validation funcs and give it a "tag" name. + + validate.SetValidationFunc("notzz", notZZ) + +Then it is possible to use the notzz validation tag. This will print +"Field A error: value cannot be ZZ" + + type T struct { + A string `validate:"nonzero,notzz"` + } + t := T{"ZZ"} + if errs := validator.Validate(t); errs != nil { + fmt.Printf("Field A error: %s\n", errs["A"][0]) + } + +To use parameters, it is very similar. + + // Very simple validator with parameter + func notSomething(v interface{}, param string) error { + st := reflect.ValueOf(v) + if st.Kind() != reflect.String { + return validate.ErrUnsupported + } + if st.String() == param { + return errors.New("value cannot be " + param) + } + return nil + } + +And then the code below should print "Field A error: value cannot be ABC". + + validator.SetValidationFunc("notsomething", notSomething) + type T struct { + A string `validate:"notsomething=ABC"` + } + t := T{"ABC"} + if errs := validator.Validate(t); errs != nil { + fmt.Printf("Field A error: %s\n", errs["A"][0]) + } + +As well, it is possible to overwrite builtin validation functions. + + validate.SetValidationFunc("min", myMinFunc) + +And you can delete a validation function by setting it to nil. + + validate.SetValidationFunc("notzz", nil) + validate.SetValidationFunc("nonzero", nil) + +Using a non-existing validation func in a field tag will always return +false and with error validate.ErrUnknownTag. + +Finally, package validator also provides a helper function that can be used +to validate simple variables/values. + + // errs: nil + errs = validator.Valid(42, "min=10, max=50") + + // errs: [validate.ErrZeroValue] + errs = validator.Valid(nil, "nonzero") + + // errs: [validate.ErrMin,validate.ErrMax] + errs = validator.Valid("hi", "nonzero,min=3,max=2") + +Custom tag name + +In case there is a reason why one would not wish to use tag 'validate' (maybe due to +a conflict with a different package), it is possible to tell the package to use +a different tag. + + validator.SetTag("valid") + +Then. + + Type T struct { + A int `valid:"min=8, max=10"` + B string `valid:"nonzero"` + } + +SetTag is permanent. The new tag name will be used until it is again changed +with a new call to SetTag. A way to temporarily use a different tag exists. + + validator.WithTag("foo").Validate(t) + validator.WithTag("bar").Validate(t) + // But this will go back to using 'validate' + validator.Validate(t) + +Multiple validators + +You may often need to have a different set of validation +rules for different situations. In all the examples above, +we only used the default validator but you could create a +new one and set specific rules for it. + +For instance, you might use the same struct to decode incoming JSON for a REST API +but your needs will change when you're using it to, say, create a new instance +in storage vs. when you need to change something. + + type User struct { + Username string `validate:"nonzero"` + Name string `validate:"nonzero"` + Age int `validate:"nonzero"` + Password string `validate:"nonzero"` + } + +Maybe when creating a new user, you need to make sure all values in the struct are filled, +but then you use the same struct to handle incoming requests to, say, change the password, +in which case you only need the Username and the Password and don't care for the others. +You might use two different validators. + + type User struct { + Username string `creating:"nonzero" chgpw:"nonzero"` + Name string `creating:"nonzero"` + Age int `creating:"nonzero"` + Password string `creating:"nonzero" chgpw:"nonzero"` + } + + var ( + creationValidator = validator.NewValidator() + chgPwValidator = validator.NewValidator() + ) + + func init() { + creationValidator.SetTag("creating") + chgPwValidator.SetTag("chgpw") + } + + ... + + func CreateUserHandler(w http.ResponseWriter, r *http.Request) { + var u User + json.NewDecoder(r.Body).Decode(&user) + if errs := creationValidator.Validate(user); errs != nil { + // the request did not include all of the User + // struct fields, so send a http.StatusBadRequest + // back or something + } + // create the new user + } + + func SetNewUserPasswordHandler(w http.ResponseWriter, r *http.Request) { + var u User + json.NewDecoder(r.Body).Decode(&user) + if errs := chgPwValidator.Validate(user); errs != nil { + // the request did not Username and Password, + // so send a http.StatusBadRequest + // back or something + } + // save the new password + } + +It is also possible to do all of that using only the default validator as long +as SetTag is always called before calling validator.Validate() or you chain the +with WithTag(). + +*/ +package validator diff --git a/vendor/gopkg.in/validator.v2/validator.go b/vendor/gopkg.in/validator.v2/validator.go new file mode 100644 index 0000000..b285876 --- /dev/null +++ b/vendor/gopkg.in/validator.v2/validator.go @@ -0,0 +1,480 @@ +// Package validator implements value validations +// +// Copyright 2014 Roberto Teixeira +// +// 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 +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// 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. + +package validator + +import ( + "bytes" + "errors" + "fmt" + "reflect" + "regexp" + "strings" +) + +// TextErr is an error that also implements the TextMarshaller interface for +// serializing out to various plain text encodings. Packages creating their +// own custom errors should use TextErr if they're intending to use serializing +// formats like json, msgpack etc. +type TextErr struct { + Err error +} + +// Error implements the error interface. +func (t TextErr) Error() string { + return t.Err.Error() +} + +// MarshalText implements the TextMarshaller +func (t TextErr) MarshalText() ([]byte, error) { + return []byte(t.Err.Error()), nil +} + +var ( + // ErrZeroValue is the error returned when variable has zero value + // and nonzero or nonnil was specified + ErrZeroValue = TextErr{errors.New("zero value")} + // ErrMin is the error returned when variable is less than mininum + // value specified + ErrMin = TextErr{errors.New("less than min")} + // ErrMax is the error returned when variable is more than + // maximum specified + ErrMax = TextErr{errors.New("greater than max")} + // ErrLen is the error returned when length is not equal to + // param specified + ErrLen = TextErr{errors.New("invalid length")} + // ErrRegexp is the error returned when the value does not + // match the provided regular expression parameter + ErrRegexp = TextErr{errors.New("regular expression mismatch")} + // ErrUnsupported is the error error returned when a validation rule + // is used with an unsupported variable type + ErrUnsupported = TextErr{errors.New("unsupported type")} + // ErrBadParameter is the error returned when an invalid parameter + // is provided to a validation rule (e.g. a string where an int was + // expected (max=foo,len=bar) or missing a parameter when one is required (len=)) + ErrBadParameter = TextErr{errors.New("bad parameter")} + // ErrUnknownTag is the error returned when an unknown tag is found + ErrUnknownTag = TextErr{errors.New("unknown tag")} + // ErrInvalid is the error returned when variable is invalid + // (normally a nil pointer) + ErrInvalid = TextErr{errors.New("invalid value")} + // ErrCannotValidate is the error returned when a struct is unexported + ErrCannotValidate = TextErr{errors.New("cannot validate unexported struct")} +) + +// ErrorMap is a map which contains all errors from validating a struct. +type ErrorMap map[string]ErrorArray + +// ErrorMap implements the Error interface so we can check error against nil. +// The returned error is all existing errors with the map. +func (err ErrorMap) Error() string { + var b bytes.Buffer + + for k, errs := range err { + if len(errs) > 0 { + b.WriteString(fmt.Sprintf("%s: %s, ", k, errs.Error())) + } + } + + return strings.TrimSuffix(b.String(), ", ") +} + +// ErrorArray is a slice of errors returned by the Validate function. +type ErrorArray []error + +// ErrorArray implements the Error interface and returns all the errors comma seprated +// if errors exist. +func (err ErrorArray) Error() string { + var b bytes.Buffer + + for _, errs := range err { + b.WriteString(fmt.Sprintf("%s, ", errs.Error())) + } + + errs := b.String() + return strings.TrimSuffix(errs, ", ") +} + +// ValidationFunc is a function that receives the value of a +// field and a parameter used for the respective validation tag. +type ValidationFunc func(v interface{}, param string) error + +// Validator implements a validator +type Validator struct { + // validationFuncs is a map of ValidationFuncs indexed + // by their name. + validationFuncs map[string]ValidationFunc + // Tag name being used. + tagName string + // printJSON set to true will make errors print with the + // name of their json field instead of their struct tag. + // If no json tag is present the name of the struct field is used. + printJSON bool +} + +// Helper validator so users can use the +// functions directly from the package +var defaultValidator = NewValidator() + +// NewValidator creates a new Validator +func NewValidator() *Validator { + return &Validator{ + tagName: "validate", + validationFuncs: map[string]ValidationFunc{ + "nonzero": nonzero, + "len": length, + "min": min, + "max": max, + "regexp": regex, + "nonnil": nonnil, + }, + printJSON: false, + } +} + +// SetTag allows you to change the tag name used in structs +func SetTag(tag string) { + defaultValidator.SetTag(tag) +} + +// SetTag allows you to change the tag name used in structs +func (mv *Validator) SetTag(tag string) { + mv.tagName = tag +} + +// WithTag creates a new Validator with the new tag name. It is +// useful to chain-call with Validate so we don't change the tag +// name permanently: validator.WithTag("foo").Validate(t) +func WithTag(tag string) *Validator { + return defaultValidator.WithTag(tag) +} + +// WithTag creates a new Validator with the new tag name. It is +// useful to chain-call with Validate so we don't change the tag +// name permanently: validator.WithTag("foo").Validate(t) +func (mv *Validator) WithTag(tag string) *Validator { + v := mv.copy() + v.SetTag(tag) + return v +} + +// SetPrintJSON allows you to print errors with json tag names present in struct tags +func SetPrintJSON(printJSON bool) { + defaultValidator.SetPrintJSON(printJSON) +} + +// SetPrintJSON allows you to print errors with json tag names present in struct tags +func (mv *Validator) SetPrintJSON(printJSON bool) { + mv.printJSON = printJSON +} + +// WithPrintJSON creates a new Validator with printJSON set to new value. It is +// useful to chain-call with Validate so we don't change the print option +// permanently: validator.WithPrintJSON(true).Validate(t) +func WithPrintJSON(printJSON bool) *Validator { + return defaultValidator.WithPrintJSON(printJSON) +} + +// WithPrintJSON creates a new Validator with printJSON set to new value. It is +// useful to chain-call with Validate so we don't change the print option +// permanently: validator.WithTag("foo").WithPrintJSON(true).Validate(t) +func (mv *Validator) WithPrintJSON(printJSON bool) *Validator { + v := mv.copy() + v.SetPrintJSON(printJSON) + return v +} + +// Copy a validator +func (mv *Validator) copy() *Validator { + newFuncs := map[string]ValidationFunc{} + for k, f := range mv.validationFuncs { + newFuncs[k] = f + } + return &Validator{ + tagName: mv.tagName, + validationFuncs: newFuncs, + printJSON: mv.printJSON, + } +} + +// SetValidationFunc sets the function to be used for a given +// validation constraint. Calling this function with nil vf +// is the same as removing the constraint function from the list. +func SetValidationFunc(name string, vf ValidationFunc) error { + return defaultValidator.SetValidationFunc(name, vf) +} + +// SetValidationFunc sets the function to be used for a given +// validation constraint. Calling this function with nil vf +// is the same as removing the constraint function from the list. +func (mv *Validator) SetValidationFunc(name string, vf ValidationFunc) error { + if name == "" { + return errors.New("name cannot be empty") + } + if vf == nil { + delete(mv.validationFuncs, name) + return nil + } + mv.validationFuncs[name] = vf + return nil +} + +// Validate calls the Validate method on the default validator. +func Validate(v interface{}) error { + return defaultValidator.Validate(v) +} + +// Validate validates the fields of structs (included embedded structs) based on +// 'validator' tags and returns errors found indexed by the field name. +func (mv *Validator) Validate(v interface{}) error { + m := make(ErrorMap) + mv.deepValidateCollection(reflect.ValueOf(v), m, func() string { + return "" + }) + if len(m) > 0 { + return m + } + return nil +} + +func (mv *Validator) validateStruct(sv reflect.Value, m ErrorMap) error { + kind := sv.Kind() + if (kind == reflect.Ptr || kind == reflect.Interface) && !sv.IsNil() { + return mv.validateStruct(sv.Elem(), m) + } + if kind != reflect.Struct && kind != reflect.Interface { + return ErrUnsupported + } + + st := sv.Type() + nfields := st.NumField() + for i := 0; i < nfields; i++ { + if err := mv.validateField(st.Field(i), sv.Field(i), m); err != nil { + return err + } + } + + return nil +} + +// validateField validates the field of fieldVal referred to by fieldDef. +// If fieldDef refers to an anonymous/embedded field, +// validateField will walk all of the embedded type's fields and validate them on sv. +func (mv *Validator) validateField(fieldDef reflect.StructField, fieldVal reflect.Value, m ErrorMap) error { + tag := fieldDef.Tag.Get(mv.tagName) + if tag == "-" { + return nil + } + // deal with pointers + for (fieldVal.Kind() == reflect.Ptr || fieldVal.Kind() == reflect.Interface) && !fieldVal.IsNil() { + fieldVal = fieldVal.Elem() + } + + // ignore private structs unless Anonymous + if !fieldDef.Anonymous && fieldDef.PkgPath != "" { + return nil + } + + var errs ErrorArray + if tag != "" { + var err error + if fieldDef.PkgPath != "" { + err = ErrCannotValidate + } else { + err = mv.validValue(fieldVal, tag) + } + if errarr, ok := err.(ErrorArray); ok { + errs = errarr + } else if err != nil { + errs = ErrorArray{err} + } + } + + // no-op if field is not a struct, interface, array, slice or map + fn := mv.fieldName(fieldDef) + mv.deepValidateCollection(fieldVal, m, func() string { + return fn + }) + + if len(errs) > 0 { + m[fn] = errs + } + return nil +} + +func (mv *Validator) fieldName(fieldDef reflect.StructField) string { + if mv.printJSON { + if jsonTagValue, ok := fieldDef.Tag.Lookup("json"); ok { + return parseName(jsonTagValue) + } + } + return fieldDef.Name +} + +func (mv *Validator) deepValidateCollection(f reflect.Value, m ErrorMap, fnameFn func() string) { + switch f.Kind() { + case reflect.Interface, reflect.Ptr: + if f.IsNil() { + return + } + mv.deepValidateCollection(f.Elem(), m, fnameFn) + case reflect.Struct: + subm := make(ErrorMap) + err := mv.validateStruct(f, subm) + parentName := fnameFn() + if err != nil { + m[parentName] = ErrorArray{err} + } + for j, k := range subm { + keyName := j + if parentName != "" { + keyName = parentName + "." + keyName + } + m[keyName] = k + } + case reflect.Array, reflect.Slice: + // we don't need to loop over every byte in a byte slice so we only end up + // looping when the kind is something we care about + switch f.Type().Elem().Kind() { + case reflect.Struct, reflect.Interface, reflect.Ptr, reflect.Map, reflect.Array, reflect.Slice: + for i := 0; i < f.Len(); i++ { + mv.deepValidateCollection(f.Index(i), m, func() string { + return fmt.Sprintf("%s[%d]", fnameFn(), i) + }) + } + } + case reflect.Map: + for _, key := range f.MapKeys() { + mv.deepValidateCollection(key, m, func() string { + return fmt.Sprintf("%s[%+v](key)", fnameFn(), key.Interface()) + }) // validate the map key + value := f.MapIndex(key) + mv.deepValidateCollection(value, m, func() string { + return fmt.Sprintf("%s[%+v](value)", fnameFn(), key.Interface()) + }) + } + } +} + +// Valid validates a value based on the provided +// tags and returns errors found or nil. +func Valid(val interface{}, tags string) error { + return defaultValidator.Valid(val, tags) +} + +// Valid validates a value based on the provided +// tags and returns errors found or nil. +func (mv *Validator) Valid(val interface{}, tags string) error { + if tags == "-" { + return nil + } + v := reflect.ValueOf(val) + if (v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface) && !v.IsNil() { + return mv.validValue(v.Elem(), tags) + } + if v.Kind() == reflect.Invalid { + return mv.validateVar(nil, tags) + } + return mv.validateVar(val, tags) +} + +// validValue is like Valid but takes a Value instead of an interface +func (mv *Validator) validValue(v reflect.Value, tags string) error { + if v.Kind() == reflect.Invalid { + return mv.validateVar(nil, tags) + } + return mv.validateVar(v.Interface(), tags) +} + +// validateVar validates one single variable +func (mv *Validator) validateVar(v interface{}, tag string) error { + tags, err := mv.parseTags(tag) + if err != nil { + // unknown tag found, give up. + return err + } + errs := make(ErrorArray, 0, len(tags)) + for _, t := range tags { + if err := t.Fn(v, t.Param); err != nil { + errs = append(errs, err) + } + } + if len(errs) > 0 { + return errs + } + return nil +} + +// tag represents one of the tag items +type tag struct { + Name string // name of the tag + Fn ValidationFunc // validation function to call + Param string // parameter to send to the validation function +} + +// separate by no escaped commas +var sepPattern *regexp.Regexp = regexp.MustCompile(`((?:^|[^\\])(?:\\\\)*),`) + +func splitUnescapedComma(str string) []string { + ret := []string{} + indexes := sepPattern.FindAllStringIndex(str, -1) + last := 0 + for _, is := range indexes { + ret = append(ret, str[last:is[1]-1]) + last = is[1] + } + ret = append(ret, str[last:]) + return ret +} + +// parseTags parses all individual tags found within a struct tag. +func (mv *Validator) parseTags(t string) ([]tag, error) { + tl := splitUnescapedComma(t) + tags := make([]tag, 0, len(tl)) + for _, i := range tl { + i = strings.Replace(i, `\,`, ",", -1) + tg := tag{} + v := strings.SplitN(i, "=", 2) + tg.Name = strings.Trim(v[0], " ") + if tg.Name == "" { + return []tag{}, ErrUnknownTag + } + if len(v) > 1 { + tg.Param = strings.Trim(v[1], " ") + } + var found bool + if tg.Fn, found = mv.validationFuncs[tg.Name]; !found { + return []tag{}, ErrUnknownTag + } + tags = append(tags, tg) + + } + return tags, nil +} + +func parseName(tag string) string { + if tag == "" { + return "" + } + + name := strings.SplitN(tag, ",", 2)[0] + + // if the field as be skipped in json, just return an empty string + if name == "-" { + return "" + } + return name +} diff --git a/vendor/modules.txt b/vendor/modules.txt index da16a30..7129b35 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,4 +1,4 @@ -# github.com/Luzifer/rconfig v1.1.0 +# github.com/Luzifer/rconfig v2.2.0+incompatible ## explicit github.com/Luzifer/rconfig # github.com/onsi/ginkgo v1.16.5 @@ -12,8 +12,9 @@ github.com/spf13/pflag ## explicit; go 1.17 golang.org/x/net/context golang.org/x/net/context/ctxhttp -# gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c -## explicit; go 1.11 +# gopkg.in/validator.v2 v2.0.1 +## explicit; go 1.18 +gopkg.in/validator.v2 # gopkg.in/yaml.v2 v2.4.0 ## explicit; go 1.15 gopkg.in/yaml.v2