diff --git a/Gopkg.lock b/Gopkg.lock index b2ae9c3..3cfe92d 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -26,10 +26,9 @@ version = "v1.2.0" [[projects]] - branch = "master" name = "github.com/emicklei/proto-contrib" packages = ["pkg/protofmt"] - revision = "45fb5125b545af5782a183e5bd64e14d2d60ccbf" + revision = "1a0b77302cc833fcf469702a8737b893a91a905c" [[projects]] branch = "master" @@ -44,10 +43,9 @@ revision = "36d33bfe519efae5632669801b180bf1a245da3b" [[projects]] - branch = "master" name = "github.com/go-openapi/spec" packages = ["."] - revision = "fa03337d7da5735229ee8f5e9d5d0b996014b7f8" + revision = "f3499b5df53897321d6f5e9c22cf19309d41d3bc" [[projects]] branch = "master" @@ -58,7 +56,11 @@ [[projects]] branch = "master" name = "github.com/mailru/easyjson" - packages = ["buffer","jlexer","jwriter"] + packages = [ + "buffer", + "jlexer", + "jwriter" + ] revision = "32fa128f234d041f196a9f3e0fea5ac9772c08e1" [[projects]] @@ -81,7 +83,11 @@ [[projects]] name = "github.com/stretchr/testify" - packages = ["assert","require","suite"] + packages = [ + "assert", + "require", + "suite" + ] revision = "b91bfb9ebec76498946beb6af7c0230c7cc7ba6c" version = "v1.2.0" @@ -112,13 +118,32 @@ [[projects]] branch = "master" name = "golang.org/x/sys" - packages = ["unix","windows"] + packages = [ + "unix", + "windows" + ] revision = "af50095a40f9041b3b38960738837185c26e9419" [[projects]] branch = "master" name = "golang.org/x/text" - packages = ["collate","collate/build","internal/colltab","internal/gen","internal/tag","internal/triegen","internal/ucd","language","secure/bidirule","transform","unicode/bidi","unicode/cldr","unicode/norm","unicode/rangetable","width"] + packages = [ + "collate", + "collate/build", + "internal/colltab", + "internal/gen", + "internal/tag", + "internal/triegen", + "internal/ucd", + "language", + "secure/bidirule", + "transform", + "unicode/bidi", + "unicode/cldr", + "unicode/norm", + "unicode/rangetable", + "width" + ] revision = "e19ae1496984b1c655b8044a65c0300a3c878dd3" [[projects]] @@ -130,6 +155,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "b415982ca9ccadceeeb843f0f9e39885d12c72e6778f59de94ebf7eb4ca6ca32" + inputs-digest = "81826861e559c7134b377fb8590c7682e025911f3ba3f436b8901ff3e7ccc7cf" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index ff5e801..51678ec 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -26,22 +26,22 @@ version = "1.2.0" [[constraint]] - branch = "master" name = "github.com/go-openapi/spec" + revision = "f3499b5df53897321d6f5e9c22cf19309d41d3bc" -[[constraint]] +[[override]] name = "github.com/pkg/errors" version = "0.8.0" -[[constraint]] +[[override]] name = "github.com/sirupsen/logrus" version = "1.0.4" -[[constraint]] +[[override]] name = "github.com/stretchr/testify" version = "1.2.0" -[[constraint]] +[[override]] name = "github.com/urfave/cli" version = "1.20.0" @@ -50,5 +50,5 @@ version = "2.2.0" [[constraint]] - branch = "master" name = "github.com/emicklei/proto-contrib" + revision = "1a0b77302cc833fcf469702a8737b893a91a905c" diff --git a/vendor/github.com/emicklei/proto-contrib/.gitignore b/vendor/github.com/emicklei/proto-contrib/.gitignore new file mode 100644 index 0000000..8741f5d --- /dev/null +++ b/vendor/github.com/emicklei/proto-contrib/.gitignore @@ -0,0 +1,2 @@ +.idea/ +bin/ \ No newline at end of file diff --git a/vendor/github.com/emicklei/proto-contrib/README.md b/vendor/github.com/emicklei/proto-contrib/README.md index f29daf0..ce3fd28 100644 --- a/vendor/github.com/emicklei/proto-contrib/README.md +++ b/vendor/github.com/emicklei/proto-contrib/README.md @@ -24,19 +24,23 @@ See folder `cmd/proto2xsd/README.md` for more details. > proto2gql -help Usage of proto2gql [flags] [path ...] - -std_out - Writes transformed files to stdout - -txt_out string - Writes transformed files to .graphql file + -filter string + Regexp to filter out matched types + -filterN string + Regexp to filter out not matched types -go_out string Writes transformed files to .go file -js_out string Writes transformed files to .js file + -no_prefix + Disables package prefix for type names -package_alias value Renames packages using given aliases -resolve_import value Resolves given external packages - -no_prefix - Disables package prefix for type names + -std_out + Writes transformed files to stdout + -txt_out string + Writes transformed files to .graphql file See folder `cmd/proto2gql/README.md` for more details. \ No newline at end of file diff --git a/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/Makefile b/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/Makefile index 6ed3dac..e86c533 100644 --- a/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/Makefile +++ b/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/Makefile @@ -3,7 +3,10 @@ default: test compile compile: - go build -v -o ./proto2gql ./*.go + go build -v -o ../../bin/proto2gql ./main.go + +fmt: + go fmt ./*.go test: go test ./*.go diff --git a/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/README.md b/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/README.md index 6ad65a0..b0662bf 100644 --- a/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/README.md +++ b/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/README.md @@ -5,20 +5,24 @@ GraphQL Schema conversion tool for Google Protocol Buffers version 3 > proto2gql -help Usage of proto2gql [flags] [path ...] - -std_out - Writes transformed files to stdout - -txt_out string - Writes transformed files to .graphql file + -filter string + Regexp to filter out matched types + -filterN string + Regexp to filter out not matched types -go_out string Writes transformed files to .go file -js_out string Writes transformed files to .js file + -no_prefix + Disables package prefix for type names -package_alias value Renames packages using given aliases -resolve_import value Resolves given external packages - -no_prefix - Disables package prefix for type names + -std_out + Writes transformed files to stdout + -txt_out string + Writes transformed files to .graphql file ### build make \ No newline at end of file diff --git a/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/main.go b/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/main.go index e8c561b..10f1b28 100644 --- a/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/main.go +++ b/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/main.go @@ -2,10 +2,13 @@ package main import ( "flag" - "fmt" + "github.com/emicklei/proto-contrib/pkg/proto2gql" + "github.com/emicklei/proto-contrib/pkg/proto2gql/writers" "io" + "log" "os" "path/filepath" + "regexp" "strings" ) @@ -48,6 +51,10 @@ var ( packageAliases StringMap + filter string + + filterN string + noPrefix bool ) @@ -61,6 +68,8 @@ func main() { flag.StringVar(&jsOut, "js_out", "", "Writes transformed files to .js file") flag.Var(&resolveImports, "resolve_import", "Resolves given external packages") flag.Var(&packageAliases, "package_alias", "Renames packages using given aliases") + flag.StringVar(&filter, "filter", "", "Regexp to filter out matched custom types") + flag.StringVar(&filterN, "filterN", "", "Regexp to filter out not matched custom types") flag.BoolVar(&noPrefix, "no_prefix", false, "Disables package prefix for type names") flag.Parse() @@ -70,95 +79,139 @@ func main() { os.Exit(0) } - var transformer *Transformer - writers := make([]io.Writer, 0, 5) + var transformer *proto2gql.Transformer + ws := make([]io.Writer, 0, 5) if stdOut == true { - writers = append(writers, os.Stdout) + ws = append(ws, os.Stdout) } if txtOut != "" { writer, err := createTextWriter(txtOut) if err != nil { - gracefullyTerminate(err, writers) + gracefullyTerminate(err, ws) } - writers = append(writers, writer) + ws = append(ws, writer) } if goOut != "" { writer, err := createGoWriter(goOut) if err != nil { - gracefullyTerminate(err, writers) + gracefullyTerminate(err, ws) } - writers = append(writers, writer) + ws = append(ws, writer) } if jsOut != "" { writer, err := createJsWriter(jsOut) if err != nil { - gracefullyTerminate(err, writers) + gracefullyTerminate(err, ws) } - writers = append(writers, writer) + ws = append(ws, writer) } - if len(writers) == 0 { - fmt.Println("output not defined") + if len(ws) == 0 { + log.Println("output not defined") os.Exit(0) } - transformer = NewTransformer( - io.MultiWriter(writers...), + transformer = proto2gql.NewTransformer( + io.MultiWriter(ws...), withResolvingImports(resolveImports), withPackageAliases(packageAliases), withNoPrefix(noPrefix), + withFilter(filter, filterN), ) - var err error - for _, filename := range flag.Args() { - if err = readAndTransform(filename, transformer); err != nil { - fmt.Println(err.Error()) - break + if err := readAndTransform(filename, transformer); err != nil { + log.Fatalln("failed to transform file: " + err.Error()) } } - if saveErr := saveWriters(writers); saveErr != nil && err == nil { - err = saveErr - } - - if err != nil { - os.Exit(1) + if err := saveWriters(ws); err != nil { + log.Fatalln("failed to save output: " + err.Error()) } } -func withResolvingImports(imports StringMap) func(transformer *Transformer) { - return func(t *Transformer) { +func withResolvingImports(imports StringMap) func(transformer *proto2gql.Transformer) { + return func(t *proto2gql.Transformer) { for key, url := range imports { t.Import(key, url) } } } -func withPackageAliases(aliases StringMap) func(transformer *Transformer) { - return func(t *Transformer) { +func withPackageAliases(aliases StringMap) func(transformer *proto2gql.Transformer) { + return func(t *proto2gql.Transformer) { for pkg, alias := range aliases { t.SetPackageAlias(pkg, alias) } } } -func withNoPrefix(noPrefix bool) func(transformer *Transformer) { - return func(t *Transformer) { +func withNoPrefix(noPrefix bool) func(transformer *proto2gql.Transformer) { + return func(t *proto2gql.Transformer) { t.DisablePrefix(noPrefix) } } +func withFilter(positive, negative string) func(transformer *proto2gql.Transformer) { + return func(t *proto2gql.Transformer) { + if positive == "" && negative == "" { + return + } + + chain := make([]func(typeName string) bool, 0, 2) + + if positive != "" { + rPos, err := regexp.Compile(positive) + + if err != nil { + log.Fatalln("invalid regular expression: " + err.Error()) + } + + chain = append(chain, func(typeName string) bool { + // filter out matched types + return rPos.Match([]byte(typeName)) == false + }) + } + + if negative != "" { + rNeg, err := regexp.Compile(negative) + + if err != nil { + panic("invalid regular expression: " + err.Error()) + } + + chain = append(chain, func(typeName string) bool { + // filter out not matched types + return rNeg.Match([]byte(typeName)) + }) + } + + t.SetFilter(func(typeName string) bool { + res := true + + for _, r := range chain { + res = r(typeName) + + if res == false { + break + } + } + + return res + }) + } +} + func ensureExtension(filename, expectedExt string) string { ext := filepath.Ext(filename) @@ -174,7 +227,7 @@ func ensureExtension(filename, expectedExt string) string { } func createTextWriter(filename string) (io.Writer, error) { - return NewFileWriter(ensureExtension(filename, ".graphql"), "", "") + return writers.NewFileWriter(ensureExtension(filename, ".graphql"), "", "") } func createGoWriter(filename string) (io.Writer, error) { @@ -190,27 +243,27 @@ func createGoWriter(filename string) (io.Writer, error) { openTag := "package " + filepath.Base(filepath.Dir(abs)) + "\n \n" openTag += "var " + strings.Title(name) + " = `\n" - return NewFileWriter(filename, openTag, "\n`") + return writers.NewFileWriter(filename, openTag, "\n`") } func createJsWriter(filename string) (io.Writer, error) { openTag := "module.exports = `\n" - return NewFileWriter(ensureExtension(filename, ".js"), openTag, "\n`") + return writers.NewFileWriter(ensureExtension(filename, ".js"), openTag, "\n`") } -func saveWriters(writers []io.Writer) error { +func saveWriters(ws []io.Writer) error { var err error - for _, writer := range writers { - fw, ok := writer.(*FileWriter) + for _, writer := range ws { + fw, ok := writer.(*writers.FileWriter) if ok == true { if err == nil { err = fw.Save() if err != nil { - fmt.Println(err.Error()) + break } } else { fw.Remove() @@ -221,20 +274,19 @@ func saveWriters(writers []io.Writer) error { return err } -func gracefullyTerminate(err error, writers []io.Writer) { - for _, writer := range writers { - fw, ok := writer.(*FileWriter) +func gracefullyTerminate(err error, ws []io.Writer) { + for _, writer := range ws { + fw, ok := writer.(*writers.FileWriter) if ok == true { fw.Remove() } } - fmt.Println("error occured: " + err.Error()) - os.Exit(1) + log.Fatalln("error occurred: " + err.Error()) } -func readAndTransform(filename string, transformer *Transformer) error { +func readAndTransform(filename string, transformer *proto2gql.Transformer) error { // open for read file, err := os.Open(filename) diff --git a/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/scope.go b/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/scope.go deleted file mode 100644 index fd85a96..0000000 --- a/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/scope.go +++ /dev/null @@ -1,153 +0,0 @@ -package main - -import ( - "path" - "strings" -) - -type ( - Type struct { - packageName string - name string - } - - Scope struct { - converter *Converter - packageName string - path []string - types map[string]*Type - imports map[string]*Type - children map[string]*Scope - } -) - -func NewScope(converter *Converter) *Scope { - return &Scope{ - converter: converter, - path: make([]string, 0, 5), - types: make(map[string]*Type), - imports: make(map[string]*Type), - children: make(map[string]*Scope), - } -} - -func (s *Scope) Fork(name string) *Scope { - p := make([]string, len(s.path)) - - copy(p, s.path) - - childScope := &Scope{ - converter: s.converter, - packageName: s.packageName, - types: s.types, // share types collection - path: append(p, name), - children: make(map[string]*Scope), - } - - s.children[name] = childScope - - return childScope -} - -func (s *Scope) SetPackageName(name string) { - s.packageName = s.converter.PackageName(strings.Split(name, ".")) -} - -func (s *Scope) AddLocalType(name string) { - typeName := s.converter.OriginalTypeName(s, name) - - _, ok := s.types[typeName] - - if ok == false { - s.types[typeName] = &Type{ - packageName: s.packageName, - name: s.converter.NewTypeName(s, name), - } - } -} - -func (s *Scope) AddImportedType(filename string) { - dir := path.Dir(filename) - name := strings.Replace(path.Base(filename), ".proto", "", -1) - ref := strings.Replace(dir, "/", ".", -1) - - var packageName string - - if dir == "." { - packageName = s.packageName - } else { - packageName = s.converter.PackageName(strings.Split(dir, "/")) - } - - s.imports[ref] = &Type{packageName, name} -} - -func (s *Scope) ResolveTypeName(ref string) string { - builtin, ok := BUILTINS[ref] - - if ok == true { - return builtin - } - - // try to find one in a global scope - scoped, ok := s.types[ref] - - if ok == true { - return scoped.name - } - - // try to find one among nested types - nested, ok := s.types[s.converter.OriginalTypeName(s, ref)] - - if ok == true { - return nested.name - } - - var foundInChildren string - - for _, childScope := range s.children { - res := childScope.ResolveTypeName(ref) - - if res != ref { - foundInChildren = res - break - } - } - - if foundInChildren != "" { - return foundInChildren - } - - // if we are still here, probably it's an imported type - - // if type does not contain "." it means it's from the same package - if strings.Contains(ref, ".") == false { - // from the same package - imported, ok := s.imports["."] - - if ok == true { - return imported.packageName + ref - } - } else { - // if it has "." it means it's from other package - parts := strings.Split(ref, ".") - - var tail string - - for idx, segment := range parts { - if tail == "" { - tail = segment - } else { - tail += "." + segment - } - - imported, ok := s.imports[tail] - - if ok == true { - return imported.packageName + strings.Join(parts[idx+1:], "") - } - } - } - - return ref -} diff --git a/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/converter.go b/vendor/github.com/emicklei/proto-contrib/pkg/proto2gql/converter.go similarity index 65% rename from vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/converter.go rename to vendor/github.com/emicklei/proto-contrib/pkg/proto2gql/converter.go index 701603b..18739ad 100644 --- a/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/converter.go +++ b/vendor/github.com/emicklei/proto-contrib/pkg/proto2gql/converter.go @@ -1,14 +1,14 @@ -package main +package proto2gql import "strings" type Converter struct { - noPrefix bool + noPrefix bool pkgAliases map[string]string } func (c *Converter) NewTypeName(scope *Scope, name string) string { - return scope.packageName + strings.Join(scope.path, "") + name + return scope.convertedPackageName + strings.Join(scope.path, "") + name } func (c *Converter) OriginalTypeName(scope *Scope, name string) string { @@ -22,6 +22,15 @@ func (c *Converter) OriginalTypeName(scope *Scope, name string) string { } } +func (c *Converter) OriginalFullTypeName(scope *Scope, name string) string { + switch len(scope.path) { + case 0: + return scope.originalPackageName + "." + name + default: + return scope.originalPackageName + "." + strings.Join(scope.path, ".") + "." + name + } +} + func (c *Converter) PackageName(parts []string) string { var name string diff --git a/vendor/github.com/emicklei/proto-contrib/pkg/proto2gql/scope.go b/vendor/github.com/emicklei/proto-contrib/pkg/proto2gql/scope.go new file mode 100644 index 0000000..4de45c4 --- /dev/null +++ b/vendor/github.com/emicklei/proto-contrib/pkg/proto2gql/scope.go @@ -0,0 +1,221 @@ +package proto2gql + +import ( + "path" + "path/filepath" + "strings" +) + +type ( + Type struct { + originalPackageName string + originalName string + convertedPackageName string + convertedName string + } + + Scope struct { + converter *Converter + originalPackageName string + convertedPackageName string + path []string + types map[string]*Type + imports map[string]*Type + children map[string]*Scope + } +) + +func NewScope(converter *Converter) *Scope { + return &Scope{ + converter: converter, + path: make([]string, 0, 5), + types: make(map[string]*Type), + imports: make(map[string]*Type), + children: make(map[string]*Scope), + } +} + +func (s *Scope) Fork(name string) *Scope { + p := make([]string, len(s.path)) + + copy(p, s.path) + + childScope := &Scope{ + converter: s.converter, + originalPackageName: s.originalPackageName, + convertedPackageName: s.convertedPackageName, + types: s.types, // share types collection + path: append(p, name), + children: make(map[string]*Scope), + } + + s.children[name] = childScope + + return childScope +} + +func (s *Scope) SetPackageName(name string) { + s.originalPackageName = name + s.convertedPackageName = s.converter.PackageName(strings.Split(name, ".")) +} + +func (s *Scope) AddLocalType(name string) { + typeName := s.converter.OriginalTypeName(s, name) + + _, ok := s.types[typeName] + + if ok == false { + s.types[typeName] = &Type{ + originalPackageName: s.originalPackageName, + originalName: name, + convertedPackageName: s.convertedPackageName, + convertedName: s.converter.NewTypeName(s, name), + } + } +} + +func (s *Scope) AddImportedType(filename string) { + dir := path.Dir(filename) + separator := string(filepath.Separator) + name := strings.ToUpper(strings.Replace(path.Base(filename), ".proto", "", -1)) + ref := strings.Replace(dir, separator, ".", -1) + + var originalPackageName string + var convertedPackageName string + + if dir == "." { + originalPackageName = s.originalPackageName + convertedPackageName = s.convertedPackageName + } else { + originalPackageName = strings.Replace(dir, separator, ".", -1) + convertedPackageName = s.converter.PackageName(strings.Split(dir, separator)) + } + + s.imports[ref] = &Type{ + originalPackageName: originalPackageName, + originalName: name, + convertedPackageName: convertedPackageName, + convertedName: name, + } +} + +func (s *Scope) ResolveConvertedTypeName(ref string) string { + builtin, ok := BUILTINS[ref] + + if ok == true { + return builtin + } + + // try to find one in a global scope + scoped, ok := s.types[ref] + + if ok == true { + return scoped.convertedName + } + + // try to find one among nested types + nested, ok := s.types[s.converter.OriginalTypeName(s, ref)] + + if ok == true { + return nested.convertedName + } + + var foundInChildren string + + for _, childScope := range s.children { + res := childScope.ResolveConvertedTypeName(ref) + + if res != ref { + foundInChildren = res + break + } + } + + if foundInChildren != "" { + return foundInChildren + } + + // if we are still here, probably it's an imported type + + // if type does not contain "." it means it's from the same package + if strings.Contains(ref, ".") == false { + // from the same package + imported, ok := s.imports["."] + + if ok == true { + return imported.convertedPackageName + ref + } + } else { + // if it has "." it means it's from other package + parts := strings.Split(ref, ".") + + var tail string + + for idx, segment := range parts { + if tail == "" { + tail = segment + } else { + tail += "." + segment + } + + imported, ok := s.imports[tail] + + if ok == true { + return imported.convertedPackageName + strings.Join(parts[idx+1:], "") + } + } + } + + return ref +} + +func (s *Scope) ResolveFullTypeName(ref string) string { + builtin, ok := BUILTINS[ref] + + if ok == true { + return builtin + } + + // try to find one in a global scope + scoped, ok := s.types[ref] + + if ok == true { + return s.converter.OriginalFullTypeName(s, scoped.originalName) + } + + // try to find one among nested types + nested, ok := s.types[s.converter.OriginalTypeName(s, ref)] + + if ok == true { + return s.converter.OriginalFullTypeName(s, nested.originalName) + } + + var foundInChildren string + + for _, childScope := range s.children { + res := childScope.ResolveFullTypeName(ref) + + if res != ref { + foundInChildren = res + break + } + } + + if foundInChildren != "" { + return foundInChildren + } + + // if we are still here, probably it's an imported type + + // if type does not contain "." it means it's from the same package + if strings.Contains(ref, ".") == false { + // from the same package + imported, ok := s.imports["."] + + if ok == true { + return imported.originalPackageName + "." + imported.originalName + } + } + + return ref +} diff --git a/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/transformer.go b/vendor/github.com/emicklei/proto-contrib/pkg/proto2gql/transformer.go similarity index 90% rename from vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/transformer.go rename to vendor/github.com/emicklei/proto-contrib/pkg/proto2gql/transformer.go index 6215d27..c125cfc 100644 --- a/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/transformer.go +++ b/vendor/github.com/emicklei/proto-contrib/pkg/proto2gql/transformer.go @@ -1,4 +1,4 @@ -package main +package proto2gql import ( "io" @@ -9,6 +9,8 @@ import ( ) type ( + Filter = func(typeName string) bool + ExternalPackage struct { url string resolved bool @@ -20,9 +22,14 @@ type ( imports map[string]*ExternalPackage pkgAliases map[string]string noPrefix bool + filter Filter } ) +func bypassFilter(_ string) bool { + return true +} + func NewTransformer(out io.Writer, opts ...func(transformer *Transformer)) *Transformer { res := &Transformer{ out, @@ -30,6 +37,7 @@ func NewTransformer(out io.Writer, opts ...func(transformer *Transformer)) *Tran make(map[string]*ExternalPackage), make(map[string]string), false, + bypassFilter, } for _, opt := range opts { @@ -61,6 +69,12 @@ func (t *Transformer) SetFilename(filename string) { t.filename = filename } +func (t *Transformer) SetFilter(filter Filter) { + if filter != nil { + t.filter = filter + } +} + func (t *Transformer) Transform(input io.Reader) error { parser := proto.NewParser(input) parser.Filename(t.filename) @@ -74,7 +88,7 @@ func (t *Transformer) Transform(input io.Reader) error { visitor := NewVisitor(&Converter{ noPrefix: t.noPrefix, pkgAliases: t.pkgAliases, - }) + }, t.filter) toDownload := make(map[string]*ExternalPackage) diff --git a/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/transformer_test.go b/vendor/github.com/emicklei/proto-contrib/pkg/proto2gql/transformer_test.go similarity index 72% rename from vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/transformer_test.go rename to vendor/github.com/emicklei/proto-contrib/pkg/proto2gql/transformer_test.go index 63afcf3..f70fb44 100644 --- a/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/transformer_test.go +++ b/vendor/github.com/emicklei/proto-contrib/pkg/proto2gql/transformer_test.go @@ -1,7 +1,9 @@ -package main +package proto2gql_test import ( "bytes" + "github.com/emicklei/proto-contrib/pkg/proto2gql" + "regexp" "strings" "testing" ) @@ -33,7 +35,7 @@ func TestTransformBasicMessage(t *testing.T) { input.Write(schema) output := new(bytes.Buffer) - transformer := NewTransformer(output) + transformer := proto2gql.NewTransformer(output) if err := transformer.Transform(input); err != nil { t.Fatal(err) @@ -98,7 +100,7 @@ func TestTransformNestedMessages(t *testing.T) { input.Write(schema) output := new(bytes.Buffer) - transformer := NewTransformer(output) + transformer := proto2gql.NewTransformer(output) if err := transformer.Transform(input); err != nil { t.Fatal(err) @@ -161,7 +163,7 @@ message SomeOtherMessage { input.Write(schema) output := new(bytes.Buffer) - transformer := NewTransformer(output) + transformer := proto2gql.NewTransformer(output) if err := transformer.Transform(input); err != nil { t.Fatal(err) @@ -212,7 +214,7 @@ message SearchResponse { input.Write(schema) output := new(bytes.Buffer) - transformer := NewTransformer(output) + transformer := proto2gql.NewTransformer(output) if err := transformer.Transform(input); err != nil { t.Fatal(err) @@ -260,7 +262,7 @@ func TestTransformNestedTypes(t *testing.T) { input.Write(schema) output := new(bytes.Buffer) - transformer := NewTransformer(output) + transformer := proto2gql.NewTransformer(output) if err := transformer.Transform(input); err != nil { t.Fatal(err) @@ -308,7 +310,7 @@ func TestTransformEnums(t *testing.T) { input.Write(schema) output := new(bytes.Buffer) - transformer := NewTransformer(output) + transformer := proto2gql.NewTransformer(output) if err := transformer.Transform(input); err != nil { t.Fatal(err) @@ -355,7 +357,7 @@ func TestTransformNestedEnums(t *testing.T) { input.Write(schema) output := new(bytes.Buffer) - transformer := NewTransformer(output) + transformer := proto2gql.NewTransformer(output) if err := transformer.Transform(input); err != nil { t.Fatal(err) @@ -399,7 +401,7 @@ func TestTransformImportedTypes(t *testing.T) { input.Write(schema) output := new(bytes.Buffer) - transformer := NewTransformer(output) + transformer := proto2gql.NewTransformer(output) if err := transformer.Transform(input); err != nil { t.Fatal(err) @@ -436,7 +438,7 @@ func TestTransformImportedNestedTypes(t *testing.T) { input.Write(schema) output := new(bytes.Buffer) - transformer := NewTransformer(output) + transformer := proto2gql.NewTransformer(output) if err := transformer.Transform(input); err != nil { t.Fatal(err) @@ -473,7 +475,7 @@ func TestTransformImportedTypesFromSamePackage(t *testing.T) { input.Write(schema) output := new(bytes.Buffer) - transformer := NewTransformer(output) + transformer := proto2gql.NewTransformer(output) if err := transformer.Transform(input); err != nil { t.Fatal(err) @@ -510,7 +512,7 @@ func TestTransformResolveImportedTypes(t *testing.T) { input.Write(schema) output := new(bytes.Buffer) - transformer := NewTransformer(output) + transformer := proto2gql.NewTransformer(output) transformer.Import("google/protobuf/timestamp.proto", "https://raw.githubusercontent.com/google/protobuf/master/src/google/protobuf/timestamp.proto") @@ -567,7 +569,7 @@ func TestTransformWithNoTypePrefix(t *testing.T) { input.Write(schema) output := new(bytes.Buffer) - transformer := NewTransformer(output) + transformer := proto2gql.NewTransformer(output) transformer.DisablePrefix(true) transformer.Import("google/protobuf/timestamp.proto", "https://raw.githubusercontent.com/google/protobuf/master/src/google/protobuf/timestamp.proto") @@ -639,7 +641,7 @@ func TestTransformWithPackageAliases(t *testing.T) { input.Write(schema) output := new(bytes.Buffer) - transformer := NewTransformer(output) + transformer := proto2gql.NewTransformer(output) transformer.SetPackageAlias("test", "Dashboard") transformer.SetPackageAlias("google.protobuf", "") transformer.Import("google/protobuf/timestamp.proto", "https://raw.githubusercontent.com/google/protobuf/master/src/google/protobuf/timestamp.proto") @@ -710,7 +712,7 @@ message Contact { input.Write(schema) output := new(bytes.Buffer) - transformer := NewTransformer(output) + transformer := proto2gql.NewTransformer(output) transformer.SetPackageAlias("com.users.api", "User") if err := transformer.Transform(input); err != nil { @@ -745,3 +747,249 @@ type UserContact { t.Fatalf("Expected %s to equal to %s", expected, actual) } } + +func TestTransformWithSimpleFiltering(t *testing.T) { + schema := []byte(` +syntax = "proto3"; +package test; + +message A { + string name = 1; +} + +message B { + string name = 1; +} + `) + + input := new(bytes.Buffer) + input.Write(schema) + + output := new(bytes.Buffer) + transformer := proto2gql.NewTransformer(output) + + transformer.SetFilter(func(typeName string) bool { + return typeName != "test.B" + }) + + if err := transformer.Transform(input); err != nil { + t.Fatal(err) + } + + expected := ` +type TestA { + name: String +} + ` + + expected = strings.TrimSpace(expected) + actual := strings.TrimSpace(output.String()) + + if expected != actual { + t.Fatalf("Expected %s to equal to %s", expected, actual) + } +} + +func TestTransformWithNestedFiltering(t *testing.T) { + schema := []byte(` +syntax = "proto3"; +package test; + +message A { + string name = 1; + + message B { + string name = 1; + } +} + `) + + input := new(bytes.Buffer) + input.Write(schema) + + output := new(bytes.Buffer) + transformer := proto2gql.NewTransformer(output) + + transformer.SetFilter(func(typeName string) bool { + return typeName != "test.A.B" + }) + + if err := transformer.Transform(input); err != nil { + t.Fatal(err) + } + + expected := ` +type TestA { + name: String +} + ` + + expected = strings.TrimSpace(expected) + actual := strings.TrimSpace(output.String()) + + if expected != actual { + t.Fatalf("Expected %s to equal to %s", expected, actual) + } +} + +func TestTransformFieldFiltering(t *testing.T) { + schema := []byte(` + syntax = "proto3"; + package my.app; + + message A { + int64 field_int64 = 1; + } + + message B { + A field_a = 1; + } + + message C { + repeated B field_b = 1; + string field_str = 2; + } + `) + + input := new(bytes.Buffer) + input.Write(schema) + + output := new(bytes.Buffer) + transformer := proto2gql.NewTransformer(output) + + transformer.SetFilter(func(typeName string) bool { + return typeName != "my.app.B" + }) + + if err := transformer.Transform(input); err != nil { + t.Fatal(err) + } + + expected := ` +type MyAppA { + field_int64: Int +} + +type MyAppC { + field_str: String +} +` + + expected = strings.TrimSpace(expected) + actual := strings.TrimSpace(output.String()) + + if expected != actual { + t.Fatalf("Expected %s to equal to %s", expected, actual) + } +} + +func TestTransformExternalFieldFiltering(t *testing.T) { + schema := []byte(` + syntax = "proto3"; + package test; + + import "google/protobuf/timestamp.proto"; + + message Timestamp { + google.protobuf.Timestamp time = 1; + } + + message A { + int64 field_int64 = 1; + } + + message B { + google.protobuf.Timestamp timestamp = 1; + string field_str = 2; + } + `) + + input := new(bytes.Buffer) + input.Write(schema) + + output := new(bytes.Buffer) + transformer := proto2gql.NewTransformer(output) + + transformer.SetFilter(func(typeName string) bool { + return typeName != "google.protobuf.Timestamp" + }) + + if err := transformer.Transform(input); err != nil { + t.Fatal(err) + } + + expected := ` +type TestTimestamp { +} + +type TestA { + field_int64: Int +} + +type TestB { + field_str: String +} +` + + expected = strings.TrimSpace(expected) + actual := strings.TrimSpace(output.String()) + + if expected != actual { + t.Fatalf("Expected %s to equal to %s", expected, actual) + } +} + +func TestTransformFilterByRegexp(t *testing.T) { + schema := []byte(` +syntax = "proto3"; +package my.app; + +message User { + string first_name = 1; + string last_name = 2; +} + +message GetUserRequest { + string correlation_id = 1; + string id = 2; +} + +message GetUserResponse { + User data = 1; +} + +`) + + input := new(bytes.Buffer) + input.Write(schema) + + output := new(bytes.Buffer) + transformer := proto2gql.NewTransformer(output) + + r := regexp.MustCompile("(Request)|(Response)") + + transformer.SetFilter(func(typeName string) bool { + return r.Match([]byte(typeName)) + }) + + if err := transformer.Transform(input); err != nil { + t.Fatal(err) + } + + expected := ` +type MyAppGetUserRequest { + correlation_id: String + id: String +} + +type MyAppGetUserResponse { +} +` + + expected = strings.TrimSpace(expected) + actual := strings.TrimSpace(output.String()) + + if expected != actual { + t.Fatalf("Expected %s to equal to %s", expected, actual) + } +} diff --git a/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/visitor.go b/vendor/github.com/emicklei/proto-contrib/pkg/proto2gql/visitor.go similarity index 74% rename from vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/visitor.go rename to vendor/github.com/emicklei/proto-contrib/pkg/proto2gql/visitor.go index 8545738..0b720ec 100644 --- a/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/visitor.go +++ b/vendor/github.com/emicklei/proto-contrib/pkg/proto2gql/visitor.go @@ -1,9 +1,10 @@ -package main +package proto2gql import ( "bytes" "github.com/emicklei/proto" "io" + "strings" ) var BUILTINS = map[string]string{ @@ -29,14 +30,16 @@ type ( scope *Scope buff *bytes.Buffer children []*Visitor + filter Filter } ) -func NewVisitor(converter *Converter) *Visitor { +func NewVisitor(converter *Converter, filter Filter) *Visitor { return &Visitor{ buff: new(bytes.Buffer), children: make([]*Visitor, 0, 5), scope: NewScope(converter), + filter: filter, } } @@ -45,6 +48,7 @@ func (v *Visitor) Fork(name string) *Visitor { buff: new(bytes.Buffer), children: make([]*Visitor, 0, 5), scope: v.scope.Fork(name), + filter: v.filter, } v.children = append(v.children, child) @@ -63,10 +67,15 @@ func (v *Visitor) Flush(out io.Writer) { } func (v *Visitor) VisitMessage(m *proto.Message) { - v.buff.WriteString("\n") - + // we add it to be able to resolve it in fields v.scope.AddLocalType(m.Name) + if v.canTransformMessage(m) == false { + return + } + + v.buff.WriteString("\n") + v.buff.WriteString("type " + v.scope.converter.NewTypeName(v.scope, m.Name) + " {\n") fields := make([]*proto.NormalField, 0, len(m.Elements)) @@ -82,8 +91,8 @@ func (v *Visitor) VisitMessage(m *proto.Message) { fields = append(fields, field) } else { // if so, create a nested visitor - // we need to track a parent's name - // in order to generate a unique name for nested ones + // we need to track a parent's convertedName + // in order to generate a unique convertedName for nested ones // we create another visitor in order to unfold nested types since GraphQL does not support nested types element.Accept(v.Fork(m.Name)) } @@ -107,9 +116,13 @@ func (v *Visitor) VisitImport(i *proto.Import) { v.scope.AddImportedType(i.Filename) } func (v *Visitor) VisitNormalField(field *proto.NormalField) { + if v.canTransformMessageField(field) == false { + return + } + v.buff.WriteString(" " + field.Name + ":") - typeName := v.scope.ResolveTypeName(field.Type) + typeName := v.scope.ResolveConvertedTypeName(field.Type) if field.Repeated == false { v.buff.WriteString(" " + typeName) @@ -127,8 +140,13 @@ func (v *Visitor) VisitEnumField(i *proto.EnumField) { v.buff.WriteString(" " + i.Name + "\n") } func (v *Visitor) VisitEnum(e *proto.Enum) { + // we add it to be able to resolve it in fields v.scope.AddLocalType(e.Name) + if v.canTransformEnum(e) == false { + return + } + v.buff.WriteString("\n") v.buff.WriteString("enum " + v.scope.converter.NewTypeName(v.scope, e.Name) + " {\n") @@ -149,3 +167,26 @@ func (v *Visitor) VisitMapField(f *proto.MapField) {} // proto2 func (v *Visitor) VisitGroup(g *proto.Group) {} func (v *Visitor) VisitExtensions(e *proto.Extensions) {} + +func (v *Visitor) canTransformMessage(m *proto.Message) bool { + return v.filter(v.scope.converter.OriginalFullTypeName(v.scope, m.Name)) +} + +func (v *Visitor) canTransformMessageField(m *proto.NormalField) bool { + // ignore builtins + _, builtin := BUILTINS[m.Type] + + if builtin == true { + return true + } + + if strings.Contains(m.Type, ".") { + return v.filter(m.Type) + } + + return v.filter(v.scope.ResolveFullTypeName(m.Type)) +} + +func (v *Visitor) canTransformEnum(e *proto.Enum) bool { + return v.filter(v.scope.converter.OriginalFullTypeName(v.scope, e.Name)) +} diff --git a/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/file.go b/vendor/github.com/emicklei/proto-contrib/pkg/proto2gql/writers/file.go similarity index 98% rename from vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/file.go rename to vendor/github.com/emicklei/proto-contrib/pkg/proto2gql/writers/file.go index 0e413c0..61a4610 100644 --- a/vendor/github.com/emicklei/proto-contrib/cmd/proto2gql/file.go +++ b/vendor/github.com/emicklei/proto-contrib/pkg/proto2gql/writers/file.go @@ -1,8 +1,8 @@ -package main +package writers import ( - "os" "io/ioutil" + "os" "path/filepath" ) @@ -75,4 +75,4 @@ func (fw *FileWriter) Close() error { } return nil -} \ No newline at end of file +} diff --git a/vendor/github.com/go-openapi/spec/expander.go b/vendor/github.com/go-openapi/spec/expander.go index 59a2270..dc33d63 100644 --- a/vendor/github.com/go-openapi/spec/expander.go +++ b/vendor/github.com/go-openapi/spec/expander.go @@ -16,9 +16,7 @@ package spec import ( "encoding/json" - "errors" "fmt" - "io/ioutil" "log" "net/url" "os" @@ -379,29 +377,26 @@ func (r *schemaLoader) resolveRef(ref *Ref, target interface{}, basePath string) return nil } - // if no basePath is provided, we attempt to resolve the reference against root - if basePath == "" { - var b []byte - switch rr := r.root.(type) { - case *Schema: - b, _ = rr.MarshalJSON() - case *Swagger: - b, _ = rr.MarshalJSON() + var data interface{} + var err error + // Resolve against the root if it isn't nil, and if ref is pointing at the root, or has a fragment only which means + // it is pointing somewhere in the root. + root := r.root + if (ref.IsRoot() || ref.HasFragmentOnly) && root == nil && basePath != "" { + if baseRef, err := NewRef(basePath); err == nil { + root, _, _, _ = r.load(baseRef.GetURL()) } - f, err := ioutil.TempFile(os.TempDir(), "tmproot") + } + if (ref.IsRoot() || ref.HasFragmentOnly) && root != nil { + data = root + } else { + baseRef := normalizeFileRef(ref, basePath) + debugLog("current ref is: %s", ref.String()) + debugLog("current ref normalized file: %s", baseRef.String()) + data, _, _, err = r.load(baseRef.GetURL()) if err != nil { return err } - f.Write(b) - f.Close() - basePath = f.Name() - } - - baseRef := normalizeFileRef(ref, basePath) - debugLog("current ref normalized file: %s", baseRef.String()) - data, _, _, err := r.load(baseRef.GetURL()) - if err != nil { - return err } var res interface{} @@ -449,6 +444,9 @@ func (r *schemaLoader) Resolve(ref *Ref, target interface{}, basePath string) er // absPath returns the absolute path of a file func absPath(fname string) (string, error) { + if strings.HasPrefix(fname, "http") { + return fname, nil + } if filepath.IsAbs(fname) { return fname, nil } @@ -471,11 +469,10 @@ func ExpandSpec(spec *Swagger, options *ExpandOptions) error { } if options == nil || !options.SkipSchemas { - rt := fmt.Sprintf("%s#/definitions/", specBasePath) for key, definition := range spec.Definitions { var def *Schema var err error - if def, err = expandSchema(definition, []string{rt + key}, resolver, specBasePath); shouldStopOnError(err, resolver.options) { + if def, err = expandSchema(definition, []string{fmt.Sprintf("#/defintions/%s", key)}, resolver, specBasePath); shouldStopOnError(err, resolver.options) { return err } if def != nil { @@ -526,29 +523,19 @@ func shouldStopOnError(err error, opts *ExpandOptions) bool { // go-openapi/validate uses this function // notice that it is impossible to reference a json scema in a different file other than root func ExpandSchema(schema *Schema, root interface{}, cache ResolutionCache) error { - // if root is passed as nil, assume root is the same as schema - if root == nil { - root = schema - } - - file, err := ioutil.TempFile(os.TempDir(), "root") - if err != nil { - return err - } - defer os.Remove(file.Name()) - - switch r := root.(type) { - case *Schema: - b, _ := r.MarshalJSON() - file.Write(b) - case *Swagger: - b, _ := r.MarshalJSON() - file.Write(b) + // Only save the root to a tmp file if it isn't nil. + var base string + if root != nil { + base, _ = absPath("root") + if cache == nil { + cache = resCache + } + cache.Set(base, root) + base = "root" } - file.Close() opts := &ExpandOptions{ - RelativeBase: file.Name(), + RelativeBase: base, SkipSchemas: false, ContinueOnError: false, } @@ -561,15 +548,11 @@ func ExpandSchemaWithBasePath(schema *Schema, cache ResolutionCache, opts *Expan return nil } - if opts == nil { - return errors.New("cannot expand schema without a base path") - } - if opts.RelativeBase == "" { - return errors.New("cannot expand schema with empty base path") + var basePath string + if opts.RelativeBase != "" { + basePath, _ = absPath(opts.RelativeBase) } - basePath, _ := absPath(opts.RelativeBase) - resolver, err := defaultSchemaLoader(nil, opts, cache) if err != nil { return err diff --git a/vendor/github.com/go-openapi/spec/expander_test.go b/vendor/github.com/go-openapi/spec/expander_test.go index 0c83d79..cd2556d 100644 --- a/vendor/github.com/go-openapi/spec/expander_test.go +++ b/vendor/github.com/go-openapi/spec/expander_test.go @@ -626,6 +626,59 @@ func TestDefaultResolutionCache(t *testing.T) { assert.Equal(t, "here", sch) } +func TestRelativeBaseURI(t *testing.T) { + server := httptest.NewServer(http.FileServer(http.Dir("fixtures/remote"))) + defer server.Close() + + spec := new(Swagger) + // resolver, err := defaultSchemaLoader(spec, nil, nil) + // assert.NoError(t, err) + + err := ExpandSpec(spec, nil) + assert.NoError(t, err) + + specDoc, err := jsonDoc("fixtures/remote/all-the-things.json") + assert.NoError(t, err) + + opts := &ExpandOptions{ + RelativeBase: server.URL + "/all-the-things.json", + } + + spec = new(Swagger) + err = json.Unmarshal(specDoc, spec) + assert.NoError(t, err) + + pet := spec.Definitions["pet"] + errorModel := spec.Definitions["errorModel"] + petResponse := spec.Responses["petResponse"] + petResponse.Schema = &pet + stringResponse := spec.Responses["stringResponse"] + tagParam := spec.Parameters["tag"] + idParam := spec.Parameters["idParam"] + + err = ExpandSpec(spec, opts) + assert.NoError(t, err) + + assert.Equal(t, tagParam, spec.Parameters["query"]) + assert.Equal(t, petResponse, spec.Responses["petResponse"]) + assert.Equal(t, petResponse, spec.Responses["anotherPet"]) + assert.Equal(t, pet, *spec.Responses["petResponse"].Schema) + assert.Equal(t, stringResponse, *spec.Paths.Paths["/"].Get.Responses.Default) + assert.Equal(t, petResponse, spec.Paths.Paths["/"].Get.Responses.StatusCodeResponses[200]) + assert.Equal(t, pet, *spec.Paths.Paths["/pets"].Get.Responses.StatusCodeResponses[200].Schema.Items.Schema) + assert.Equal(t, errorModel, *spec.Paths.Paths["/pets"].Get.Responses.Default.Schema) + assert.Equal(t, pet, spec.Definitions["petInput"].AllOf[0]) + assert.Equal(t, spec.Definitions["petInput"], *spec.Paths.Paths["/pets"].Post.Parameters[0].Schema) + assert.Equal(t, petResponse, spec.Paths.Paths["/pets"].Post.Responses.StatusCodeResponses[200]) + assert.Equal(t, errorModel, *spec.Paths.Paths["/pets"].Post.Responses.Default.Schema) + pi := spec.Paths.Paths["/pets/{id}"] + assert.Equal(t, idParam, pi.Get.Parameters[0]) + assert.Equal(t, petResponse, pi.Get.Responses.StatusCodeResponses[200]) + assert.Equal(t, errorModel, *pi.Get.Responses.Default.Schema) + assert.Equal(t, idParam, pi.Delete.Parameters[0]) + assert.Equal(t, errorModel, *pi.Delete.Responses.Default.Schema) +} + func resolutionContextServer() *httptest.Server { var servedAt string server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { diff --git a/vendor/github.com/go-openapi/spec/fixtures/expansion/missingRef.json b/vendor/github.com/go-openapi/spec/fixtures/expansion/missingRef.json index 551449e..d597946 100644 --- a/vendor/github.com/go-openapi/spec/fixtures/expansion/missingRef.json +++ b/vendor/github.com/go-openapi/spec/fixtures/expansion/missingRef.json @@ -14,12 +14,12 @@ "schema": { "type": "array", "items": { - "$ref": "#/input/definitions/todo-full" + "$ref": "#/definitions/todo-full" } } }, "404": { - "$ref": "#/input/responses/404" + "$ref": "#/responses/404" } } } @@ -40,7 +40,7 @@ "todo-full": { "allOf": [ { - "$ref": "#/input/definitions/todo-partial" + "$ref": "#/definitions/todo-partial" }, { "type": "object", diff --git a/vendor/github.com/go-openapi/spec/fixtures/remote/all-the-things.json b/vendor/github.com/go-openapi/spec/fixtures/remote/all-the-things.json new file mode 100644 index 0000000..b25836f --- /dev/null +++ b/vendor/github.com/go-openapi/spec/fixtures/remote/all-the-things.json @@ -0,0 +1,242 @@ +{ + "swagger": "2.0", + "info": { + "version": "1.0.0", + "title": "Swagger Petstore", + "description": + "A sample API that uses a petstore as an example to demonstrate features in the swagger-2.0 specification", + "termsOfService": "http://helloreverb.com/terms/", + "contact": { + "name": "Wordnik API Team" + }, + "license": { + "name": "MIT" + } + }, + "host": "petstore.swagger.wordnik.com", + "basePath": "/api", + "schemes": ["http"], + "consumes": ["application/json"], + "produces": ["application/json"], + "parameters": { + "idParam": { + "name": "id", + "in": "path", + "description": "ID of pet to fetch", + "required": true, + "type": "integer", + "format": "int64" + }, + "tag": { + "type": "string", + "in": "query", + "required": false + }, + "query": { + "$ref": "#/parameters/tag" + } + }, + "responses": { + "petResponse": { + "description": "pet response", + "schema": { + "$ref": "pet/pet.json#/definitions/pet" + } + }, + "stringResponse": { + "descripion": "string response", + "schema": { + "type": "string" + } + }, + "anotherPet": { + "$ref": "#/responses/petResponse" + } + }, + "paths": { + "/": { + "get": { + "operationId": "indexStuff", + "responses": { + "default": { + "$ref": "#/responses/stringResponse" + }, + "200": { + "$ref": "#/responses/anotherPet" + } + } + } + }, + "/pets": { + "get": { + "description": + "Returns all pets from the system that the user has access to", + "operationId": "findPets", + "produces": [ + "application/json", + "application/xml", + "text/xml", + "text/html" + ], + "parameters": [ + { + "name": "tags", + "in": "query", + "description": "tags to filter by", + "required": false, + "type": "array", + "items": { + "type": "string" + }, + "collectionFormat": "csv" + }, + { + "name": "limit", + "in": "query", + "description": "maximum number of results to return", + "required": false, + "type": "integer", + "format": "int32" + } + ], + "responses": { + "200": { + "description": "pet response", + "schema": { + "type": "array", + "items": { + "$ref": "pet/pet.json#/definitions/pet" + } + } + }, + "default": { + "description": "unexpected error", + "schema": { + "$ref": "#/definitions/errorModel" + } + } + } + }, + "post": { + "description": + "Creates a new pet in the store. Duplicates are allowed", + "operationId": "addPet", + "produces": ["application/json"], + "parameters": [ + { + "name": "pet", + "in": "body", + "description": "Pet to add to the store", + "required": true, + "schema": { + "$ref": "#/definitions/petInput" + } + } + ], + "responses": { + "200": { "$ref": "#/responses/petResponse" }, + "default": { + "description": "unexpected error", + "schema": { + "$ref": "#/definitions/errorModel" + } + } + } + } + }, + "/pets/{id}": { + "get": { + "description": + "Returns a user based on a single ID, if the user does not have access to the pet", + "operationId": "findPetById", + "produces": [ + "application/json", + "application/xml", + "text/xml", + "text/html" + ], + "parameters": [ + { + "$ref": "#/parameters/idParam" + } + ], + "responses": { + "200": { + "$ref": "#/responses/petResponse" + }, + "default": { + "description": "unexpected error", + "schema": { + "$ref": "#/definitions/errorModel" + } + } + } + }, + "delete": { + "description": "deletes a single pet based on the ID supplied", + "operationId": "deletePet", + "parameters": [ + { + "$ref": "#/parameters/idParam" + } + ], + "responses": { + "204": { + "description": "pet deleted" + }, + "default": { + "description": "unexpected error", + "schema": { + "$ref": "#/definitions/errorModel" + } + } + } + } + } + }, + "definitions": { + "pet": { + "required": ["id", "name"], + "properties": { + "id": { + "type": "integer", + "format": "int64" + }, + "name": { + "type": "string" + }, + "tag": { + "type": "string" + } + } + }, + "petInput": { + "allOf": [ + { + "$ref": "pet/pet.json#/definitions/pet" + }, + { + "required": ["name"], + "properties": { + "id": { + "type": "integer", + "format": "int64" + } + } + } + ] + }, + "errorModel": { + "required": ["code", "message"], + "properties": { + "code": { + "type": "integer", + "format": "int32" + }, + "message": { + "type": "string" + } + } + } + } +} diff --git a/vendor/github.com/go-openapi/spec/fixtures/remote/pet/pet.json b/vendor/github.com/go-openapi/spec/fixtures/remote/pet/pet.json new file mode 100644 index 0000000..5aa885f --- /dev/null +++ b/vendor/github.com/go-openapi/spec/fixtures/remote/pet/pet.json @@ -0,0 +1,19 @@ +{ + "definitions": { + "pet": { + "required": ["id", "name"], + "properties": { + "id": { + "type": "integer", + "format": "int64" + }, + "name": { + "type": "string" + }, + "tag": { + "type": "string" + } + } + } + } +}