Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from go-dummy/feat/parse
Browse files Browse the repository at this point in the history
feat: add parse and more openapi fields
  • Loading branch information
sashamelentyev authored Jan 30, 2022
2 parents 30baf9e + 5b60ea5 commit 43f2cf7
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 13 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
OpenAPI specification object model

## Features
- OpenAPI specification
- Easy to integrate.

## Installation
```shell
Expand All @@ -28,16 +28,16 @@ See [these docs][pkg-url].

[MIT License](LICENSE).

[build-img]: https://github.com/go-dummy/tmp/workflows/build/badge.svg
[build-url]: https://github.com/go-dummy/tmp/actions
[pkg-img]: https://pkg.go.dev/badge/go-dummy/tmp
[pkg-url]: https://pkg.go.dev/github.com/go-dummy/tmp
[reportcard-img]: https://goreportcard.com/badge/go-dummy/tmp
[reportcard-url]: https://goreportcard.com/report/go-dummy/tmp
[coverage-img]: https://codecov.io/gh/go-dummy/tmp/branch/main/graph/badge.svg
[coverage-url]: https://codecov.io/gh/go-dummy/tmp
[version-img]: https://img.shields.io/github/v/release/go-dummy/tmp
[version-url]: https://github.com/go-dummy/tmp/releases
[build-img]: https://github.com/go-dummy/openapi/workflows/build/badge.svg
[build-url]: https://github.com/go-dummy/openapi/actions
[pkg-img]: https://pkg.go.dev/badge/go-dummy/openapi
[pkg-url]: https://pkg.go.dev/github.com/go-dummy/openapi
[reportcard-img]: https://goreportcard.com/badge/go-dummy/openapi
[reportcard-url]: https://goreportcard.com/report/go-dummy/openapi
[coverage-img]: https://codecov.io/gh/go-dummy/openapi/branch/main/graph/badge.svg
[coverage-url]: https://codecov.io/gh/go-dummy/openapi
[version-img]: https://img.shields.io/github/v/release/go-dummy/openapi
[version-url]: https://github.com/go-dummy/openapi/releases

## Sponsors
<p>
Expand Down
12 changes: 10 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
module github.com/go-dummy/openapi

go 1.18
go 1.17

require github.com/stretchr/testify v1.7.0
require (
github.com/goccy/go-yaml v1.9.5
github.com/stretchr/testify v1.7.0
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/fatih/color v1.10.0 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
33 changes: 33 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,44 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/goccy/go-yaml v1.9.5 h1:Eh/+3uk9kLxG4koCX6lRMAPS1OaMSAi+FJcya0INdB0=
github.com/goccy/go-yaml v1.9.5/go.mod h1:U/jl18uSupI5rdI2jmuCswEA2htH9eXfferR3KfscvA=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3 changes: 3 additions & 0 deletions openapi3.go → openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ func (e *SchemaError) Error() string {
type OpenAPI struct {
OpenAPI string `json:"openapi" yaml:"openapi"`
Info Info `json:"info" yaml:"info"`
Servers Servers `json:"servers,omitempty" yaml:"servers,omitempty"`
Paths Paths `json:"paths" yaml:"paths"`
Components Components `json:"components,omitempty" yaml:"components,omitempty"`
Security []Security `json:"security,omitempty" yaml:"security,omitempty"`
Tags Tags `json:"tags,omitempty" yaml:"tags,omitempty"`
}

// LookupByReference -.
Expand Down
File renamed without changes.
16 changes: 16 additions & 0 deletions parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package openapi

import (
"github.com/goccy/go-yaml"
)

func Parse(data []byte) (OpenAPI, error) {
var openapi OpenAPI

err := yaml.Unmarshal(data, &openapi)
if err != nil {
return OpenAPI{}, err
}

return openapi, nil
}
49 changes: 49 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package openapi_test

import (
"errors"
"io/ioutil"
"testing"

"github.com/go-dummy/openapi"
"github.com/stretchr/testify/require"
)

func TestParse(t *testing.T) {
tests := []struct {
name string
path string
want openapi.OpenAPI
err error
}{
{
name: "wrong yml",
path: "./testdata/wrong-openapi.yml",
want: openapi.OpenAPI{},
err: errors.New("[1:1] string was used where mapping is expected\n> 1 | openapi\n ^\n"),
},
{
name: "",
path: "./testdata/openapi.yml",
want: openapi.OpenAPI{
OpenAPI: "3.0.3",
},
err: nil,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
data, err := ioutil.ReadFile(tc.path)
if err != nil {
t.Fatal(err)
}
got, err := openapi.Parse(data)
if err != nil {
require.EqualError(t, err, tc.err.Error())
}
require.Equal(t, tc.want, got)
})
}

}
3 changes: 3 additions & 0 deletions security.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package openapi

type Security map[string][]string
10 changes: 10 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package openapi

// Server -.
type Server struct {
URL string `json:"url" yaml:"url"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
}

// Servers -.
type Servers []*Server
10 changes: 10 additions & 0 deletions tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package openapi

// Tag -.
type Tag struct {
Name string `json:"name,omitempty" yaml:"name,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
}

// Tags -.
type Tags []*Tag
1 change: 1 addition & 0 deletions testdata/openapi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
openapi: 3.0.3
1 change: 1 addition & 0 deletions testdata/wrong-openapi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
openapi

0 comments on commit 43f2cf7

Please sign in to comment.