Fix the dependency issue (#231)

This commit is contained in:
Robbie Zhang
2018-06-21 12:09:42 -07:00
committed by GitHub
parent 027b76651d
commit 6ec1098bb8
16629 changed files with 74837 additions and 4975021 deletions

View File

@@ -1,26 +0,0 @@
# 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

View File

@@ -1,117 +0,0 @@
## Contribution Guidelines
### Pull requests are always welcome
We are always thrilled to receive pull requests, and do our best to
process them as fast as possible. Not sure if that typo is worth a pull
request? Do it! We will appreciate it.
If your pull request is not accepted on the first try, don't be
discouraged! If there's a problem with the implementation, hopefully you
received feedback on what to improve.
We're trying very hard to keep go-swagger lean and focused. We don't want it
to do everything for everybody. This means that we might decide against
incorporating a new feature. However, there might be a way to implement
that feature *on top of* go-swagger.
### Conventions
Fork the repo and make changes on your fork in a feature branch:
- If it's a bugfix branch, name it XXX-something where XXX is the number of the
issue
- If it's a feature branch, create an enhancement issue to announce your
intentions, and name it XXX-something where XXX is the number of the issue.
Submit unit tests for your changes. Go has a great test framework built in; use
it! Take a look at existing tests for inspiration. Run the full test suite on
your branch before submitting a pull request.
Update the documentation when creating or modifying features. Test
your documentation changes for clarity, concision, and correctness, as
well as a clean documentation build. See ``docs/README.md`` for more
information on building the docs and how docs get released.
Write clean code. Universally formatted code promotes ease of writing, reading,
and maintenance. Always run `gofmt -s -w file.go` on each changed file before
committing your changes. Most editors have plugins that do this automatically.
Pull requests descriptions should be as clear as possible and include a
reference to all the issues that they address.
Pull requests must not contain commits from other users or branches.
Commit messages must start with a capitalized and short summary (max. 50
chars) written in the imperative, followed by an optional, more detailed
explanatory text which is separated from the summary by an empty line.
Code review comments may be added to your pull request. Discuss, then make the
suggested modifications and push additional commits to your feature branch. Be
sure to post a comment after pushing. The new commits will show up in the pull
request automatically, but the reviewers will not be notified unless you
comment.
Before the pull request is merged, make sure that you squash your commits into
logical units of work using `git rebase -i` and `git push -f`. After every
commit the test suite should be passing. Include documentation changes in the
same commit so that a revert would remove all traces of the feature or fix.
Commits that fix or close an issue should include a reference like `Closes #XXX`
or `Fixes #XXX`, which will automatically close the issue when merged.
### Sign your work
The sign-off is a simple line at the end of the explanation for the
patch, which certifies that you wrote it or otherwise have the right to
pass it on as an open-source patch. The rules are pretty simple: if you
can certify the below (from
[developercertificate.org](http://developercertificate.org/)):
```
Developer Certificate of Origin
Version 1.1
Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
660 York Street, Suite 102,
San Francisco, CA 94110 USA
Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.
Developer's Certificate of Origin 1.1
By making a contribution to this project, I certify that:
(a) The contribution was created in whole or in part by me and I
have the right to submit it under the open source license
indicated in the file; or
(b) The contribution is based upon previous work that, to the best
of my knowledge, is covered under an appropriate open source
license and I have the right under that license to submit that
work with modifications, whether created in whole or in part
by me, under the same open source license (unless I am
permitted to submit under a different license), as indicated
in the file; or
(c) The contribution was provided directly to me by some other
person who certified (a), (b) or (c) and I have not modified
it.
(d) I understand and agree that this project and the contribution
are public and that a record of the contribution (including all
personal information I submit with it, including my sign-off) is
maintained indefinitely and may be redistributed consistent with
this project or the open source license(s) involved.
```
then you just add a line to every git commit message:
Signed-off-by: Joe Smith <joe@gmail.com>
using your real name (sorry, no pseudonyms or anonymous contributions.)
You can add the sign off when creating the git commit via `git commit -s`.

View File

@@ -1,2 +0,0 @@
secrets.yml
coverage.out

View File

@@ -1,17 +0,0 @@
language: go
go:
- 1.7
install:
- go get -u github.com/stretchr/testify/assert
- go get -u github.com/pborman/uuid
- go get -u github.com/asaskevich/govalidator
- go get -u github.com/mailru/easyjson
- go get -u github.com/go-openapi/errors
- go get -u github.com/mitchellh/mapstructure
script:
- go test -v -race -cover -coverprofile=coverage.txt -covermode=atomic ./...
after_success:
- bash <(curl -s https://codecov.io/bash)
notifications:
slack:
secure: zE5AtIYTpYfQPnTzP+EaQPN7JKtfFAGv6PrJqoIZLOXa8B6zGb6+J1JRNNxWi7faWbyJOxa4FSSsuPsKZMycUK6wlLFIdhDxwqeo7Ew8r6rdZKdfUHQggfNS9wO79ARoNYUDHtmnaBUS+eWSM1YqSc4i99QxyyfuURLOeAaA/q14YbdlTlaw3lrZ0qT92ot1FnVGNOx064zuHtFeUf+jAVRMZ6Q3rvqllwIlPszE6rmHGXBt2VoJxRaBetdwd7FgkcYw9FPXKHhadwC7/75ZAdmxIukhxNMw4Tr5NuPcqNcnbYLenDP7B3lssGVIrP4BRSqekS1d/tqvdvnnFWHMwrNCkSnSc065G5+qWTlXKAemIclgiXXqE2furBNLm05MDdG8fn5epS0UNarkjD+zX336RiqwBlOX4KbF+vPyqcO98CsN0lnd+H6loc9reiTHs37orFFpQ+309av9be2GGsHUsRB9ssIyrewmhAccOmkRtr2dVTZJNFQwa5Kph5TNJuTjnZEwG/xUkEX2YSfwShOsb062JWiflV6PJdnl80pc9Tn7D5sO5Bf9DbijGRJwwP+YiiJtwtr+vsvS+n4sM0b5eqm4UoRo+JJO8ffoJtHS7ItuyRbVQCwEPJ4221WLcf5PquEEDdAPwR+K4Gj8qTXqTDdxOiES1xFUKVgmzhI=

View File

@@ -1,74 +0,0 @@
# 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/

View File

@@ -1,5 +0,0 @@
# Strfmt [![Build Status](https://travis-ci.org/go-openapi/strfmt.svg?branch=master)](https://travis-ci.org/go-openapi/strfmt) [![codecov](https://codecov.io/gh/go-openapi/strfmt/branch/master/graph/badge.svg)](https://codecov.io/gh/go-openapi/strfmt) [![Slack Status](https://slackin.goswagger.io/badge.svg)](https://slackin.goswagger.io)
[![license](http://img.shields.io/badge/license-Apache%20v2-orange.svg)](https://raw.githubusercontent.com/go-openapi/strfmt/master/LICENSE) [![GoDoc](https://godoc.org/github.com/go-openapi/strfmt?status.svg)](http://godoc.org/github.com/go-openapi/strfmt)
strfmt represents a well known string format such as credit card or email. The go toolkit for open api specifications knows how to deal with those.

View File

@@ -1,71 +0,0 @@
// 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"
"database/sql/driver"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var _ sql.Scanner = &Date{}
var _ driver.Valuer = Date{}
func TestDate(t *testing.T) {
pp := Date{}
err := pp.UnmarshalText([]byte{})
assert.NoError(t, err)
err = pp.UnmarshalText([]byte("yada"))
assert.Error(t, err)
orig := "2014-12-15"
b := []byte(orig)
bj := []byte("\"" + orig + "\"")
err = pp.UnmarshalText([]byte(orig))
assert.NoError(t, err)
txt, err := pp.MarshalText()
assert.NoError(t, err)
assert.Equal(t, orig, string(txt))
err = pp.UnmarshalJSON(bj)
assert.NoError(t, err)
assert.EqualValues(t, orig, pp.String())
b, err = pp.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bj, b)
}
func TestDate_Scan(t *testing.T) {
ref := time.Now().Truncate(24 * time.Hour).UTC()
date, str := Date(ref), ref.Format(RFC3339FullDate)
values := []interface{}{str, []byte(str), ref}
for _, value := range values {
result := Date{}
(&result).Scan(value)
assert.Equal(t, date, result, "value: %#v", value)
}
}
func TestDate_Value(t *testing.T) {
ref := time.Now().Truncate(24 * time.Hour).UTC()
date := Date(ref)
dbv, err := date.Value()
assert.NoError(t, err)
assert.EqualValues(t, dbv, ref)
}

View File

@@ -1,538 +0,0 @@
// 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 (
"testing"
"github.com/pborman/uuid"
"github.com/stretchr/testify/assert"
)
func testValid(t *testing.T, name, value string) {
ok := Default.Validates(name, value)
if !ok {
t.Errorf("expected %s of type %s to be valid", value, name)
}
}
func testInvalid(t *testing.T, name, value string) {
ok := Default.Validates(name, value)
if ok {
t.Errorf("expected %s of type %s to be invalid", value, name)
}
}
func TestFormatURI(t *testing.T) {
uri := URI("http://somewhere.com")
str := string("http://somewhereelse.com")
b := []byte(str)
bj := []byte("\"" + str + "\"")
err := uri.UnmarshalText(b)
assert.NoError(t, err)
assert.EqualValues(t, URI("http://somewhereelse.com"), string(b))
b, err = uri.MarshalText()
assert.NoError(t, err)
assert.Equal(t, []byte("http://somewhereelse.com"), b)
err = uri.UnmarshalJSON(bj)
assert.NoError(t, err)
assert.EqualValues(t, URI("http://somewhereelse.com"), string(b))
b, err = uri.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bj, b)
testValid(t, "uri", str)
testInvalid(t, "uri", "somewhere.com")
}
func TestFormatEmail(t *testing.T) {
email := Email("somebody@somewhere.com")
str := string("somebodyelse@somewhere.com")
b := []byte(str)
bj := []byte("\"" + str + "\"")
err := email.UnmarshalText(b)
assert.NoError(t, err)
assert.EqualValues(t, Email("somebodyelse@somewhere.com"), string(b))
b, err = email.MarshalText()
assert.NoError(t, err)
assert.Equal(t, []byte("somebodyelse@somewhere.com"), b)
err = email.UnmarshalJSON(bj)
assert.NoError(t, err)
assert.EqualValues(t, Email(str), string(b))
b, err = email.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bj, b)
testValid(t, "email", str)
testInvalid(t, "email", "somebody@somewhere@com")
}
func TestFormatHostname(t *testing.T) {
hostname := Hostname("somewhere.com")
str := string("somewhere.com")
b := []byte(str)
bj := []byte("\"" + str + "\"")
err := hostname.UnmarshalText(b)
assert.NoError(t, err)
assert.EqualValues(t, Hostname("somewhere.com"), string(b))
b, err = hostname.MarshalText()
assert.NoError(t, err)
assert.Equal(t, []byte("somewhere.com"), b)
err = hostname.UnmarshalJSON(bj)
assert.NoError(t, err)
assert.EqualValues(t, Hostname(str), string(b))
b, err = hostname.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bj, b)
testValid(t, "hostname", str)
testInvalid(t, "hostname", "somewhere.com!")
}
func TestFormatIPv4(t *testing.T) {
ipv4 := IPv4("192.168.254.1")
str := string("192.168.254.2")
b := []byte(str)
bj := []byte("\"" + str + "\"")
err := ipv4.UnmarshalText(b)
assert.NoError(t, err)
assert.EqualValues(t, IPv4("192.168.254.2"), string(b))
b, err = ipv4.MarshalText()
assert.NoError(t, err)
assert.Equal(t, []byte("192.168.254.2"), b)
err = ipv4.UnmarshalJSON(bj)
assert.NoError(t, err)
assert.EqualValues(t, IPv4(str), string(b))
b, err = ipv4.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bj, b)
testValid(t, "ipv4", str)
testInvalid(t, "ipv4", "192.168.254.2.2")
}
func TestFormatIPv6(t *testing.T) {
ipv6 := IPv6("::1")
str := string("::2")
b := []byte(str)
bj := []byte("\"" + str + "\"")
err := ipv6.UnmarshalText(b)
assert.NoError(t, err)
assert.EqualValues(t, IPv6("::2"), string(b))
b, err = ipv6.MarshalText()
assert.NoError(t, err)
assert.Equal(t, []byte("::2"), b)
err = ipv6.UnmarshalJSON(bj)
assert.NoError(t, err)
assert.EqualValues(t, IPv6(str), string(b))
b, err = ipv6.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bj, b)
testValid(t, "ipv6", str)
testInvalid(t, "ipv6", "127.0.0.1")
}
func TestFormatMAC(t *testing.T) {
mac := MAC("01:02:03:04:05:06")
str := string("06:05:04:03:02:01")
b := []byte(str)
bj := []byte("\"" + str + "\"")
err := mac.UnmarshalText(b)
assert.NoError(t, err)
assert.EqualValues(t, MAC("06:05:04:03:02:01"), string(b))
b, err = mac.MarshalText()
assert.NoError(t, err)
assert.Equal(t, []byte("06:05:04:03:02:01"), b)
err = mac.UnmarshalJSON(bj)
assert.NoError(t, err)
assert.EqualValues(t, MAC(str), string(b))
b, err = mac.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bj, b)
testValid(t, "mac", str)
testInvalid(t, "mac", "01:02:03:04:05")
}
func TestFormatUUID3(t *testing.T) {
first3 := uuid.NewMD5(uuid.NameSpace_URL, []byte("somewhere.com"))
other3 := uuid.NewMD5(uuid.NameSpace_URL, []byte("somewhereelse.com"))
uuid3 := UUID3(first3.String())
str := string(other3.String())
b := []byte(str)
bj := []byte("\"" + str + "\"")
err := uuid3.UnmarshalText(b)
assert.NoError(t, err)
assert.EqualValues(t, UUID3(other3.String()), string(b))
b, err = uuid3.MarshalText()
assert.NoError(t, err)
assert.EqualValues(t, []byte(other3.String()), b)
err = uuid3.UnmarshalJSON(bj)
assert.NoError(t, err)
assert.EqualValues(t, UUID3(str), string(b))
b, err = uuid3.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bj, b)
testValid(t, "uuid3", str)
testInvalid(t, "uuid3", "not-a-uuid")
}
func TestFormatUUID4(t *testing.T) {
first4 := uuid.NewRandom()
other4 := uuid.NewRandom()
uuid4 := UUID4(first4.String())
str := string(other4.String())
b := []byte(str)
bj := []byte("\"" + str + "\"")
err := uuid4.UnmarshalText(b)
assert.NoError(t, err)
assert.EqualValues(t, UUID4(other4.String()), string(b))
b, err = uuid4.MarshalText()
assert.NoError(t, err)
assert.Equal(t, []byte(other4.String()), b)
err = uuid4.UnmarshalJSON(bj)
assert.NoError(t, err)
assert.EqualValues(t, UUID4(str), string(b))
b, err = uuid4.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bj, b)
testValid(t, "uuid4", str)
testInvalid(t, "uuid4", "not-a-uuid")
}
func TestFormatUUID5(t *testing.T) {
first5 := uuid.NewSHA1(uuid.NameSpace_URL, []byte("somewhere.com"))
other5 := uuid.NewSHA1(uuid.NameSpace_URL, []byte("somewhereelse.com"))
uuid5 := UUID5(first5.String())
str := string(other5.String())
b := []byte(str)
bj := []byte("\"" + str + "\"")
err := uuid5.UnmarshalText(b)
assert.NoError(t, err)
assert.EqualValues(t, UUID5(other5.String()), string(b))
b, err = uuid5.MarshalText()
assert.NoError(t, err)
assert.Equal(t, []byte(other5.String()), b)
err = uuid5.UnmarshalJSON(bj)
assert.NoError(t, err)
assert.EqualValues(t, UUID5(str), string(b))
b, err = uuid5.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bj, b)
testValid(t, "uuid5", str)
testInvalid(t, "uuid5", "not-a-uuid")
}
func TestFormatUUID(t *testing.T) {
first5 := uuid.NewSHA1(uuid.NameSpace_URL, []byte("somewhere.com"))
other5 := uuid.NewSHA1(uuid.NameSpace_URL, []byte("somewhereelse.com"))
uuid := UUID(first5.String())
str := string(other5.String())
b := []byte(str)
bj := []byte("\"" + str + "\"")
err := uuid.UnmarshalText(b)
assert.NoError(t, err)
assert.EqualValues(t, UUID(other5.String()), string(b))
b, err = uuid.MarshalText()
assert.NoError(t, err)
assert.Equal(t, []byte(other5.String()), b)
err = uuid.UnmarshalJSON(bj)
assert.NoError(t, err)
assert.EqualValues(t, UUID(str), string(b))
b, err = uuid.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bj, b)
testValid(t, "uuid", str)
testInvalid(t, "uuid", "not-a-uuid")
}
func TestFormatISBN(t *testing.T) {
isbn := ISBN("0321751043")
str := string("0321751043")
b := []byte(str)
bj := []byte("\"" + str + "\"")
err := isbn.UnmarshalText(b)
assert.NoError(t, err)
assert.EqualValues(t, ISBN("0321751043"), string(b))
b, err = isbn.MarshalText()
assert.NoError(t, err)
assert.Equal(t, []byte("0321751043"), b)
err = isbn.UnmarshalJSON(bj)
assert.NoError(t, err)
assert.EqualValues(t, ISBN(str), string(b))
b, err = isbn.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bj, b)
testValid(t, "isbn", str)
testInvalid(t, "isbn", "836217463") // bad checksum
}
func TestFormatISBN10(t *testing.T) {
isbn10 := ISBN10("0321751043")
str := string("0321751043")
b := []byte(str)
bj := []byte("\"" + str + "\"")
err := isbn10.UnmarshalText(b)
assert.NoError(t, err)
assert.EqualValues(t, ISBN10("0321751043"), string(b))
b, err = isbn10.MarshalText()
assert.NoError(t, err)
assert.Equal(t, []byte("0321751043"), b)
err = isbn10.UnmarshalJSON(bj)
assert.NoError(t, err)
assert.EqualValues(t, ISBN10(str), string(b))
b, err = isbn10.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bj, b)
testValid(t, "isbn10", str)
testInvalid(t, "isbn10", "836217463") // bad checksum
}
func TestFormatISBN13(t *testing.T) {
isbn13 := ISBN13("978-0321751041")
str := string("978-0321751041")
b := []byte(str)
bj := []byte("\"" + str + "\"")
err := isbn13.UnmarshalText(b)
assert.NoError(t, err)
assert.EqualValues(t, ISBN13("978-0321751041"), string(b))
b, err = isbn13.MarshalText()
assert.NoError(t, err)
assert.Equal(t, []byte("978-0321751041"), b)
err = isbn13.UnmarshalJSON(bj)
assert.NoError(t, err)
assert.EqualValues(t, ISBN13(str), string(b))
b, err = isbn13.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bj, b)
testValid(t, "isbn13", str)
testInvalid(t, "isbn13", "978-0321751042") // bad checksum
}
func TestFormatHexColor(t *testing.T) {
hexColor := HexColor("#FFFFFF")
str := string("#000000")
b := []byte(str)
bj := []byte("\"" + str + "\"")
err := hexColor.UnmarshalText(b)
assert.NoError(t, err)
assert.EqualValues(t, HexColor("#000000"), string(b))
b, err = hexColor.MarshalText()
assert.NoError(t, err)
assert.Equal(t, []byte("#000000"), b)
err = hexColor.UnmarshalJSON(bj)
assert.NoError(t, err)
assert.EqualValues(t, HexColor(str), string(b))
b, err = hexColor.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bj, b)
testValid(t, "hexcolor", str)
testInvalid(t, "hexcolor", "#fffffffz")
}
func TestFormatRGBColor(t *testing.T) {
rgbColor := RGBColor("rgb(255,255,255)")
str := string("rgb(0,0,0)")
b := []byte(str)
bj := []byte("\"" + str + "\"")
err := rgbColor.UnmarshalText(b)
assert.NoError(t, err)
assert.EqualValues(t, RGBColor("rgb(0,0,0)"), string(b))
b, err = rgbColor.MarshalText()
assert.NoError(t, err)
assert.Equal(t, []byte("rgb(0,0,0)"), b)
err = rgbColor.UnmarshalJSON(bj)
assert.NoError(t, err)
assert.EqualValues(t, RGBColor(str), string(b))
b, err = rgbColor.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bj, b)
testValid(t, "rgbcolor", str)
testInvalid(t, "rgbcolor", "rgb(300,0,0)")
}
func TestFormatSSN(t *testing.T) {
ssn := SSN("111-11-1111")
str := string("999 99 9999")
b := []byte(str)
bj := []byte("\"" + str + "\"")
err := ssn.UnmarshalText(b)
assert.NoError(t, err)
assert.EqualValues(t, SSN("999 99 9999"), string(b))
b, err = ssn.MarshalText()
assert.NoError(t, err)
assert.Equal(t, []byte("999 99 9999"), b)
err = ssn.UnmarshalJSON(bj)
assert.NoError(t, err)
assert.EqualValues(t, SSN(str), string(b))
b, err = ssn.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bj, b)
testValid(t, "ssn", str)
testInvalid(t, "ssn", "999 99 999")
}
func TestFormatCreditCard(t *testing.T) {
creditCard := CreditCard("4111-1111-1111-1111")
str := string("4012-8888-8888-1881")
b := []byte(str)
bj := []byte("\"" + str + "\"")
err := creditCard.UnmarshalText(b)
assert.NoError(t, err)
assert.EqualValues(t, CreditCard("4012-8888-8888-1881"), string(b))
b, err = creditCard.MarshalText()
assert.NoError(t, err)
assert.Equal(t, []byte("4012-8888-8888-1881"), b)
err = creditCard.UnmarshalJSON(bj)
assert.NoError(t, err)
assert.EqualValues(t, CreditCard(str), string(b))
b, err = creditCard.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bj, b)
testValid(t, "creditcard", str)
testInvalid(t, "creditcard", "9999-9999-9999-999") // bad checksum
}
func TestFormatPassword(t *testing.T) {
password := Password("super secret stuff here")
str := string("even more secret")
b := []byte(str)
bj := []byte("\"" + str + "\"")
err := password.UnmarshalText(b)
assert.NoError(t, err)
assert.EqualValues(t, Password("even more secret"), string(b))
b, err = password.MarshalText()
assert.NoError(t, err)
assert.Equal(t, []byte("even more secret"), b)
err = password.UnmarshalJSON(bj)
assert.NoError(t, err)
assert.EqualValues(t, Password(str), string(b))
b, err = password.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bj, b)
// everything is valid
testValid(t, "password", str)
}
func TestFormatBase64(t *testing.T) {
b64 := Base64("ZWxpemFiZXRocG9zZXk=")
str := string("ZWxpemFiZXRocG9zZXk=")
b := []byte(str)
bj := []byte("\"" + str + "\"")
err := b64.UnmarshalText(b)
assert.NoError(t, err)
assert.EqualValues(t, Base64("ZWxpemFiZXRocG9zZXk="), string(b))
b, err = b64.MarshalText()
assert.NoError(t, err)
assert.Equal(t, []byte("ZWxpemFiZXRocG9zZXk="), b)
err = b64.UnmarshalJSON(bj)
assert.NoError(t, err)
assert.EqualValues(t, Base64(str), string(b))
b, err = b64.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bj, b)
testValid(t, "byte", str)
testInvalid(t, "byte", "ZWxpemFiZXRocG9zZXk") // missing pad char
}

