fix: error handling and vendor
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
26
vendor/github.com/go-openapi/strfmt/.editorconfig
generated
vendored
Normal file
26
vendor/github.com/go-openapi/strfmt/.editorconfig
generated
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
# top-most EditorConfig file
|
||||
root = true
|
||||
|
||||
# Unix-style newlines with a newline ending every file
|
||||
[*]
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
# Set default charset
|
||||
[*.{js,py,go,scala,rb,java,html,css,less,sass,md}]
|
||||
charset = utf-8
|
||||
|
||||
# Tab indentation (no size specified)
|
||||
[*.go]
|
||||
indent_style = tab
|
||||
|
||||
[*.md]
|
||||
trim_trailing_whitespace = false
|
||||
|
||||
# Matches the exact files either package.json or .travis.yml
|
||||
[{package.json,.travis.yml}]
|
||||
indent_style = space
|
||||
indent_size = 2
|
2
vendor/github.com/go-openapi/strfmt/.gitattributes
generated
vendored
Normal file
2
vendor/github.com/go-openapi/strfmt/.gitattributes
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*.go text eol=lf
|
||||
|
2
vendor/github.com/go-openapi/strfmt/.gitignore
generated
vendored
Normal file
2
vendor/github.com/go-openapi/strfmt/.gitignore
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
secrets.yml
|
||||
coverage.out
|
61
vendor/github.com/go-openapi/strfmt/.golangci.yml
generated
vendored
Normal file
61
vendor/github.com/go-openapi/strfmt/.golangci.yml
generated
vendored
Normal file
@ -0,0 +1,61 @@
|
||||
linters-settings:
|
||||
govet:
|
||||
check-shadowing: true
|
||||
golint:
|
||||
min-confidence: 0
|
||||
gocyclo:
|
||||
min-complexity: 45
|
||||
maligned:
|
||||
suggest-new: true
|
||||
dupl:
|
||||
threshold: 200
|
||||
goconst:
|
||||
min-len: 2
|
||||
min-occurrences: 3
|
||||
|
||||
linters:
|
||||
enable-all: true
|
||||
disable:
|
||||
- maligned
|
||||
- unparam
|
||||
- lll
|
||||
- gochecknoinits
|
||||
- gochecknoglobals
|
||||
- funlen
|
||||
- godox
|
||||
- gocognit
|
||||
- whitespace
|
||||
- wsl
|
||||
- wrapcheck
|
||||
- testpackage
|
||||
- nlreturn
|
||||
- gomnd
|
||||
- exhaustivestruct
|
||||
- goerr113
|
||||
- errorlint
|
||||
- nestif
|
||||
- godot
|
||||
- gofumpt
|
||||
- paralleltest
|
||||
- tparallel
|
||||
- thelper
|
||||
- ifshort
|
||||
- exhaustruct
|
||||
- varnamelen
|
||||
- gci
|
||||
- depguard
|
||||
- errchkjson
|
||||
- inamedparam
|
||||
- nonamedreturns
|
||||
- musttag
|
||||
- ireturn
|
||||
- forcetypeassert
|
||||
- cyclop
|
||||
# deprecated linters
|
||||
- deadcode
|
||||
- interfacer
|
||||
- scopelint
|
||||
- varcheck
|
||||
- structcheck
|
||||
- golint
|
||||
- nosnakecase
|
74
vendor/github.com/go-openapi/strfmt/CODE_OF_CONDUCT.md
generated
vendored
Normal file
74
vendor/github.com/go-openapi/strfmt/CODE_OF_CONDUCT.md
generated
vendored
Normal file
@ -0,0 +1,74 @@
|
||||
# Contributor Covenant Code of Conduct
|
||||
|
||||
## Our Pledge
|
||||
|
||||
In the interest of fostering an open and welcoming environment, we as
|
||||
contributors and maintainers pledge to making participation in our project and
|
||||
our community a harassment-free experience for everyone, regardless of age, body
|
||||
size, disability, ethnicity, gender identity and expression, level of experience,
|
||||
nationality, personal appearance, race, religion, or sexual identity and
|
||||
orientation.
|
||||
|
||||
## Our Standards
|
||||
|
||||
Examples of behavior that contributes to creating a positive environment
|
||||
include:
|
||||
|
||||
* Using welcoming and inclusive language
|
||||
* Being respectful of differing viewpoints and experiences
|
||||
* Gracefully accepting constructive criticism
|
||||
* Focusing on what is best for the community
|
||||
* Showing empathy towards other community members
|
||||
|
||||
Examples of unacceptable behavior by participants include:
|
||||
|
||||
* The use of sexualized language or imagery and unwelcome sexual attention or
|
||||
advances
|
||||
* Trolling, insulting/derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or electronic
|
||||
address, without explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a
|
||||
professional setting
|
||||
|
||||
## Our Responsibilities
|
||||
|
||||
Project maintainers are responsible for clarifying the standards of acceptable
|
||||
behavior and are expected to take appropriate and fair corrective action in
|
||||
response to any instances of unacceptable behavior.
|
||||
|
||||
Project maintainers have the right and responsibility to remove, edit, or
|
||||
reject comments, commits, code, wiki edits, issues, and other contributions
|
||||
that are not aligned to this Code of Conduct, or to ban temporarily or
|
||||
permanently any contributor for other behaviors that they deem inappropriate,
|
||||
threatening, offensive, or harmful.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies both within project spaces and in public spaces
|
||||
when an individual is representing the project or its community. Examples of
|
||||
representing a project or community include using an official project e-mail
|
||||
address, posting via an official social media account, or acting as an appointed
|
||||
representative at an online or offline event. Representation of a project may be
|
||||
further defined and clarified by project maintainers.
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
||||
reported by contacting the project team at ivan+abuse@flanders.co.nz. All
|
||||
complaints will be reviewed and investigated and will result in a response that
|
||||
is deemed necessary and appropriate to the circumstances. The project team is
|
||||
obligated to maintain confidentiality with regard to the reporter of an incident.
|
||||
Further details of specific enforcement policies may be posted separately.
|
||||
|
||||
Project maintainers who do not follow or enforce the Code of Conduct in good
|
||||
faith may face temporary or permanent repercussions as determined by other
|
||||
members of the project's leadership.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
||||
available at [http://contributor-covenant.org/version/1/4][version]
|
||||
|
||||
[homepage]: http://contributor-covenant.org
|
||||
[version]: http://contributor-covenant.org/version/1/4/
|
202
vendor/github.com/go-openapi/strfmt/LICENSE
generated
vendored
Normal file
202
vendor/github.com/go-openapi/strfmt/LICENSE
generated
vendored
Normal file
@ -0,0 +1,202 @@
|
||||
|
||||
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.
|
87
vendor/github.com/go-openapi/strfmt/README.md
generated
vendored
Normal file
87
vendor/github.com/go-openapi/strfmt/README.md
generated
vendored
Normal file
@ -0,0 +1,87 @@
|
||||
# Strfmt [](https://github.com/go-openapi/strfmt/actions?query=workflow%3A"go+test") [](https://codecov.io/gh/go-openapi/strfmt)
|
||||
[](https://slackin.goswagger.io)
|
||||
[](https://raw.githubusercontent.com/go-openapi/strfmt/master/LICENSE)
|
||||
[](http://godoc.org/github.com/go-openapi/strfmt)
|
||||
[](https://goreportcard.com/report/github.com/go-openapi/strfmt)
|
||||
|
||||
This package exposes a registry of data types to support string formats in the go-openapi toolkit.
|
||||
|
||||
strfmt represents a well known string format such as credit card or email. The go toolkit for OpenAPI specifications knows how to deal with those.
|
||||
|
||||
## Supported data formats
|
||||
go-openapi/strfmt follows the swagger 2.0 specification with the following formats
|
||||
defined [here](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#data-types).
|
||||
|
||||
It also provides convenient extensions to go-openapi users.
|
||||
|
||||
- [x] JSON-schema draft 4 formats
|
||||
- date-time
|
||||
- email
|
||||
- hostname
|
||||
- ipv4
|
||||
- ipv6
|
||||
- uri
|
||||
- [x] swagger 2.0 format extensions
|
||||
- binary
|
||||
- byte (e.g. base64 encoded string)
|
||||
- date (e.g. "1970-01-01")
|
||||
- password
|
||||
- [x] go-openapi custom format extensions
|
||||
- bsonobjectid (BSON objectID)
|
||||
- creditcard
|
||||
- duration (e.g. "3 weeks", "1ms")
|
||||
- hexcolor (e.g. "#FFFFFF")
|
||||
- isbn, isbn10, isbn13
|
||||
- mac (e.g "01:02:03:04:05:06")
|
||||
- rgbcolor (e.g. "rgb(100,100,100)")
|
||||
- ssn
|
||||
- uuid, uuid3, uuid4, uuid5
|
||||
- cidr (e.g. "192.0.2.1/24", "2001:db8:a0b:12f0::1/32")
|
||||
- ulid (e.g. "00000PP9HGSBSSDZ1JTEXBJ0PW", [spec](https://github.com/ulid/spec))
|
||||
|
||||
> NOTE: as the name stands for, this package is intended to support string formatting only.
|
||||
> It does not provide validation for numerical values with swagger format extension for JSON types "number" or
|
||||
> "integer" (e.g. float, double, int32...).
|
||||
|
||||
## Type conversion
|
||||
|
||||
All types defined here are stringers and may be converted to strings with `.String()`.
|
||||
Note that most types defined by this package may be converted directly to string like `string(Email{})`.
|
||||
|
||||
`Date` and `DateTime` may be converted directly to `time.Time` like `time.Time(Time{})`.
|
||||
Similarly, you can convert `Duration` to `time.Duration` as in `time.Duration(Duration{})`
|
||||
|
||||
## Using pointers
|
||||
|
||||
The `conv` subpackage provides helpers to convert the types to and from pointers, just like `go-openapi/swag` does
|
||||
with primitive types.
|
||||
|
||||
## Format types
|
||||
Types defined in strfmt expose marshaling and validation capabilities.
|
||||
|
||||
List of defined types:
|
||||
- Base64
|
||||
- CreditCard
|
||||
- Date
|
||||
- DateTime
|
||||
- Duration
|
||||
- Email
|
||||
- HexColor
|
||||
- Hostname
|
||||
- IPv4
|
||||
- IPv6
|
||||
- CIDR
|
||||
- ISBN
|
||||
- ISBN10
|
||||
- ISBN13
|
||||
- MAC
|
||||
- ObjectId
|
||||
- Password
|
||||
- RGBColor
|
||||
- SSN
|
||||
- URI
|
||||
- UUID
|
||||
- UUID3
|
||||
- UUID4
|
||||
- UUID5
|
||||
- [ULID](https://github.com/ulid/spec)
|
165
vendor/github.com/go-openapi/strfmt/bson.go
generated
vendored
Normal file
165
vendor/github.com/go-openapi/strfmt/bson.go
generated
vendored
Normal file
@ -0,0 +1,165 @@
|
||||
// Copyright 2015 go-swagger maintainers
|
||||
//
|
||||
// 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 strfmt
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/bsontype"
|
||||
bsonprim "go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
func init() {
|
||||
var id ObjectId
|
||||
// register this format in the default registry
|
||||
Default.Add("bsonobjectid", &id, IsBSONObjectID)
|
||||
}
|
||||
|
||||
// IsBSONObjectID returns true when the string is a valid BSON.ObjectId
|
||||
func IsBSONObjectID(str string) bool {
|
||||
_, err := bsonprim.ObjectIDFromHex(str)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// ObjectId represents a BSON object ID (alias to go.mongodb.org/mongo-driver/bson/primitive.ObjectID)
|
||||
//
|
||||
// swagger:strfmt bsonobjectid
|
||||
type ObjectId bsonprim.ObjectID //nolint:revive,stylecheck
|
||||
|
||||
// NewObjectId creates a ObjectId from a Hex String
|
||||
func NewObjectId(hex string) ObjectId { //nolint:revive,stylecheck
|
||||
oid, err := bsonprim.ObjectIDFromHex(hex)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return ObjectId(oid)
|
||||
}
|
||||
|
||||
// MarshalText turns this instance into text
|
||||
func (id ObjectId) MarshalText() ([]byte, error) {
|
||||
oid := bsonprim.ObjectID(id)
|
||||
if oid == bsonprim.NilObjectID {
|
||||
return nil, nil
|
||||
}
|
||||
return []byte(oid.Hex()), nil
|
||||
}
|
||||
|
||||
// UnmarshalText hydrates this instance from text
|
||||
func (id *ObjectId) UnmarshalText(data []byte) error { // validation is performed later on
|
||||
if len(data) == 0 {
|
||||
*id = ObjectId(bsonprim.NilObjectID)
|
||||
return nil
|
||||
}
|
||||
oidstr := string(data)
|
||||
oid, err := bsonprim.ObjectIDFromHex(oidstr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*id = ObjectId(oid)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Scan read a value from a database driver
|
||||
func (id *ObjectId) Scan(raw interface{}) error {
|
||||
var data []byte
|
||||
switch v := raw.(type) {
|
||||
case []byte:
|
||||
data = v
|
||||
case string:
|
||||
data = []byte(v)
|
||||
default:
|
||||
return fmt.Errorf("cannot sql.Scan() strfmt.URI from: %#v", v)
|
||||
}
|
||||
|
||||
return id.UnmarshalText(data)
|
||||
}
|
||||
|
||||
// Value converts a value to a database driver value
|
||||
func (id ObjectId) Value() (driver.Value, error) {
|
||||
return driver.Value(bsonprim.ObjectID(id).Hex()), nil
|
||||
}
|
||||
|
||||
func (id ObjectId) String() string {
|
||||
return bsonprim.ObjectID(id).Hex()
|
||||
}
|
||||
|
||||
// MarshalJSON returns the ObjectId as JSON
|
||||
func (id ObjectId) MarshalJSON() ([]byte, error) {
|
||||
return bsonprim.ObjectID(id).MarshalJSON()
|
||||
}
|
||||
|
||||
// UnmarshalJSON sets the ObjectId from JSON
|
||||
func (id *ObjectId) UnmarshalJSON(data []byte) error {
|
||||
var obj bsonprim.ObjectID
|
||||
if err := obj.UnmarshalJSON(data); err != nil {
|
||||
return err
|
||||
}
|
||||
*id = ObjectId(obj)
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalBSON renders the object id as a BSON document
|
||||
func (id ObjectId) MarshalBSON() ([]byte, error) {
|
||||
return bson.Marshal(bson.M{"data": bsonprim.ObjectID(id)})
|
||||
}
|
||||
|
||||
// UnmarshalBSON reads the objectId from a BSON document
|
||||
func (id *ObjectId) UnmarshalBSON(data []byte) error {
|
||||
var obj struct {
|
||||
Data bsonprim.ObjectID
|
||||
}
|
||||
if err := bson.Unmarshal(data, &obj); err != nil {
|
||||
return err
|
||||
}
|
||||
*id = ObjectId(obj.Data)
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalBSONValue is an interface implemented by types that can marshal themselves
|
||||
// into a BSON document represented as bytes. The bytes returned must be a valid
|
||||
// BSON document if the error is nil.
|
||||
func (id ObjectId) MarshalBSONValue() (bsontype.Type, []byte, error) {
|
||||
oid := bsonprim.ObjectID(id)
|
||||
return bson.TypeObjectID, oid[:], nil
|
||||
}
|
||||
|
||||
// UnmarshalBSONValue is an interface implemented by types that can unmarshal a
|
||||
// BSON value representation of themselves. The BSON bytes and type can be
|
||||
// assumed to be valid. UnmarshalBSONValue must copy the BSON value bytes if it
|
||||
// wishes to retain the data after returning.
|
||||
func (id *ObjectId) UnmarshalBSONValue(_ bsontype.Type, data []byte) error {
|
||||
var oid bsonprim.ObjectID
|
||||
copy(oid[:], data)
|
||||
*id = ObjectId(oid)
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeepCopyInto copies the receiver and writes its value into out.
|
||||
func (id *ObjectId) DeepCopyInto(out *ObjectId) {
|
||||
*out = *id
|
||||
}
|
||||
|
||||
// DeepCopy copies the receiver into a new ObjectId.
|
||||
func (id *ObjectId) DeepCopy() *ObjectId {
|
||||
if id == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(ObjectId)
|
||||
id.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
187
vendor/github.com/go-openapi/strfmt/date.go
generated
vendored
Normal file
187
vendor/github.com/go-openapi/strfmt/date.go
generated
vendored
Normal file
@ -0,0 +1,187 @@
|
||||
// Copyright 2015 go-swagger maintainers
|
||||
//
|
||||
// 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 strfmt
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
func init() {
|
||||
d := Date{}
|
||||
// register this format in the default registry
|
||||
Default.Add("date", &d, IsDate)
|
||||
}
|
||||
|
||||
// IsDate returns true when the string is a valid date
|
||||
func IsDate(str string) bool {
|
||||
_, err := time.Parse(RFC3339FullDate, str)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
const (
|
||||
// RFC3339FullDate represents a full-date as specified by RFC3339
|
||||
// See: http://goo.gl/xXOvVd
|
||||
RFC3339FullDate = "2006-01-02"
|
||||
)
|
||||
|
||||
// Date represents a date from the API
|
||||
//
|
||||
// swagger:strfmt date
|
||||
type Date time.Time
|
||||
|
||||
// String converts this date into a string
|
||||
func (d Date) String() string {
|
||||
return time.Time(d).Format(RFC3339FullDate)
|
||||
}
|
||||
|
||||
// UnmarshalText parses a text representation into a date type
|
||||
func (d *Date) UnmarshalText(text []byte) error {
|
||||
if len(text) == 0 {
|
||||
return nil
|
||||
}
|
||||
dd, err := time.ParseInLocation(RFC3339FullDate, string(text), DefaultTimeLocation)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*d = Date(dd)
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalText serializes this date type to string
|
||||
func (d Date) MarshalText() ([]byte, error) {
|
||||
return []byte(d.String()), nil
|
||||
}
|
||||
|
||||
// Scan scans a Date value from database driver type.
|
||||
func (d *Date) Scan(raw interface{}) error {
|
||||
switch v := raw.(type) {
|
||||
case []byte:
|
||||
return d.UnmarshalText(v)
|
||||
case string:
|
||||
return d.UnmarshalText([]byte(v))
|
||||
case time.Time:
|
||||
*d = Date(v)
|
||||
return nil
|
||||
case nil:
|
||||
*d = Date{}
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("cannot sql.Scan() strfmt.Date from: %#v", v)
|
||||
}
|
||||
}
|
||||
|
||||
// Value converts Date to a primitive value ready to written to a database.
|
||||
func (d Date) Value() (driver.Value, error) {
|
||||
return driver.Value(d.String()), nil
|
||||
}
|
||||
|
||||
// MarshalJSON returns the Date as JSON
|
||||
func (d Date) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(time.Time(d).Format(RFC3339FullDate))
|
||||
}
|
||||
|
||||
// UnmarshalJSON sets the Date from JSON
|
||||
func (d *Date) UnmarshalJSON(data []byte) error {
|
||||
if string(data) == jsonNull {
|
||||
return nil
|
||||
}
|
||||
var strdate string
|
||||
if err := json.Unmarshal(data, &strdate); err != nil {
|
||||
return err
|
||||
}
|
||||
tt, err := time.ParseInLocation(RFC3339FullDate, strdate, DefaultTimeLocation)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*d = Date(tt)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Date) MarshalBSON() ([]byte, error) {
|
||||
return bson.Marshal(bson.M{"data": d.String()})
|
||||
}
|
||||
|
||||
func (d *Date) UnmarshalBSON(data []byte) error {
|
||||
var m bson.M
|
||||
if err := bson.Unmarshal(data, &m); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if data, ok := m["data"].(string); ok {
|
||||
rd, err := time.ParseInLocation(RFC3339FullDate, data, DefaultTimeLocation)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*d = Date(rd)
|
||||
return nil
|
||||
}
|
||||
|
||||
return errors.New("couldn't unmarshal bson bytes value as Date")
|
||||
}
|
||||
|
||||
// DeepCopyInto copies the receiver and writes its value into out.
|
||||
func (d *Date) DeepCopyInto(out *Date) {
|
||||
*out = *d
|
||||
}
|
||||
|
||||
// DeepCopy copies the receiver into a new Date.
|
||||
func (d *Date) DeepCopy() *Date {
|
||||
if d == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(Date)
|
||||
d.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// GobEncode implements the gob.GobEncoder interface.
|
||||
func (d Date) GobEncode() ([]byte, error) {
|
||||
return d.MarshalBinary()
|
||||
}
|
||||
|
||||
// GobDecode implements the gob.GobDecoder interface.
|
||||
func (d *Date) GobDecode(data []byte) error {
|
||||
return d.UnmarshalBinary(data)
|
||||
}
|
||||
|
||||
// MarshalBinary implements the encoding.BinaryMarshaler interface.
|
||||
func (d Date) MarshalBinary() ([]byte, error) {
|
||||
return time.Time(d).MarshalBinary()
|
||||
}
|
||||
|
||||
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
|
||||
func (d *Date) UnmarshalBinary(data []byte) error {
|
||||
var original time.Time
|
||||
|
||||
err := original.UnmarshalBinary(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*d = Date(original)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Equal checks if two Date instances are equal
|
||||
func (d Date) Equal(d2 Date) bool {
|
||||
return time.Time(d).Equal(time.Time(d2))
|
||||
}
|
2051
vendor/github.com/go-openapi/strfmt/default.go
generated
vendored
Normal file
2051
vendor/github.com/go-openapi/strfmt/default.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
18
vendor/github.com/go-openapi/strfmt/doc.go
generated
vendored
Normal file
18
vendor/github.com/go-openapi/strfmt/doc.go
generated
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
// Copyright 2015 go-swagger maintainers
|
||||
//
|
||||
// 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 strfmt contains custom string formats
|
||||
//
|
||||
// TODO: add info on how to define and register a custom format
|
||||
package strfmt
|
211
vendor/github.com/go-openapi/strfmt/duration.go
generated
vendored
Normal file
211
vendor/github.com/go-openapi/strfmt/duration.go
generated
vendored
Normal file
@ -0,0 +1,211 @@
|
||||
// Copyright 2015 go-swagger maintainers
|
||||
//
|
||||
// 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 strfmt
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
func init() {
|
||||
d := Duration(0)
|
||||
// register this format in the default registry
|
||||
Default.Add("duration", &d, IsDuration)
|
||||
}
|
||||
|
||||
var (
|
||||
timeUnits = [][]string{
|
||||
{"ns", "nano"},
|
||||
{"us", "µs", "micro"},
|
||||
{"ms", "milli"},
|
||||
{"s", "sec"},
|
||||
{"m", "min"},
|
||||
{"h", "hr", "hour"},
|
||||
{"d", "day"},
|
||||
{"w", "wk", "week"},
|
||||
}
|
||||
|
||||
timeMultiplier = map[string]time.Duration{
|
||||
"ns": time.Nanosecond,
|
||||
"us": time.Microsecond,
|
||||
"ms": time.Millisecond,
|
||||
"s": time.Second,
|
||||
"m": time.Minute,
|
||||
"h": time.Hour,
|
||||
"d": 24 * time.Hour,
|
||||
"w": 7 * 24 * time.Hour,
|
||||
}
|
||||
|
||||
durationMatcher = regexp.MustCompile(`((\d+)\s*([A-Za-zµ]+))`)
|
||||
)
|
||||
|
||||
// IsDuration returns true if the provided string is a valid duration
|
||||
func IsDuration(str string) bool {
|
||||
_, err := ParseDuration(str)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// Duration represents a duration
|
||||
//
|
||||
// Duration stores a period of time as a nanosecond count, with the largest
|
||||
// repesentable duration being approximately 290 years.
|
||||
//
|
||||
// swagger:strfmt duration
|
||||
type Duration time.Duration
|
||||
|
||||
// MarshalText turns this instance into text
|
||||
func (d Duration) MarshalText() ([]byte, error) {
|
||||
return []byte(time.Duration(d).String()), nil
|
||||
}
|
||||
|
||||
// UnmarshalText hydrates this instance from text
|
||||
func (d *Duration) UnmarshalText(data []byte) error { // validation is performed later on
|
||||
dd, err := ParseDuration(string(data))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*d = Duration(dd)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ParseDuration parses a duration from a string, compatible with scala duration syntax
|
||||
func ParseDuration(cand string) (time.Duration, error) {
|
||||
if dur, err := time.ParseDuration(cand); err == nil {
|
||||
return dur, nil
|
||||
}
|
||||
|
||||
var dur time.Duration
|
||||
ok := false
|
||||
for _, match := range durationMatcher.FindAllStringSubmatch(cand, -1) {
|
||||
|
||||
factor, err := strconv.Atoi(match[2]) // converts string to int
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
unit := strings.ToLower(strings.TrimSpace(match[3]))
|
||||
|
||||
for _, variants := range timeUnits {
|
||||
last := len(variants) - 1
|
||||
multiplier := timeMultiplier[variants[0]]
|
||||
|
||||
for i, variant := range variants {
|
||||
if (last == i && strings.HasPrefix(unit, variant)) || strings.EqualFold(variant, unit) {
|
||||
ok = true
|
||||
dur += (time.Duration(factor) * multiplier)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ok {
|
||||
return dur, nil
|
||||
}
|
||||
return 0, fmt.Errorf("unable to parse %s as duration", cand)
|
||||
}
|
||||
|
||||
// Scan reads a Duration value from database driver type.
|
||||
func (d *Duration) Scan(raw interface{}) error {
|
||||
switch v := raw.(type) {
|
||||
// TODO: case []byte: // ?
|
||||
case int64:
|
||||
*d = Duration(v)
|
||||
case float64:
|
||||
*d = Duration(int64(v))
|
||||
case nil:
|
||||
*d = Duration(0)
|
||||
default:
|
||||
return fmt.Errorf("cannot sql.Scan() strfmt.Duration from: %#v", v)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value converts Duration to a primitive value ready to be written to a database.
|
||||
func (d Duration) Value() (driver.Value, error) {
|
||||
return driver.Value(int64(d)), nil
|
||||
}
|
||||
|
||||
// String converts this duration to a string
|
||||
func (d Duration) String() string {
|
||||
return time.Duration(d).String()
|
||||
}
|
||||
|
||||
// MarshalJSON returns the Duration as JSON
|
||||
func (d Duration) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(time.Duration(d).String())
|
||||
}
|
||||
|
||||
// UnmarshalJSON sets the Duration from JSON
|
||||
func (d *Duration) UnmarshalJSON(data []byte) error {
|
||||
if string(data) == jsonNull {
|
||||
return nil
|
||||
}
|
||||
|
||||
var dstr string
|
||||
if err := json.Unmarshal(data, &dstr); err != nil {
|
||||
return err
|
||||
}
|
||||
tt, err := ParseDuration(dstr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*d = Duration(tt)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d Duration) MarshalBSON() ([]byte, error) {
|
||||
return bson.Marshal(bson.M{"data": d.String()})
|
||||
}
|
||||
|
||||
func (d *Duration) UnmarshalBSON(data []byte) error {
|
||||
var m bson.M
|
||||
if err := bson.Unmarshal(data, &m); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if data, ok := m["data"].(string); ok {
|
||||
rd, err := ParseDuration(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*d = Duration(rd)
|
||||
return nil
|
||||
}
|
||||
|
||||
return errors.New("couldn't unmarshal bson bytes value as Date")
|
||||
}
|
||||
|
||||
// DeepCopyInto copies the receiver and writes its value into out.
|
||||
func (d *Duration) DeepCopyInto(out *Duration) {
|
||||
*out = *d
|
||||
}
|
||||
|
||||
// DeepCopy copies the receiver into a new Duration.
|
||||
func (d *Duration) DeepCopy() *Duration {
|
||||
if d == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(Duration)
|
||||
d.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
327
vendor/github.com/go-openapi/strfmt/format.go
generated
vendored
Normal file
327
vendor/github.com/go-openapi/strfmt/format.go
generated
vendored
Normal file
@ -0,0 +1,327 @@
|
||||
// Copyright 2015 go-swagger maintainers
|
||||
//
|
||||
// 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 strfmt
|
||||
|
||||
import (
|
||||
"encoding"
|
||||
stderrors "errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-openapi/errors"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
// Default is the default formats registry
|
||||
var Default = NewSeededFormats(nil, nil)
|
||||
|
||||
// Validator represents a validator for a string format.
|
||||
type Validator func(string) bool
|
||||
|
||||
// Format represents a string format.
|
||||
//
|
||||
// All implementations of Format provide a string representation and text
|
||||
// marshaling/unmarshaling interface to be used by encoders (e.g. encoding/json).
|
||||
type Format interface {
|
||||
String() string
|
||||
encoding.TextMarshaler
|
||||
encoding.TextUnmarshaler
|
||||
}
|
||||
|
||||
// Registry is a registry of string formats, with a validation method.
|
||||
type Registry interface {
|
||||
Add(string, Format, Validator) bool
|
||||
DelByName(string) bool
|
||||
GetType(string) (reflect.Type, bool)
|
||||
ContainsName(string) bool
|
||||
Validates(string, string) bool
|
||||
Parse(string, string) (interface{}, error)
|
||||
MapStructureHookFunc() mapstructure.DecodeHookFunc
|
||||
}
|
||||
|
||||
type knownFormat struct {
|
||||
Name string
|
||||
OrigName string
|
||||
Type reflect.Type
|
||||
Validator Validator
|
||||
}
|
||||
|
||||
// NameNormalizer is a function that normalizes a format name.
|
||||
type NameNormalizer func(string) string
|
||||
|
||||
// DefaultNameNormalizer removes all dashes
|
||||
func DefaultNameNormalizer(name string) string {
|
||||
return strings.ReplaceAll(name, "-", "")
|
||||
}
|
||||
|
||||
type defaultFormats struct {
|
||||
sync.Mutex
|
||||
data []knownFormat
|
||||
normalizeName NameNormalizer
|
||||
}
|
||||
|
||||
// NewFormats creates a new formats registry seeded with the values from the default
|
||||
func NewFormats() Registry {
|
||||
//nolint:forcetypeassert
|
||||
return NewSeededFormats(Default.(*defaultFormats).data, nil)
|
||||
}
|
||||
|
||||
// NewSeededFormats creates a new formats registry
|
||||
func NewSeededFormats(seeds []knownFormat, normalizer NameNormalizer) Registry {
|
||||
if normalizer == nil {
|
||||
normalizer = DefaultNameNormalizer
|
||||
}
|
||||
// copy here, don't modify original
|
||||
d := append([]knownFormat(nil), seeds...)
|
||||
return &defaultFormats{
|
||||
data: d,
|
||||
normalizeName: normalizer,
|
||||
}
|
||||
}
|
||||
|
||||
// MapStructureHookFunc is a decode hook function for mapstructure
|
||||
func (f *defaultFormats) MapStructureHookFunc() mapstructure.DecodeHookFunc {
|
||||
return func(from reflect.Type, to reflect.Type, obj interface{}) (interface{}, error) {
|
||||
if from.Kind() != reflect.String {
|
||||
return obj, nil
|
||||
}
|
||||
data, ok := obj.(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("failed to cast %+v to string", obj)
|
||||
}
|
||||
|
||||
for _, v := range f.data {
|
||||
tpe, _ := f.GetType(v.Name)
|
||||
if to == tpe {
|
||||
switch v.Name {
|
||||
case "date":
|
||||
d, err := time.ParseInLocation(RFC3339FullDate, data, DefaultTimeLocation)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return Date(d), nil
|
||||
case "datetime":
|
||||
input := data
|
||||
if len(input) == 0 {
|
||||
return nil, stderrors.New("empty string is an invalid datetime format")
|
||||
}
|
||||
return ParseDateTime(input)
|
||||
case "duration":
|
||||
dur, err := ParseDuration(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return Duration(dur), nil
|
||||
case "uri":
|
||||
return URI(data), nil
|
||||
case "email":
|
||||
return Email(data), nil
|
||||
case "uuid":
|
||||
return UUID(data), nil
|
||||
case "uuid3":
|
||||
return UUID3(data), nil
|
||||
case "uuid4":
|
||||
return UUID4(data), nil
|
||||
case "uuid5":
|
||||
return UUID5(data), nil
|
||||
case "hostname":
|
||||
return Hostname(data), nil
|
||||
case "ipv4":
|
||||
return IPv4(data), nil
|
||||
case "ipv6":
|
||||
return IPv6(data), nil
|
||||
case "cidr":
|
||||
return CIDR(data), nil
|
||||
case "mac":
|
||||
return MAC(data), nil
|
||||
case "isbn":
|
||||
return ISBN(data), nil
|
||||
case "isbn10":
|
||||
return ISBN10(data), nil
|
||||
case "isbn13":
|
||||
return ISBN13(data), nil
|
||||
case "creditcard":
|
||||
return CreditCard(data), nil
|
||||
case "ssn":
|
||||
return SSN(data), nil
|
||||
case "hexcolor":
|
||||
return HexColor(data), nil
|
||||
case "rgbcolor":
|
||||
return RGBColor(data), nil
|
||||
case "byte":
|
||||
return Base64(data), nil
|
||||
case "password":
|
||||
return Password(data), nil
|
||||
case "ulid":
|
||||
ulid, err := ParseULID(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ulid, nil
|
||||
default:
|
||||
return nil, errors.InvalidTypeName(v.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Add adds a new format, return true if this was a new item instead of a replacement
|
||||
func (f *defaultFormats) Add(name string, strfmt Format, validator Validator) bool {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
|
||||
nme := f.normalizeName(name)
|
||||
|
||||
tpe := reflect.TypeOf(strfmt)
|
||||
if tpe.Kind() == reflect.Ptr {
|
||||
tpe = tpe.Elem()
|
||||
}
|
||||
|
||||
for i := range f.data {
|
||||
v := &f.data[i]
|
||||
if v.Name == nme {
|
||||
v.Type = tpe
|
||||
v.Validator = validator
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// turns out it's new after all
|
||||
f.data = append(f.data, knownFormat{Name: nme, OrigName: name, Type: tpe, Validator: validator})
|
||||
return true
|
||||
}
|
||||
|
||||
// GetType gets the type for the specified name
|
||||
func (f *defaultFormats) GetType(name string) (reflect.Type, bool) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
nme := f.normalizeName(name)
|
||||
for _, v := range f.data {
|
||||
if v.Name == nme {
|
||||
return v.Type, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// DelByName removes the format by the specified name, returns true when an item was actually removed
|
||||
func (f *defaultFormats) DelByName(name string) bool {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
|
||||
nme := f.normalizeName(name)
|
||||
|
||||
for i, v := range f.data {
|
||||
if v.Name == nme {
|
||||
f.data[i] = knownFormat{} // release
|
||||
f.data = append(f.data[:i], f.data[i+1:]...)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// DelByFormat removes the specified format, returns true when an item was actually removed
|
||||
func (f *defaultFormats) DelByFormat(strfmt Format) bool {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
|
||||
tpe := reflect.TypeOf(strfmt)
|
||||
if tpe.Kind() == reflect.Ptr {
|
||||
tpe = tpe.Elem()
|
||||
}
|
||||
|
||||
for i, v := range f.data {
|
||||
if v.Type == tpe {
|
||||
f.data[i] = knownFormat{} // release
|
||||
f.data = append(f.data[:i], f.data[i+1:]...)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ContainsName returns true if this registry contains the specified name
|
||||
func (f *defaultFormats) ContainsName(name string) bool {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
nme := f.normalizeName(name)
|
||||
for _, v := range f.data {
|
||||
if v.Name == nme {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// ContainsFormat returns true if this registry contains the specified format
|
||||
func (f *defaultFormats) ContainsFormat(strfmt Format) bool {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
tpe := reflect.TypeOf(strfmt)
|
||||
if tpe.Kind() == reflect.Ptr {
|
||||
tpe = tpe.Elem()
|
||||
}
|
||||
|
||||
for _, v := range f.data {
|
||||
if v.Type == tpe {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Validates passed data against format.
|
||||
//
|
||||
// Note that the format name is automatically normalized, e.g. one may
|
||||
// use "date-time" to use the "datetime" format validator.
|
||||
func (f *defaultFormats) Validates(name, data string) bool {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
nme := f.normalizeName(name)
|
||||
for _, v := range f.data {
|
||||
if v.Name == nme {
|
||||
return v.Validator(data)
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Parse a string into the appropriate format representation type.
|
||||
//
|
||||
// E.g. parsing a string a "date" will return a Date type.
|
||||
func (f *defaultFormats) Parse(name, data string) (interface{}, error) {
|
||||
f.Lock()
|
||||
defer f.Unlock()
|
||||
nme := f.normalizeName(name)
|
||||
for _, v := range f.data {
|
||||
if v.Name == nme {
|
||||
nw := reflect.New(v.Type).Interface()
|
||||
if dec, ok := nw.(encoding.TextUnmarshaler); ok {
|
||||
if err := dec.UnmarshalText([]byte(data)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nw, nil
|
||||
}
|
||||
return nil, errors.InvalidTypeName(name)
|
||||
}
|
||||
}
|
||||
return nil, errors.InvalidTypeName(name)
|
||||
}
|
321
vendor/github.com/go-openapi/strfmt/time.go
generated
vendored
Normal file
321
vendor/github.com/go-openapi/strfmt/time.go
generated
vendored
Normal file
@ -0,0 +1,321 @@
|
||||
// Copyright 2015 go-swagger maintainers
|
||||
//
|
||||
// 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 strfmt
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/bsontype"
|
||||
)
|
||||
|
||||
var (
|
||||
// UnixZero sets the zero unix timestamp we want to compare against.
|
||||
// Unix 0 for an EST timezone is not equivalent to a UTC timezone.
|
||||
UnixZero = time.Unix(0, 0).UTC()
|
||||
)
|
||||
|
||||
func init() {
|
||||
dt := DateTime{}
|
||||
Default.Add("datetime", &dt, IsDateTime)
|
||||
}
|
||||
|
||||
// IsDateTime returns true when the string is a valid date-time
|
||||
func IsDateTime(str string) bool {
|
||||
if len(str) < 4 {
|
||||
return false
|
||||
}
|
||||
s := strings.Split(strings.ToLower(str), "t")
|
||||
if len(s) < 2 || !IsDate(s[0]) {
|
||||
return false
|
||||
}
|
||||
|
||||
matches := rxDateTime.FindAllStringSubmatch(s[1], -1)
|
||||
if len(matches) == 0 || len(matches[0]) == 0 {
|
||||
return false
|
||||
}
|
||||
m := matches[0]
|
||||
res := m[1] <= "23" && m[2] <= "59" && m[3] <= "59"
|
||||
return res
|
||||
}
|
||||
|
||||
const (
|
||||
// RFC3339Millis represents a ISO8601 format to millis instead of to nanos
|
||||
RFC3339Millis = "2006-01-02T15:04:05.000Z07:00"
|
||||
// RFC3339MillisNoColon represents a ISO8601 format to millis instead of to nanos
|
||||
RFC3339MillisNoColon = "2006-01-02T15:04:05.000Z0700"
|
||||
// RFC3339Micro represents a ISO8601 format to micro instead of to nano
|
||||
RFC3339Micro = "2006-01-02T15:04:05.000000Z07:00"
|
||||
// RFC3339MicroNoColon represents a ISO8601 format to micro instead of to nano
|
||||
RFC3339MicroNoColon = "2006-01-02T15:04:05.000000Z0700"
|
||||
// ISO8601LocalTime represents a ISO8601 format to ISO8601 in local time (no timezone)
|
||||
ISO8601LocalTime = "2006-01-02T15:04:05"
|
||||
// ISO8601TimeWithReducedPrecision represents a ISO8601 format with reduced precision (dropped secs)
|
||||
ISO8601TimeWithReducedPrecision = "2006-01-02T15:04Z"
|
||||
// ISO8601TimeWithReducedPrecisionLocaltime represents a ISO8601 format with reduced precision and no timezone (dropped seconds + no timezone)
|
||||
ISO8601TimeWithReducedPrecisionLocaltime = "2006-01-02T15:04"
|
||||
// ISO8601TimeUniversalSortableDateTimePattern represents a ISO8601 universal sortable date time pattern.
|
||||
ISO8601TimeUniversalSortableDateTimePattern = "2006-01-02 15:04:05"
|
||||
// short form of ISO8601TimeUniversalSortableDateTimePattern
|
||||
ISO8601TimeUniversalSortableDateTimePatternShortForm = "2006-01-02"
|
||||
// DateTimePattern pattern to match for the date-time format from http://tools.ietf.org/html/rfc3339#section-5.6
|
||||
DateTimePattern = `^([0-9]{2}):([0-9]{2}):([0-9]{2})(.[0-9]+)?(z|([+-][0-9]{2}:[0-9]{2}))$`
|
||||
)
|
||||
|
||||
var (
|
||||
rxDateTime = regexp.MustCompile(DateTimePattern)
|
||||
|
||||
// DateTimeFormats is the collection of formats used by ParseDateTime()
|
||||
DateTimeFormats = []string{RFC3339Micro, RFC3339MicroNoColon, RFC3339Millis, RFC3339MillisNoColon, time.RFC3339, time.RFC3339Nano, ISO8601LocalTime, ISO8601TimeWithReducedPrecision, ISO8601TimeWithReducedPrecisionLocaltime, ISO8601TimeUniversalSortableDateTimePattern, ISO8601TimeUniversalSortableDateTimePatternShortForm}
|
||||
|
||||
// MarshalFormat sets the time resolution format used for marshaling time (set to milliseconds)
|
||||
MarshalFormat = RFC3339Millis
|
||||
|
||||
// NormalizeTimeForMarshal provides a normalization function on time befeore marshalling (e.g. time.UTC).
|
||||
// By default, the time value is not changed.
|
||||
NormalizeTimeForMarshal = func(t time.Time) time.Time { return t }
|
||||
|
||||
// DefaultTimeLocation provides a location for a time when the time zone is not encoded in the string (ex: ISO8601 Local variants).
|
||||
DefaultTimeLocation = time.UTC
|
||||
)
|
||||
|
||||
// ParseDateTime parses a string that represents an ISO8601 time or a unix epoch
|
||||
func ParseDateTime(data string) (DateTime, error) {
|
||||
if data == "" {
|
||||
return NewDateTime(), nil
|
||||
}
|
||||
var lastError error
|
||||
for _, layout := range DateTimeFormats {
|
||||
dd, err := time.ParseInLocation(layout, data, DefaultTimeLocation)
|
||||
if err != nil {
|
||||
lastError = err
|
||||
continue
|
||||
}
|
||||
return DateTime(dd), nil
|
||||
}
|
||||
return DateTime{}, lastError
|
||||
}
|
||||
|
||||
// DateTime is a time but it serializes to ISO8601 format with millis
|
||||
// It knows how to read 3 different variations of a RFC3339 date time.
|
||||
// Most APIs we encounter want either millisecond or second precision times.
|
||||
// This just tries to make it worry-free.
|
||||
//
|
||||
// swagger:strfmt date-time
|
||||
type DateTime time.Time
|
||||
|
||||
// NewDateTime is a representation of zero value for DateTime type
|
||||
func NewDateTime() DateTime {
|
||||
return DateTime(time.Unix(0, 0).UTC())
|
||||
}
|
||||
|
||||
// String converts this time to a string
|
||||
func (t DateTime) String() string {
|
||||
return NormalizeTimeForMarshal(time.Time(t)).Format(MarshalFormat)
|
||||
}
|
||||
|
||||
// IsZero returns whether the date time is a zero value
|
||||
func (t *DateTime) IsZero() bool {
|
||||
if t == nil {
|
||||
return true
|
||||
}
|
||||
return time.Time(*t).IsZero()
|
||||
}
|
||||
|
||||
// IsUnixZerom returns whether the date time is equivalent to time.Unix(0, 0).UTC().
|
||||
func (t *DateTime) IsUnixZero() bool {
|
||||
if t == nil {
|
||||
return true
|
||||
}
|
||||
return time.Time(*t).Equal(UnixZero)
|
||||
}
|
||||
|
||||
// MarshalText implements the text marshaller interface
|
||||
func (t DateTime) MarshalText() ([]byte, error) {
|
||||
return []byte(t.String()), nil
|
||||
}
|
||||
|
||||
// UnmarshalText implements the text unmarshaller interface
|
||||
func (t *DateTime) UnmarshalText(text []byte) error {
|
||||
tt, err := ParseDateTime(string(text))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*t = tt
|
||||
return nil
|
||||
}
|
||||
|
||||
// Scan scans a DateTime value from database driver type.
|
||||
func (t *DateTime) Scan(raw interface{}) error {
|
||||
// TODO: case int64: and case float64: ?
|
||||
switch v := raw.(type) {
|
||||
case []byte:
|
||||
return t.UnmarshalText(v)
|
||||
case string:
|
||||
return t.UnmarshalText([]byte(v))
|
||||
case time.Time:
|
||||
*t = DateTime(v)
|
||||
case nil:
|
||||
*t = DateTime{}
|
||||
default:
|
||||
return fmt.Errorf("cannot sql.Scan() strfmt.DateTime from: %#v", v)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value converts DateTime to a primitive value ready to written to a database.
|
||||
func (t DateTime) Value() (driver.Value, error) {
|
||||
return driver.Value(t.String()), nil
|
||||
}
|
||||
|
||||
// MarshalJSON returns the DateTime as JSON
|
||||
func (t DateTime) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(NormalizeTimeForMarshal(time.Time(t)).Format(MarshalFormat))
|
||||
}
|
||||
|
||||
// UnmarshalJSON sets the DateTime from JSON
|
||||
func (t *DateTime) UnmarshalJSON(data []byte) error {
|
||||
if string(data) == jsonNull {
|
||||
return nil
|
||||
}
|
||||
|
||||
var tstr string
|
||||
if err := json.Unmarshal(data, &tstr); err != nil {
|
||||
return err
|
||||
}
|
||||
tt, err := ParseDateTime(tstr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*t = tt
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalBSON renders the DateTime as a BSON document
|
||||
func (t DateTime) MarshalBSON() ([]byte, error) {
|
||||
return bson.Marshal(bson.M{"data": t})
|
||||
}
|
||||
|
||||
// UnmarshalBSON reads the DateTime from a BSON document
|
||||
func (t *DateTime) UnmarshalBSON(data []byte) error {
|
||||
var obj struct {
|
||||
Data DateTime
|
||||
}
|
||||
|
||||
if err := bson.Unmarshal(data, &obj); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*t = obj.Data
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalBSONValue is an interface implemented by types that can marshal themselves
|
||||
// into a BSON document represented as bytes. The bytes returned must be a valid
|
||||
// BSON document if the error is nil.
|
||||
// Marshals a DateTime as a bsontype.DateTime, an int64 representing
|
||||
// milliseconds since epoch.
|
||||
func (t DateTime) MarshalBSONValue() (bsontype.Type, []byte, error) {
|
||||
// UnixNano cannot be used directly, the result of calling UnixNano on the zero
|
||||
// Time is undefined. Thats why we use time.Nanosecond() instead.
|
||||
|
||||
tNorm := NormalizeTimeForMarshal(time.Time(t))
|
||||
i64 := tNorm.Unix()*1000 + int64(tNorm.Nanosecond())/1e6
|
||||
|
||||
buf := make([]byte, 8)
|
||||
binary.LittleEndian.PutUint64(buf, uint64(i64))
|
||||
|
||||
return bson.TypeDateTime, buf, nil
|
||||
}
|
||||
|
||||
// UnmarshalBSONValue is an interface implemented by types that can unmarshal a
|
||||
// BSON value representation of themselves. The BSON bytes and type can be
|
||||
// assumed to be valid. UnmarshalBSONValue must copy the BSON value bytes if it
|
||||
// wishes to retain the data after returning.
|
||||
func (t *DateTime) UnmarshalBSONValue(tpe bsontype.Type, data []byte) error {
|
||||
if tpe == bson.TypeNull {
|
||||
*t = DateTime{}
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(data) != 8 {
|
||||
return errors.New("bson date field length not exactly 8 bytes")
|
||||
}
|
||||
|
||||
i64 := int64(binary.LittleEndian.Uint64(data))
|
||||
// TODO: Use bsonprim.DateTime.Time() method
|
||||
*t = DateTime(time.Unix(i64/1000, i64%1000*1000000))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeepCopyInto copies the receiver and writes its value into out.
|
||||
func (t *DateTime) DeepCopyInto(out *DateTime) {
|
||||
*out = *t
|
||||
}
|
||||
|
||||
// DeepCopy copies the receiver into a new DateTime.
|
||||
func (t *DateTime) DeepCopy() *DateTime {
|
||||
if t == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(DateTime)
|
||||
t.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// GobEncode implements the gob.GobEncoder interface.
|
||||
func (t DateTime) GobEncode() ([]byte, error) {
|
||||
return t.MarshalBinary()
|
||||
}
|
||||
|
||||
// GobDecode implements the gob.GobDecoder interface.
|
||||
func (t *DateTime) GobDecode(data []byte) error {
|
||||
return t.UnmarshalBinary(data)
|
||||
}
|
||||
|
||||
// MarshalBinary implements the encoding.BinaryMarshaler interface.
|
||||
func (t DateTime) MarshalBinary() ([]byte, error) {
|
||||
return NormalizeTimeForMarshal(time.Time(t)).MarshalBinary()
|
||||
}
|
||||
|
||||
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
|
||||
func (t *DateTime) UnmarshalBinary(data []byte) error {
|
||||
var original time.Time
|
||||
|
||||
err := original.UnmarshalBinary(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*t = DateTime(original)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Equal checks if two DateTime instances are equal using time.Time's Equal method
|
||||
func (t DateTime) Equal(t2 DateTime) bool {
|
||||
return time.Time(t).Equal(time.Time(t2))
|
||||
}
|
230
vendor/github.com/go-openapi/strfmt/ulid.go
generated
vendored
Normal file
230
vendor/github.com/go-openapi/strfmt/ulid.go
generated
vendored
Normal file
@ -0,0 +1,230 @@
|
||||
package strfmt
|
||||
|
||||
import (
|
||||
cryptorand "crypto/rand"
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"sync"
|
||||
|
||||
"github.com/oklog/ulid"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
// ULID represents a ulid string format
|
||||
// ref:
|
||||
//
|
||||
// https://github.com/ulid/spec
|
||||
//
|
||||
// impl:
|
||||
//
|
||||
// https://github.com/oklog/ulid
|
||||
//
|
||||
// swagger:strfmt ulid
|
||||
type ULID struct {
|
||||
ulid.ULID
|
||||
}
|
||||
|
||||
var (
|
||||
ulidEntropyPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return cryptorand.Reader
|
||||
},
|
||||
}
|
||||
|
||||
ULIDScanDefaultFunc = func(raw interface{}) (ULID, error) {
|
||||
u := NewULIDZero()
|
||||
switch x := raw.(type) {
|
||||
case nil:
|
||||
// zerp ulid
|
||||
return u, nil
|
||||
case string:
|
||||
if x == "" {
|
||||
// zero ulid
|
||||
return u, nil
|
||||
}
|
||||
return u, u.UnmarshalText([]byte(x))
|
||||
case []byte:
|
||||
return u, u.UnmarshalText(x)
|
||||
}
|
||||
|
||||
return u, fmt.Errorf("cannot sql.Scan() strfmt.ULID from: %#v: %w", raw, ulid.ErrScanValue)
|
||||
}
|
||||
|
||||
// ULIDScanOverrideFunc allows you to override the Scan method of the ULID type
|
||||
ULIDScanOverrideFunc = ULIDScanDefaultFunc
|
||||
|
||||
ULIDValueDefaultFunc = func(u ULID) (driver.Value, error) {
|
||||
return driver.Value(u.String()), nil
|
||||
}
|
||||
|
||||
// ULIDValueOverrideFunc allows you to override the Value method of the ULID type
|
||||
ULIDValueOverrideFunc = ULIDValueDefaultFunc
|
||||
)
|
||||
|
||||
func init() {
|
||||
// register formats in the default registry:
|
||||
// - ulid
|
||||
ulid := ULID{}
|
||||
Default.Add("ulid", &ulid, IsULID)
|
||||
}
|
||||
|
||||
// IsULID checks if provided string is ULID format
|
||||
// Be noticed that this function considers overflowed ULID as non-ulid.
|
||||
// For more details see https://github.com/ulid/spec
|
||||
func IsULID(str string) bool {
|
||||
_, err := ulid.ParseStrict(str)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// ParseULID parses a string that represents an valid ULID
|
||||
func ParseULID(str string) (ULID, error) {
|
||||
var u ULID
|
||||
|
||||
return u, u.UnmarshalText([]byte(str))
|
||||
}
|
||||
|
||||
// NewULIDZero returns a zero valued ULID type
|
||||
func NewULIDZero() ULID {
|
||||
return ULID{}
|
||||
}
|
||||
|
||||
// NewULID generates new unique ULID value and a error if any
|
||||
func NewULID() (ULID, error) {
|
||||
var u ULID
|
||||
|
||||
obj := ulidEntropyPool.Get()
|
||||
entropy, ok := obj.(io.Reader)
|
||||
if !ok {
|
||||
return u, fmt.Errorf("failed to cast %+v to io.Reader", obj)
|
||||
}
|
||||
|
||||
id, err := ulid.New(ulid.Now(), entropy)
|
||||
if err != nil {
|
||||
return u, err
|
||||
}
|
||||
ulidEntropyPool.Put(entropy)
|
||||
|
||||
u.ULID = id
|
||||
return u, nil
|
||||
}
|
||||
|
||||
// GetULID returns underlying instance of ULID
|
||||
func (u *ULID) GetULID() interface{} {
|
||||
return u.ULID
|
||||
}
|
||||
|
||||
// MarshalText returns this instance into text
|
||||
func (u ULID) MarshalText() ([]byte, error) {
|
||||
return u.ULID.MarshalText()
|
||||
}
|
||||
|
||||
// UnmarshalText hydrates this instance from text
|
||||
func (u *ULID) UnmarshalText(data []byte) error { // validation is performed later on
|
||||
return u.ULID.UnmarshalText(data)
|
||||
}
|
||||
|
||||
// Scan reads a value from a database driver
|
||||
func (u *ULID) Scan(raw interface{}) error {
|
||||
ul, err := ULIDScanOverrideFunc(raw)
|
||||
if err == nil {
|
||||
*u = ul
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Value converts a value to a database driver value
|
||||
func (u ULID) Value() (driver.Value, error) {
|
||||
return ULIDValueOverrideFunc(u)
|
||||
}
|
||||
|
||||
func (u ULID) String() string {
|
||||
return u.ULID.String()
|
||||
}
|
||||
|
||||
// MarshalJSON returns the ULID as JSON
|
||||
func (u ULID) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(u.String())
|
||||
}
|
||||
|
||||
// UnmarshalJSON sets the ULID from JSON
|
||||
func (u *ULID) UnmarshalJSON(data []byte) error {
|
||||
if string(data) == jsonNull {
|
||||
return nil
|
||||
}
|
||||
var ustr string
|
||||
if err := json.Unmarshal(data, &ustr); err != nil {
|
||||
return err
|
||||
}
|
||||
id, err := ulid.ParseStrict(ustr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("couldn't parse JSON value as ULID: %w", err)
|
||||
}
|
||||
u.ULID = id
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalBSON document from this value
|
||||
func (u ULID) MarshalBSON() ([]byte, error) {
|
||||
return bson.Marshal(bson.M{"data": u.String()})
|
||||
}
|
||||
|
||||
// UnmarshalBSON document into this value
|
||||
func (u *ULID) UnmarshalBSON(data []byte) error {
|
||||
var m bson.M
|
||||
if err := bson.Unmarshal(data, &m); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if ud, ok := m["data"].(string); ok {
|
||||
id, err := ulid.ParseStrict(ud)
|
||||
if err != nil {
|
||||
return fmt.Errorf("couldn't parse bson bytes as ULID: %w", err)
|
||||
}
|
||||
u.ULID = id
|
||||
return nil
|
||||
}
|
||||
return errors.New("couldn't unmarshal bson bytes as ULID")
|
||||
}
|
||||
|
||||
// DeepCopyInto copies the receiver and writes its value into out.
|
||||
func (u *ULID) DeepCopyInto(out *ULID) {
|
||||
*out = *u
|
||||
}
|
||||
|
||||
// DeepCopy copies the receiver into a new ULID.
|
||||
func (u *ULID) DeepCopy() *ULID {
|
||||
if u == nil {
|
||||
return nil
|
||||
}
|
||||
out := new(ULID)
|
||||
u.DeepCopyInto(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// GobEncode implements the gob.GobEncoder interface.
|
||||
func (u ULID) GobEncode() ([]byte, error) {
|
||||
return u.ULID.MarshalBinary()
|
||||
}
|
||||
|
||||
// GobDecode implements the gob.GobDecoder interface.
|
||||
func (u *ULID) GobDecode(data []byte) error {
|
||||
return u.ULID.UnmarshalBinary(data)
|
||||
}
|
||||
|
||||
// MarshalBinary implements the encoding.BinaryMarshaler interface.
|
||||
func (u ULID) MarshalBinary() ([]byte, error) {
|
||||
return u.ULID.MarshalBinary()
|
||||
}
|
||||
|
||||
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
|
||||
func (u *ULID) UnmarshalBinary(data []byte) error {
|
||||
return u.ULID.UnmarshalBinary(data)
|
||||
}
|
||||
|
||||
// Equal checks if two ULID instances are equal by their underlying type
|
||||
func (u ULID) Equal(other ULID) bool {
|
||||
return u.ULID == other.ULID
|
||||
}
|
Reference in New Issue
Block a user