View File

@@ -1,132 +0,0 @@
// 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 (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestDuration(t *testing.T) {
pp := Duration(0)
err := pp.UnmarshalText([]byte("0ms"))
assert.NoError(t, err)
err = pp.UnmarshalText([]byte("yada"))
assert.Error(t, err)
orig := "2ms"
b := []byte(orig)
bj := []byte("\"" + orig + "\"")
err = pp.UnmarshalText(b)
assert.NoError(t, err)
txt, err := pp.MarshalText()
assert.NoError(t, err)
assert.Equal(t, orig, string(txt))
err = pp.UnmarshalJSON(bj)
assert.NoError(t, err)
assert.EqualValues(t, orig, pp.String())
b, err = pp.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, bj, b)
}
func testDurationParser(t *testing.T, toParse string, expected time.Duration) {
r, e := ParseDuration(toParse)
assert.NoError(t, e)
assert.Equal(t, expected, r)
}
func testDurationSQLScanner(t *testing.T, dur time.Duration) {
values := []interface{}{int64(dur), float64(dur)}
for _, value := range values {
var result Duration
err := result.Scan(value)
assert.NoError(t, err)
assert.Equal(t, dur, time.Duration(result))
}
}
func TestDurationParser(t *testing.T) {
testcases := map[string]time.Duration{
// parse the short forms without spaces
"1ns": 1 * time.Nanosecond,
"1us": 1 * time.Microsecond,
"1µs": 1 * time.Microsecond,
"1ms": 1 * time.Millisecond,
"1s": 1 * time.Second,
"1m": 1 * time.Minute,
"1h": 1 * time.Hour,
"1hr": 1 * time.Hour,
"1d": 24 * time.Hour,
"1w": 7 * 24 * time.Hour,
"1wk": 7 * 24 * time.Hour,
// parse the long forms without spaces
"1nanoseconds": 1 * time.Nanosecond,
"1nanos": 1 * time.Nanosecond,
"1microseconds": 1 * time.Microsecond,
"1micros": 1 * time.Microsecond,
"1millis": 1 * time.Millisecond,
"1milliseconds": 1 * time.Millisecond,
"1second": 1 * time.Second,
"1sec": 1 * time.Second,
"1min": 1 * time.Minute,
"1minute": 1 * time.Minute,
"1hour": 1 * time.Hour,
"1day": 24 * time.Hour,
"1week": 7 * 24 * time.Hour,
// parse the short forms with spaces
"1 ns": 1 * time.Nanosecond,
"1 us": 1 * time.Microsecond,
"1 µs": 1 * time.Microsecond,
"1 ms": 1 * time.Millisecond,
"1 s": 1 * time.Second,
"1 m": 1 * time.Minute,
"1 h": 1 * time.Hour,
"1 hr": 1 * time.Hour,
"1 d": 24 * time.Hour,
"1 w": 7 * 24 * time.Hour,
"1 wk": 7 * 24 * time.Hour,
// parse the long forms without spaces
"1 nanoseconds": 1 * time.Nanosecond,
"1 nanos": 1 * time.Nanosecond,
"1 microseconds": 1 * time.Microsecond,
"1 micros": 1 * time.Microsecond,
"1 millis": 1 * time.Millisecond,
"1 milliseconds": 1 * time.Millisecond,
"1 second": 1 * time.Second,
"1 sec": 1 * time.Second,
"1 min": 1 * time.Minute,
"1 minute": 1 * time.Minute,
"1 hour": 1 * time.Hour,
"1 day": 24 * time.Hour,
"1 week": 7 * 24 * time.Hour,
}
for str, dur := range testcases {
testDurationParser(t, str, dur)
testDurationSQLScanner(t, dur)
}
}

View File

@@ -1,223 +0,0 @@
// 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 (
"strings"
"testing"
"time"
"github.com/mitchellh/mapstructure"
"github.com/stretchr/testify/assert"
)
type testFormat string
func (t testFormat) MarshalText() ([]byte, error) {
return []byte(string(t)), nil
}
func (t *testFormat) UnmarshalText(b []byte) error {
*t = testFormat(string(b))
return nil
}
func (t testFormat) String() string {
return string(t)
}
func isTestFormat(s string) bool {
return strings.HasPrefix(s, "tf")
}
type tf2 string
func (t tf2) MarshalText() ([]byte, error) {
return []byte(string(t)), nil
}
func (t *tf2) UnmarshalText(b []byte) error {
*t = tf2(string(b))
return nil
}
func istf2(s string) bool {
return strings.HasPrefix(s, "af")
}
func (t tf2) String() string {
return string(t)
}
type bf string
func (t bf) MarshalText() ([]byte, error) {
return []byte(string(t)), nil
}
func (t *bf) UnmarshalText(b []byte) error {
*t = bf(string(b))
return nil
}
func (t bf) String() string {
return string(t)
}
func isbf(s string) bool {
return strings.HasPrefix(s, "bf")
}
func istf3(s string) bool {
return strings.HasPrefix(s, "ff")
}
func init() {
tf := testFormat("")
Default.Add("test-format", &tf, isTestFormat)
}
func TestFormatRegistry(t *testing.T) {
f2 := tf2("")
f3 := bf("")
registry := NewFormats()
assert.True(t, registry.ContainsName("test-format"))
assert.True(t, registry.ContainsName("testformat"))
assert.False(t, registry.ContainsName("ttt"))
assert.True(t, registry.Validates("testformat", "tfa"))
assert.False(t, registry.Validates("testformat", "ffa"))
assert.True(t, registry.Add("tf2", &f2, istf2))
assert.True(t, registry.ContainsName("tf2"))
assert.False(t, registry.ContainsName("tfw"))
assert.True(t, registry.Validates("tf2", "afa"))
assert.False(t, registry.Add("tf2", &f3, isbf))
assert.True(t, registry.ContainsName("tf2"))
assert.False(t, registry.ContainsName("tfw"))
assert.True(t, registry.Validates("tf2", "bfa"))
assert.False(t, registry.Validates("tf2", "afa"))
assert.False(t, registry.Add("tf2", &f2, istf2))
assert.True(t, registry.Add("tf3", &f2, istf3))
assert.True(t, registry.ContainsName("tf3"))
assert.True(t, registry.ContainsName("tf2"))
assert.False(t, registry.ContainsName("tfw"))
assert.True(t, registry.Validates("tf3", "ffa"))
assert.True(t, registry.DelByName("tf3"))
assert.True(t, registry.Add("tf3", &f2, istf3))
assert.True(t, registry.DelByName("tf3"))
assert.False(t, registry.DelByName("unknown"))
assert.False(t, registry.Validates("unknown", ""))
}
type testStruct struct {
D Date `json:"d,omitempty"`
DT DateTime `json:"dt,omitempty"`
Dur Duration `json:"dur,omitempty"`
Uri URI `json:"uri,omitempty"`
Eml Email `json:"eml,omitempty"`
Uuid UUID `json:"uuid,omitempty"`
Uuid3 UUID3 `json:"uuid3,omitempty"`
Uuid4 UUID4 `json:"uuid4,omitempty"`
Uuid5 UUID5 `json:"uuid5,omitempty"`
Hn Hostname `json:"hn,omitempty"`
Ipv4 IPv4 `json:"ipv4,omitempty"`
Ipv6 IPv6 `json:"ipv6,omitempty"`
Mac MAC `json:"mac,omitempty"`
Isbn ISBN `json:"isbn,omitempty"`
Isbn10 ISBN10 `json:"isbn10,omitempty"`
Isbn13 ISBN13 `json:"isbn13,omitempty"`
Creditcard CreditCard `json:"creditcard,omitempty"`
Ssn SSN `json:"ssn,omitempty"`
Hexcolor HexColor `json:"hexcolor,omitempty"`
Rgbcolor RGBColor `json:"rgbcolor,omitempty"`
B64 Base64 `json:"b64,omitempty"`
Pw Password `json:"pw,omitempty"`
}
func TestDecodeHook(t *testing.T) {
registry := NewFormats()
m := map[string]interface{}{
"d": "2014-12-15",
"dt": "2012-03-02T15:06:05.999999999Z",
"dur": "5s",
"uri": "http://www.dummy.com",
"eml": "dummy@dummy.com",
"uuid": "a8098c1a-f86e-11da-bd1a-00112444be1e",
"uuid3": "bcd02e22-68f0-3046-a512-327cca9def8f",
"uuid4": "025b0d74-00a2-4048-bf57-227c5111bb34",
"uuid5": "886313e1-3b8a-5372-9b90-0c9aee199e5d",
"hn": "somewhere.com",
"ipv4": "192.168.254.1",
"ipv6": "::1",
"mac": "01:02:03:04:05:06",
"isbn": "0321751043",
"isbn10": "0321751043",
"isbn13": "978-0321751041",
"hexcolor": "#FFFFFF",
"rgbcolor": "rgb(255,255,255)",
"pw": "super secret stuff here",
"ssn": "111-11-1111",
"creditcard": "4111-1111-1111-1111",
"b64": "ZWxpemFiZXRocG9zZXk=",
}
date, _ := time.Parse(RFC3339FullDate, "2014-12-15")
dur, _ := ParseDuration("5s")
dt, _ := ParseDateTime("2012-03-02T15:06:05.999999999Z")
exp := &testStruct{
D: Date(date),
DT: dt,
Dur: Duration(dur),
Uri: URI("http://www.dummy.com"),
Eml: Email("dummy@dummy.com"),
Uuid: UUID("a8098c1a-f86e-11da-bd1a-00112444be1e"),
Uuid3: UUID3("bcd02e22-68f0-3046-a512-327cca9def8f"),
Uuid4: UUID4("025b0d74-00a2-4048-bf57-227c5111bb34"),
Uuid5: UUID5("886313e1-3b8a-5372-9b90-0c9aee199e5d"),
Hn: Hostname("somewhere.com"),
Ipv4: IPv4("192.168.254.1"),
Ipv6: IPv6("::1"),
Mac: MAC("01:02:03:04:05:06"),
Isbn: ISBN("0321751043"),
Isbn10: ISBN10("0321751043"),
Isbn13: ISBN13("978-0321751041"),
Creditcard: CreditCard("4111-1111-1111-1111"),
Ssn: SSN("111-11-1111"),
Hexcolor: HexColor("#FFFFFF"),
Rgbcolor: RGBColor("rgb(255,255,255)"),
B64: Base64("ZWxpemFiZXRocG9zZXk="),
Pw: Password("super secret stuff here"),
}
test := new(testStruct)
cfg := &mapstructure.DecoderConfig{
DecodeHook: registry.MapStructureHookFunc(),
// weakly typed will pass if this passes
WeaklyTypedInput: false,
Result: test,
}
d, err := mapstructure.NewDecoder(cfg)
assert.Nil(t, err)
err = d.Decode(m)
assert.Nil(t, err)
assert.Equal(t, exp, test)
}

View File

@@ -1,137 +0,0 @@
// 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 (
"bytes"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var (
p, _ = time.Parse(time.RFC3339Nano, "2011-08-18T19:03:37.000000000+01:00")
testCases = []struct {
in []byte // externally sourced data -- to be unmarshalled
time time.Time // its representation in time.Time
str string // its marshalled representation
}{
{[]byte("2014-12-15T08:00:00.000Z"), time.Date(2014, 12, 15, 8, 0, 0, 0, time.UTC), "2014-12-15T08:00:00.000Z"},
{[]byte("2011-08-18T19:03:37.000000000+01:00"), time.Date(2011, 8, 18, 19, 3, 37, 0, p.Location()), "2011-08-18T19:03:37.000+01:00"},
{[]byte("2014-12-15T19:30:20Z"), time.Date(2014, 12, 15, 19, 30, 20, 0, time.UTC), "2014-12-15T19:30:20.000Z"},
{[]byte("0001-01-01T00:00:00Z"), time.Time{}.UTC(), "0001-01-01T00:00:00.000Z"},
{[]byte(""), time.Unix(0, 0).UTC(), "1970-01-01T00:00:00.000Z"},
{[]byte(nil), time.Unix(0, 0).UTC(), "1970-01-01T00:00:00.000Z"},
}
)
func TestNewDateTime(t *testing.T) {
assert.EqualValues(t, time.Unix(0, 0).UTC(), NewDateTime())
}
func TestParseDateTime_errorCases(t *testing.T) {
_, err := ParseDateTime("yada")
assert.Error(t, err)
}
// TestParseDateTime tests the full cycle:
// parsing -> marshalling -> unmarshalling / scanning
func TestParseDateTime_fullCycle(t *testing.T) {
for caseNum, example := range testCases {
t.Logf("Case #%d", caseNum)
parsed, err := ParseDateTime(string(example.in))
assert.NoError(t, err)
assert.EqualValues(t, example.time, parsed)
mt, err := parsed.MarshalText()
assert.NoError(t, err)
assert.Equal(t, []byte(example.str), mt)
pp := NewDateTime()
err = pp.UnmarshalText(mt)
assert.NoError(t, err)
assert.EqualValues(t, example.time, pp)
pp = NewDateTime()
err = pp.Scan(mt)
assert.NoError(t, err)
assert.Equal(t, DateTime(example.time), pp)
}
}
func TestDateTime_UnmarshalText_errorCases(t *testing.T) {
pp := NewDateTime()
err := pp.UnmarshalText([]byte("yada"))
assert.Error(t, err)
err = pp.UnmarshalJSON([]byte("yada"))
assert.Error(t, err)
}
func TestDateTime_UnmarshalText(t *testing.T) {
for caseNum, example := range testCases {
t.Logf("Case #%d", caseNum)
pp := NewDateTime()
err := pp.UnmarshalText(example.in)
assert.NoError(t, err)
assert.EqualValues(t, example.time, pp)
}
}
func TestDateTime_UnmarshalJSON(t *testing.T) {
for caseNum, example := range testCases {
t.Logf("Case #%d", caseNum)
pp := NewDateTime()
err := pp.UnmarshalJSON(esc(example.in))
assert.NoError(t, err)
assert.EqualValues(t, example.time, pp)
}
}
func esc(v []byte) []byte {
var buf bytes.Buffer
buf.WriteByte('"')
buf.Write(v)
buf.WriteByte('"')
return buf.Bytes()
}
func TestDateTime_MarshalText(t *testing.T) {
for caseNum, example := range testCases {
t.Logf("Case #%d", caseNum)
dt := DateTime(example.time)
mt, err := dt.MarshalText()
assert.NoError(t, err)
assert.Equal(t, []byte(example.str), mt)
}
}
func TestDateTime_MarshalJSON(t *testing.T) {
for caseNum, example := range testCases {
t.Logf("Case #%d", caseNum)
dt := DateTime(example.time)
bb, err := dt.MarshalJSON()
assert.NoError(t, err)
assert.EqualValues(t, esc([]byte(example.str)), bb)
}
}
func TestDateTime_Scan(t *testing.T) {
for caseNum, example := range testCases {
t.Logf("Case #%d", caseNum)
pp := NewDateTime()
err := pp.Scan(example.in)
assert.NoError(t, err)
assert.Equal(t, DateTime(example.time), pp)
}
}