Compare commits
68 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
30b0ec5000 | ||
|
4e25b6e83a | ||
|
5448f25fd6 | ||
|
dc386661ca | ||
|
e1486970f7 | ||
|
c750f5de81 | ||
|
266eb3707c | ||
|
d37b141a64 | ||
|
47ef416835 | ||
|
69cc91572b | ||
|
91ebb6e521 | ||
|
c73a4b8c46 | ||
|
a68198f386 | ||
|
21cba66d10 | ||
|
fcfaf82ebd | ||
|
a4fbdd0369 | ||
|
263cb87b9c | ||
|
7393098e01 | ||
|
b4a019192c | ||
|
ff370a965d | ||
|
1594e18f3c | ||
|
c40779224f | ||
|
024c5a4e4e | ||
|
266d42dc25 | ||
|
9da682760b | ||
|
97cfa60fc8 | ||
|
191df6c8ec | ||
|
6d0ee10322 | ||
|
b3496838ad | ||
|
7758db8357 | ||
|
cf5e1b7064 | ||
|
bc937830d2 | ||
|
6202a3f762 | ||
|
c1f10d57e9 | ||
|
199050ce6f | ||
|
1006df3564 | ||
|
99cff16e71 | ||
|
fee245f5d4 | ||
|
99d310ac79 | ||
|
5a65b0fca4 | ||
|
0d32837e5d | ||
|
72880bafe5 | ||
|
4d3b9c31b6 | ||
|
4149bd5c25 | ||
|
0ce8ebe54b | ||
|
b3b9aec439 | ||
|
47277242ae | ||
|
5fa1ea5cba | ||
|
738a68fd4c | ||
|
dc8e191077 | ||
|
acbd1bf87b | ||
|
66e9d20e1f | ||
|
6cbc8f35eb | ||
|
37d97c5693 | ||
|
ab2a5d5181 | ||
|
a8d641cd8d | ||
|
af1ed8a2fb | ||
|
54eb016142 | ||
|
81310a7f0f | ||
|
2c01e8b298 | ||
|
6b43c020b1 | ||
|
e8cdc47633 | ||
|
f8c091326e | ||
|
c73331e20d | ||
|
83f2bd1514 | ||
|
22d4ea9a44 | ||
|
791af18420 | ||
|
6106c552da |
@@ -1,8 +1,9 @@
|
|||||||
language: go
|
language: go
|
||||||
go: 1.7.x
|
go: 1.8.x
|
||||||
install:
|
install:
|
||||||
- wget https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/master/.travis/install-protoc.sh && chmod +x install-protoc.sh && ./install-protoc.sh 3.1.0
|
- go get github.com/Masterminds/glide
|
||||||
- go get github.com/gogo/protobuf/protoc-gen-gogo
|
- wget https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/master/.travis/install-protoc.sh && chmod +x install-protoc.sh && ./install-protoc.sh 3.2.0
|
||||||
|
- go get -u github.com/golang/protobuf/protoc-gen-go
|
||||||
script:
|
script:
|
||||||
- make install
|
- make install
|
||||||
- make test
|
- make test
|
||||||
|
14
Makefile
14
Makefile
@@ -1,15 +1,23 @@
|
|||||||
.PHONY: build
|
.PHONY: build
|
||||||
build:
|
build:
|
||||||
go build -o protoc-gen-gotemplate .
|
go build -v -i -o protoc-gen-gotemplate .
|
||||||
|
|
||||||
.PHONY: install
|
.PHONY: install
|
||||||
install:
|
install:
|
||||||
go install .
|
go install .
|
||||||
|
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
test: build
|
test: install
|
||||||
|
cd examples/enum && make
|
||||||
|
cd examples/import && make
|
||||||
cd examples/dummy && make
|
cd examples/dummy && make
|
||||||
cd examples/js-grpc && make
|
cd examples/flow && make
|
||||||
|
cd examples/concat && make
|
||||||
|
cd examples/flow && make
|
||||||
|
cd examples/sitemap && make
|
||||||
|
cd examples/go-generate && make
|
||||||
|
cd examples/single-package-mode && make
|
||||||
|
# cd examples/go-kit && make
|
||||||
|
|
||||||
.PHONY: docker.build
|
.PHONY: docker.build
|
||||||
docker.build:
|
docker.build:
|
||||||
|
62
README.md
62
README.md
@@ -1,19 +1,32 @@
|
|||||||
# `protoc-gen-gotemplate`
|
# `protoc-gen-gotemplate`
|
||||||
:open_file_folder: protocol generator + golang text/template (protobuf)
|
:open_file_folder: protocol generator + golang text/template (protobuf)
|
||||||
|
|
||||||
Generic protocol buffer generator backed by Golang's [text/template](https://golang.org/pkg/text/template).
|
A generic **code**/script/data generator based on [Protobuf](https://developers.google.com/protocol-buffers/).
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
This is a generator plugin for the Google Protocol Buffers compiler (`protoc`).
|
This project is a generator plugin for the Google Protocol Buffers compiler (`protoc`).
|
||||||
|
|
||||||
The plugin can generate files based on a template directory using the [Golang's `text/template`](https://golang.org/pkg/text/template/) engine.
|
The plugin parses **protobuf** files, generates an **ast**, and walks a local **templates directory** to generate files using the [Golang's `text/template` engine](https://golang.org/pkg/text/template/).
|
||||||
|
|
||||||
|
## Philosophy
|
||||||
|
|
||||||
|
* protobuf-first
|
||||||
|
* no built-in template, only user defined templates
|
||||||
|
* kiss, *keep it stupid simple*
|
||||||
|
|
||||||
|
## Under the hood
|
||||||
|
|
||||||
|
1. the *user* `protobuf` files are parsed by [`protoc`](https://github.com/google/protobuf/releases)
|
||||||
|
2. the `ast` is generated by [`protoc-gen-go` helpers](https://github.com/golang/protobuf/tree/master/protoc-gen-go)
|
||||||
|
3. the `ast` is given to [Golang's `text/template` engine](https://golang.org/pkg/text/template/) for each *user* template files
|
||||||
|
4. the *funcmap* enriching the template engine is based on [Masterminds/sprig](https://github.com/Masterminds/sprig), and contains type-manipulation, iteration and language-specific helpers
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
`protoc-gen-gotemplate` requires a **template_dir** directory *(by default `./templates`)*.
|
`protoc-gen-gotemplate` requires a **template_dir** directory *(by default `./templates`)*.
|
||||||
|
|
||||||
Every files ending with `.tmpl` will be processed and written in the destination folder, following the file hierarchy of the `template_dir`, and removing the `.tmpl` extension.
|
Every file ending with `.tmpl` will be processed and written to the destination folder, following the file hierarchy of the `template_dir`, and remove the `.tmpl` extension.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -26,15 +39,27 @@ input.proto templates/doc.txt.tmpl templates/config.json.tmpl
|
|||||||
doc.txt config.json
|
doc.txt config.json
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
### Options
|
||||||
|
|
||||||
You can specify a custom `template_dir` or enable `debug`:
|
You can specify custom options, as follow:
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$> protoc --gotemplate_out=debug=true,template_dir=/path/to/template/directory:. input.proto
|
$> protoc --gotemplate_out=debug=true,template_dir=/path/to/template/directory:. input.proto
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
| Option | Default Value | Accepted Values | Description
|
||||||
|
|-----------------------|---------------|---------------------------|-----------------------
|
||||||
|
| `template_dir` | `./template` | absolute or relative path | path to look for templates
|
||||||
|
| `destination_dir` | `.` | absolute or relative path | base path to write output
|
||||||
|
| `single-package-mode` | *false* | `true` or `false` | if *true*, `protoc` won't accept multiple packages to be compiled at once (*!= from `all`*), but will support `Message` lookup across the imported protobuf dependencies
|
||||||
|
| `debug` | *false* | `true` or `false` | if *true*, `protoc` will generate a more verbose output
|
||||||
|
| `all` | *false* | `true` or `false` | if *true*, protobuf files without `Service` will also be parsed
|
||||||
|
|
||||||
|
##### Hints
|
||||||
|
|
||||||
|
Shipping the templates with your project is very smart and useful when contributing on git-based projects.
|
||||||
|
|
||||||
|
Another workflow consists in having a dedicated repository for generic templates which is then versioned and vendored with multiple projects (npm package, golang vendor package, ...)
|
||||||
|
|
||||||
See [examples](./examples).
|
See [examples](./examples).
|
||||||
|
|
||||||
@@ -42,13 +67,34 @@ See [examples](./examples).
|
|||||||
|
|
||||||
This project uses [Masterminds/sprig](https://github.com/Masterminds/sprig) library and additional functions to extend the builtin [text/template](https://golang.org/pkg/text/template) helpers.
|
This project uses [Masterminds/sprig](https://github.com/Masterminds/sprig) library and additional functions to extend the builtin [text/template](https://golang.org/pkg/text/template) helpers.
|
||||||
|
|
||||||
Non-exhaustive list of new helpers:
|
Non-exhaustive list of new helpers:s
|
||||||
|
|
||||||
* **all the functions from [sprig](https://github.com/Masterminds/sprig)**
|
* **all the functions from [sprig](https://github.com/Masterminds/sprig)**
|
||||||
* `json`
|
* `json`
|
||||||
* `prettyjson`
|
* `prettyjson`
|
||||||
* `first`
|
* `first`
|
||||||
* `last`
|
* `last`
|
||||||
|
* `splitArray`
|
||||||
|
* `upperFirst`
|
||||||
|
* `lowerFirst`
|
||||||
|
* `camelCase`
|
||||||
|
* `lowerCamelCase`
|
||||||
|
* `kebabCase`
|
||||||
|
* `snakeCase`
|
||||||
|
* `getProtoFile`
|
||||||
|
* `getMessageType`
|
||||||
|
* `getEnumValue`
|
||||||
|
* `isFieldMessage`
|
||||||
|
* `isFieldRepeated`
|
||||||
|
* `goType`
|
||||||
|
* `goTypeWithPackage`
|
||||||
|
* `jsType`
|
||||||
|
* `jsSuffixReserved`
|
||||||
|
* `namespacedFlowType`
|
||||||
|
* `httpVerb`
|
||||||
|
* `httpPath`
|
||||||
|
* `shortType`
|
||||||
|
* `urlHasVarsFromMessage`
|
||||||
|
|
||||||
See the project helpers for the complete list.
|
See the project helpers for the complete list.
|
||||||
|
|
||||||
|
25
encoder.go
25
encoder.go
@@ -17,6 +17,7 @@ type GenericTemplateBasedEncoder struct {
|
|||||||
templateDir string
|
templateDir string
|
||||||
service *descriptor.ServiceDescriptorProto
|
service *descriptor.ServiceDescriptorProto
|
||||||
file *descriptor.FileDescriptorProto
|
file *descriptor.FileDescriptorProto
|
||||||
|
enum []*descriptor.EnumDescriptorProto
|
||||||
debug bool
|
debug bool
|
||||||
destinationDir string
|
destinationDir string
|
||||||
}
|
}
|
||||||
@@ -34,18 +35,18 @@ type Ast struct {
|
|||||||
Filename string `json:"filename"`
|
Filename string `json:"filename"`
|
||||||
TemplateDir string `json:"template-dir"`
|
TemplateDir string `json:"template-dir"`
|
||||||
Service *descriptor.ServiceDescriptorProto `json:"service"`
|
Service *descriptor.ServiceDescriptorProto `json:"service"`
|
||||||
Environment []string `json:"environment"`
|
Enum []*descriptor.EnumDescriptorProto `json:"enum"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGenericTemplateBasedEncoder(templateDir string, service *descriptor.ServiceDescriptorProto, file *descriptor.FileDescriptorProto, debug bool, destinationDir string) (e *GenericTemplateBasedEncoder) {
|
func NewGenericServiceTemplateBasedEncoder(templateDir string, service *descriptor.ServiceDescriptorProto, file *descriptor.FileDescriptorProto, debug bool, destinationDir string) (e *GenericTemplateBasedEncoder) {
|
||||||
e = &GenericTemplateBasedEncoder{
|
e = &GenericTemplateBasedEncoder{
|
||||||
service: service,
|
service: service,
|
||||||
file: file,
|
file: file,
|
||||||
templateDir: templateDir,
|
templateDir: templateDir,
|
||||||
debug: debug,
|
debug: debug,
|
||||||
destinationDir: destinationDir,
|
destinationDir: destinationDir,
|
||||||
|
enum: file.GetEnumType(),
|
||||||
}
|
}
|
||||||
|
|
||||||
if debug {
|
if debug {
|
||||||
log.Printf("new encoder: file=%q service=%q template-dir=%q", file.GetName(), service.GetName(), templateDir)
|
log.Printf("new encoder: file=%q service=%q template-dir=%q", file.GetName(), service.GetName(), templateDir)
|
||||||
}
|
}
|
||||||
@@ -53,6 +54,22 @@ func NewGenericTemplateBasedEncoder(templateDir string, service *descriptor.Serv
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewGenericTemplateBasedEncoder(templateDir string, file *descriptor.FileDescriptorProto, debug bool, destinationDir string) (e *GenericTemplateBasedEncoder) {
|
||||||
|
e = &GenericTemplateBasedEncoder{
|
||||||
|
service: nil,
|
||||||
|
file: file,
|
||||||
|
templateDir: templateDir,
|
||||||
|
enum: file.GetEnumType(),
|
||||||
|
debug: debug,
|
||||||
|
destinationDir: destinationDir,
|
||||||
|
}
|
||||||
|
if debug {
|
||||||
|
log.Printf("new encoder: file=%q template-dir=%q", file.GetName(), templateDir)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (e *GenericTemplateBasedEncoder) templates() ([]string, error) {
|
func (e *GenericTemplateBasedEncoder) templates() ([]string, error) {
|
||||||
filenames := []string{}
|
filenames := []string{}
|
||||||
|
|
||||||
@@ -101,8 +118,8 @@ func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, err
|
|||||||
DestinationDir: e.destinationDir,
|
DestinationDir: e.destinationDir,
|
||||||
RawFilename: templateFilename,
|
RawFilename: templateFilename,
|
||||||
Filename: "",
|
Filename: "",
|
||||||
Environment: os.Environ(),
|
|
||||||
Service: e.service,
|
Service: e.service,
|
||||||
|
Enum: e.enum,
|
||||||
}
|
}
|
||||||
buffer := new(bytes.Buffer)
|
buffer := new(bytes.Buffer)
|
||||||
tmpl, err := template.New("").Funcs(ProtoHelpersFuncMap).Parse(templateFilename)
|
tmpl, err := template.New("").Funcs(ProtoHelpersFuncMap).Parse(templateFilename)
|
||||||
|
13
examples/concat/Makefile
Normal file
13
examples/concat/Makefile
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
.PHONY: build
|
||||||
|
build:
|
||||||
|
mkdir -p output
|
||||||
|
protoc -I. --gotemplate_out=template_dir=templates,debug=true,all=true:output proto/*.proto
|
||||||
|
|
||||||
|
|
||||||
|
.PHONY: re
|
||||||
|
re: clean build
|
||||||
|
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
rm -rf output
|
3
examples/concat/output/concat.txt
Normal file
3
examples/concat/output/concat.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
I'm Eric
|
||||||
|
I'm Francis
|
||||||
|
I'm Arnold
|
1
examples/concat/output/static.txt
Normal file
1
examples/concat/output/static.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
This is static text.This is static text.This is static text.
|
2
examples/concat/proto/Eric.proto
Normal file
2
examples/concat/proto/Eric.proto
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
package Eric;
|
2
examples/concat/proto/Francis.proto
Normal file
2
examples/concat/proto/Francis.proto
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
package Francis;
|
2
examples/concat/proto/arnold.proto
Normal file
2
examples/concat/proto/arnold.proto
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
package Arnold;
|
1
examples/concat/templates/concat.txt.tmpl
Normal file
1
examples/concat/templates/concat.txt.tmpl
Normal file
@@ -0,0 +1 @@
|
|||||||
|
I'm {{.File.Package}}
|
1
examples/concat/templates/static.txt.tmpl
Normal file
1
examples/concat/templates/static.txt.tmpl
Normal file
@@ -0,0 +1 @@
|
|||||||
|
This is static text.
|
@@ -1,9 +1,8 @@
|
|||||||
{
|
{
|
||||||
"build-date": "2016-12-20T11:30:36.474403064+01:00",
|
"build-date": "2017-05-02T11:50:25.063291628+02:00",
|
||||||
"build-hostname": "Zoon-MacBook.local",
|
"build-hostname": "manfred-spacegray.local",
|
||||||
"build-user": "zoon",
|
"build-user": "moul",
|
||||||
"go-pwd": "github.com/protoc-gen-gotemplate/examples/dummy",
|
"pwd": "/Users/moul/Git/moul/protoc-gen-gotemplate/examples/dummy",
|
||||||
"pwd": "/Users/zoon/Projects/gopath/src/github.com/protoc-gen-gotemplate/examples/dummy",
|
|
||||||
"debug": false,
|
"debug": false,
|
||||||
"destination-dir": ".",
|
"destination-dir": ".",
|
||||||
"file": {
|
"file": {
|
||||||
@@ -840,42 +839,5 @@
|
|||||||
"options": {}
|
"options": {}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
}
|
||||||
"environment": [
|
|
||||||
"TERM_PROGRAM=iTerm.app",
|
|
||||||
"ANDROID_HOME=/usr/local/opt/android-sdk",
|
|
||||||
"TERM=xterm-256color",
|
|
||||||
"SHELL=/bin/zsh",
|
|
||||||
"MAKEFLAGS=",
|
|
||||||
"TMPDIR=/var/folders/sq/wlptrlpn4v52xv7xpsgw0fc80000gn/T/",
|
|
||||||
"Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.q9vxeee00S/Render",
|
|
||||||
"TERM_PROGRAM_VERSION=3.0.12",
|
|
||||||
"TERM_SESSION_ID=w0t0p0:6D29EB08-1B96-41B8-8672-0B035605AEE5",
|
|
||||||
"ZSH=/Users/zoon/.oh-my-zsh",
|
|
||||||
"USER=zoon",
|
|
||||||
"SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.5ZcNGIwTRZ/Listeners",
|
|
||||||
"__CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0",
|
|
||||||
"MAKELEVEL=2",
|
|
||||||
"PAGER=less",
|
|
||||||
"MFLAGS=",
|
|
||||||
"LSCOLORS=Gxfxcxdxbxegedabagacad",
|
|
||||||
"PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/zoon/Projects/gopath/bin",
|
|
||||||
"_=/usr/local/bin/protoc",
|
|
||||||
"PWD=/Users/zoon/Projects/gopath/src/github.com/protoc-gen-gotemplate/examples/dummy",
|
|
||||||
"JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home",
|
|
||||||
"EDITOR=micro",
|
|
||||||
"LANG=en_US.UTF-8",
|
|
||||||
"ANDROID_SDK=/usr/local/opt/android-sdk",
|
|
||||||
"ITERM_PROFILE=Default",
|
|
||||||
"XPC_FLAGS=0x0",
|
|
||||||
"XPC_SERVICE_NAME=0",
|
|
||||||
"COLORFGBG=15;0",
|
|
||||||
"SHLVL=3",
|
|
||||||
"HOME=/Users/zoon",
|
|
||||||
"ITERM_SESSION_ID=w0t0p0:6D29EB08-1B96-41B8-8672-0B035605AEE5",
|
|
||||||
"LESS=-R",
|
|
||||||
"LOGNAME=zoon",
|
|
||||||
"LC_CTYPE=UTF-8",
|
|
||||||
"GOPATH=/Users/zoon/Projects/gopath"
|
|
||||||
]
|
|
||||||
}
|
}
|
13
examples/enum/Makefile
Normal file
13
examples/enum/Makefile
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
.PHONY: build
|
||||||
|
build:
|
||||||
|
mkdir -p output
|
||||||
|
protoc -I. --gotemplate_out=template_dir=templates,debug=true,all=true:output proto/*.proto
|
||||||
|
|
||||||
|
|
||||||
|
.PHONY: re
|
||||||
|
re: clean build
|
||||||
|
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
rm -rf output
|
9
examples/enum/output/enum.txt
Normal file
9
examples/enum/output/enum.txt
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
-red
|
||||||
|
-blue
|
||||||
|
-black
|
||||||
|
-yellow
|
||||||
|
-green
|
||||||
|
-dark
|
||||||
|
-white
|
||||||
|
-gray
|
||||||
|
-orange
|
14
examples/enum/proto/sample.proto
Normal file
14
examples/enum/proto/sample.proto
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
package Sample;
|
||||||
|
|
||||||
|
enum Colors {
|
||||||
|
red = 0;
|
||||||
|
blue = 1;
|
||||||
|
black = 2;
|
||||||
|
yellow = 3;
|
||||||
|
green = 4;
|
||||||
|
dark = 5;
|
||||||
|
white = 6;
|
||||||
|
gray = 7;
|
||||||
|
orange = 8;
|
||||||
|
}
|
2
examples/enum/templates/enum.txt.tmpl
Normal file
2
examples/enum/templates/enum.txt.tmpl
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
{{range $m := "colors" | getEnumValue .Enum }}-{{$m.Name}}
|
||||||
|
{{end}}
|
311
examples/flow/output/test/test_grpc_js.js
Normal file
311
examples/flow/output/test/test_grpc_js.js
Normal file
@@ -0,0 +1,311 @@
|
|||||||
|
// @flow
|
||||||
|
// GENERATED CODE -- DO NOT EDIT!
|
||||||
|
|
||||||
|
import base64 from 'base64-js'
|
||||||
|
import test_pb from './test_pb'
|
||||||
|
|
||||||
|
|
||||||
|
export type TestEnum = {|
|
||||||
|
ELEMENT_A?: 0;
|
||||||
|
ELEMENT_B?: 1;
|
||||||
|
|};
|
||||||
|
|
||||||
|
|
||||||
|
export type TestMessage$TestNestedEnum = {|
|
||||||
|
ELEMENT_C?: 0;
|
||||||
|
ELEMENT_D?: 1;
|
||||||
|
|};
|
||||||
|
|
||||||
|
|
||||||
|
export type TestMessage$TestNestedMessage = {
|
||||||
|
getS?: () => string;
|
||||||
|
setS?: (s: string) => void;
|
||||||
|
getT?: () => number;
|
||||||
|
setT?: (t: number) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type TestMessage = {
|
||||||
|
getA?: () => string;
|
||||||
|
setA?: (a: string) => void;
|
||||||
|
getB?: () => number;
|
||||||
|
setB?: (b: number) => void;
|
||||||
|
getC?: () => number;
|
||||||
|
setC?: (c: number) => void;
|
||||||
|
getD?: () => number;
|
||||||
|
setD?: (d: number) => void;
|
||||||
|
getE?: () => number;
|
||||||
|
setE?: (e: number) => void;
|
||||||
|
getNList?: () => Array<string>;
|
||||||
|
setNList?: (n: Array<string>) => void;
|
||||||
|
addN?: (n: string) => void;
|
||||||
|
clearNList?: () => void;
|
||||||
|
getOList?: () => Array<number>;
|
||||||
|
setOList?: (o: Array<number>) => void;
|
||||||
|
addO?: (o: number) => void;
|
||||||
|
clearOList?: () => void;
|
||||||
|
getPList?: () => Array<number>;
|
||||||
|
setPList?: (p: Array<number>) => void;
|
||||||
|
addP?: (p: number) => void;
|
||||||
|
clearPList?: () => void;
|
||||||
|
getQList?: () => Array<number>;
|
||||||
|
setQList?: (q: Array<number>) => void;
|
||||||
|
addQ?: (q: number) => void;
|
||||||
|
clearQList?: () => void;
|
||||||
|
getRList?: () => Array<number>;
|
||||||
|
setRList?: (r: Array<number>) => void;
|
||||||
|
addR?: (r: number) => void;
|
||||||
|
clearRList?: () => void;
|
||||||
|
getU?: () => test$TestEnum;
|
||||||
|
setU?: (u: test$TestEnum) => void;
|
||||||
|
getV?: () => test$TestMessage$TestNestedEnum;
|
||||||
|
setV?: (v: test$TestMessage$TestNestedEnum) => void;
|
||||||
|
getWList?: () => Array<test$TestMessage$TestNestedMessage>;
|
||||||
|
setWList?: (w: Array<test$TestMessage$TestNestedMessage>) => void;
|
||||||
|
addW?: (w: test$TestMessage$TestNestedMessage) => void;
|
||||||
|
clearWList?: () => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export type TestNoStreamRequest = {
|
||||||
|
getMessage?: () => test$TestMessage;
|
||||||
|
setMessage?: (message: test$TestMessage) => void;
|
||||||
|
clearMessage?: () => void;
|
||||||
|
hasMessage?: () => boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export type TestNoStreamReply = {
|
||||||
|
getMessage?: () => test$TestMessage;
|
||||||
|
setMessage?: (message: test$TestMessage) => void;
|
||||||
|
clearMessage?: () => void;
|
||||||
|
hasMessage?: () => boolean;
|
||||||
|
getErrMsg?: () => string;
|
||||||
|
setErrMsg?: (err_msg: string) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export type TestStreamRequestRequest = {
|
||||||
|
getMessage?: () => test$TestMessage;
|
||||||
|
setMessage?: (message: test$TestMessage) => void;
|
||||||
|
clearMessage?: () => void;
|
||||||
|
hasMessage?: () => boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export type TestStreamRequestReply = {
|
||||||
|
getMessage?: () => test$TestMessage;
|
||||||
|
setMessage?: (message: test$TestMessage) => void;
|
||||||
|
clearMessage?: () => void;
|
||||||
|
hasMessage?: () => boolean;
|
||||||
|
getErrMsg?: () => string;
|
||||||
|
setErrMsg?: (err_msg: string) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export type TestStreamReplyRequest = {
|
||||||
|
getMessage?: () => test$TestMessage;
|
||||||
|
setMessage?: (message: test$TestMessage) => void;
|
||||||
|
clearMessage?: () => void;
|
||||||
|
hasMessage?: () => boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export type TestStreamReplyReply = {
|
||||||
|
getMessage?: () => test$TestMessage;
|
||||||
|
setMessage?: (message: test$TestMessage) => void;
|
||||||
|
clearMessage?: () => void;
|
||||||
|
hasMessage?: () => boolean;
|
||||||
|
getErrMsg?: () => string;
|
||||||
|
setErrMsg?: (err_msg: string) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export type TestStreamBothRequest = {
|
||||||
|
getMessage?: () => test$TestMessage;
|
||||||
|
setMessage?: (message: test$TestMessage) => void;
|
||||||
|
clearMessage?: () => void;
|
||||||
|
hasMessage?: () => boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export type TestStreamBothReply = {
|
||||||
|
getMessage?: () => test$TestMessage;
|
||||||
|
setMessage?: (message: test$TestMessage) => void;
|
||||||
|
clearMessage?: () => void;
|
||||||
|
hasMessage?: () => boolean;
|
||||||
|
getErrMsg?: () => string;
|
||||||
|
setErrMsg?: (err_msg: string) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
const serializeToBase64 = (byteArray: Uint8Array): string => base64.fromByteArray(byteArray)
|
||||||
|
const deserializeFromBase64 = (base64Encoded: string): Uint8Array => new Uint8Array(base64.toByteArray(base64Encoded))
|
||||||
|
|
||||||
|
|
||||||
|
function serialize_test_TestNoStreamRequest(arg : TestNoStreamRequest): string {
|
||||||
|
if (!(arg instanceof test_pb.TestNoStreamRequest)) {
|
||||||
|
throw new Error('Expected argument of type TestNoStreamRequest')
|
||||||
|
}
|
||||||
|
return serializeToBase64(arg.serializeBinary())
|
||||||
|
}
|
||||||
|
|
||||||
|
function deserialize_test_TestNoStreamRequest(base64Encoded: string): TestNoStreamRequest {
|
||||||
|
return test_pb.TestNoStreamRequest.deserializeBinary(deserializeFromBase64(base64Encoded))
|
||||||
|
}
|
||||||
|
|
||||||
|
function serialize_test_TestNoStreamReply(arg : TestNoStreamReply): string {
|
||||||
|
if (!(arg instanceof test_pb.TestNoStreamReply)) {
|
||||||
|
throw new Error('Expected argument of type TestNoStreamReply')
|
||||||
|
}
|
||||||
|
return serializeToBase64(arg.serializeBinary())
|
||||||
|
}
|
||||||
|
|
||||||
|
function deserialize_test_TestNoStreamReply(base64Encoded: string): TestNoStreamReply {
|
||||||
|
return test_pb.TestNoStreamReply.deserializeBinary(deserializeFromBase64(base64Encoded))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function serialize_test_TestStreamRequestRequest(arg : TestStreamRequestRequest): string {
|
||||||
|
if (!(arg instanceof test_pb.TestStreamRequestRequest)) {
|
||||||
|
throw new Error('Expected argument of type TestStreamRequestRequest')
|
||||||
|
}
|
||||||
|
return serializeToBase64(arg.serializeBinary())
|
||||||
|
}
|
||||||
|
|
||||||
|
function deserialize_test_TestStreamRequestRequest(base64Encoded: string): TestStreamRequestRequest {
|
||||||
|
return test_pb.TestStreamRequestRequest.deserializeBinary(deserializeFromBase64(base64Encoded))
|
||||||
|
}
|
||||||
|
|
||||||
|
function serialize_test_TestStreamRequestReply(arg : TestStreamRequestReply): string {
|
||||||
|
if (!(arg instanceof test_pb.TestStreamRequestReply)) {
|
||||||
|
throw new Error('Expected argument of type TestStreamRequestReply')
|
||||||
|
}
|
||||||
|
return serializeToBase64(arg.serializeBinary())
|
||||||
|
}
|
||||||
|
|
||||||
|
function deserialize_test_TestStreamRequestReply(base64Encoded: string): TestStreamRequestReply {
|
||||||
|
return test_pb.TestStreamRequestReply.deserializeBinary(deserializeFromBase64(base64Encoded))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function serialize_test_TestStreamReplyRequest(arg : TestStreamReplyRequest): string {
|
||||||
|
if (!(arg instanceof test_pb.TestStreamReplyRequest)) {
|
||||||
|
throw new Error('Expected argument of type TestStreamReplyRequest')
|
||||||
|
}
|
||||||
|
return serializeToBase64(arg.serializeBinary())
|
||||||
|
}
|
||||||
|
|
||||||
|
function deserialize_test_TestStreamReplyRequest(base64Encoded: string): TestStreamReplyRequest {
|
||||||
|
return test_pb.TestStreamReplyRequest.deserializeBinary(deserializeFromBase64(base64Encoded))
|
||||||
|
}
|
||||||
|
|
||||||
|
function serialize_test_TestStreamReplyReply(arg : TestStreamReplyReply): string {
|
||||||
|
if (!(arg instanceof test_pb.TestStreamReplyReply)) {
|
||||||
|
throw new Error('Expected argument of type TestStreamReplyReply')
|
||||||
|
}
|
||||||
|
return serializeToBase64(arg.serializeBinary())
|
||||||
|
}
|
||||||
|
|
||||||
|
function deserialize_test_TestStreamReplyReply(base64Encoded: string): TestStreamReplyReply {
|
||||||
|
return test_pb.TestStreamReplyReply.deserializeBinary(deserializeFromBase64(base64Encoded))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function serialize_test_TestStreamBothRequest(arg : TestStreamBothRequest): string {
|
||||||
|
if (!(arg instanceof test_pb.TestStreamBothRequest)) {
|
||||||
|
throw new Error('Expected argument of type TestStreamBothRequest')
|
||||||
|
}
|
||||||
|
return serializeToBase64(arg.serializeBinary())
|
||||||
|
}
|
||||||
|
|
||||||
|
function deserialize_test_TestStreamBothRequest(base64Encoded: string): TestStreamBothRequest {
|
||||||
|
return test_pb.TestStreamBothRequest.deserializeBinary(deserializeFromBase64(base64Encoded))
|
||||||
|
}
|
||||||
|
|
||||||
|
function serialize_test_TestStreamBothReply(arg : TestStreamBothReply): string {
|
||||||
|
if (!(arg instanceof test_pb.TestStreamBothReply)) {
|
||||||
|
throw new Error('Expected argument of type TestStreamBothReply')
|
||||||
|
}
|
||||||
|
return serializeToBase64(arg.serializeBinary())
|
||||||
|
}
|
||||||
|
|
||||||
|
function deserialize_test_TestStreamBothReply(base64Encoded: string): TestStreamBothReply {
|
||||||
|
return test_pb.TestStreamBothReply.deserializeBinary(deserializeFromBase64(base64Encoded))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export default {
|
||||||
|
|
||||||
|
TestService: {
|
||||||
|
|
||||||
|
testNoStream: {
|
||||||
|
path: '/test.TestService/TestNoStream',
|
||||||
|
requestStream: false,
|
||||||
|
responseStream: false,
|
||||||
|
requestType: test_pb.TestNoStreamRequest,
|
||||||
|
responseType: test_pb.TestNoStreamReply,
|
||||||
|
requestSerialize: serialize_test_TestNoStreamRequest,
|
||||||
|
requestDeserialize: deserialize_test_TestNoStreamRequest,
|
||||||
|
responseSerialize: serialize_test_TestNoStreamReply,
|
||||||
|
responseDeserialize: deserialize_test_TestNoStreamReply,
|
||||||
|
},
|
||||||
|
testStreamRequest: {
|
||||||
|
path: '/test.TestService/TestStreamRequest',
|
||||||
|
requestStream: true,
|
||||||
|
responseStream: false,
|
||||||
|
requestType: test_pb.TestStreamRequestRequest,
|
||||||
|
responseType: test_pb.TestStreamRequestReply,
|
||||||
|
requestSerialize: serialize_test_TestStreamRequestRequest,
|
||||||
|
requestDeserialize: deserialize_test_TestStreamRequestRequest,
|
||||||
|
responseSerialize: serialize_test_TestStreamRequestReply,
|
||||||
|
responseDeserialize: deserialize_test_TestStreamRequestReply,
|
||||||
|
},
|
||||||
|
testStreamReply: {
|
||||||
|
path: '/test.TestService/TestStreamReply',
|
||||||
|
requestStream: false,
|
||||||
|
responseStream: true,
|
||||||
|
requestType: test_pb.TestStreamReplyRequest,
|
||||||
|
responseType: test_pb.TestStreamReplyReply,
|
||||||
|
requestSerialize: serialize_test_TestStreamReplyRequest,
|
||||||
|
requestDeserialize: deserialize_test_TestStreamReplyRequest,
|
||||||
|
responseSerialize: serialize_test_TestStreamReplyReply,
|
||||||
|
responseDeserialize: deserialize_test_TestStreamReplyReply,
|
||||||
|
},
|
||||||
|
testStreamBoth: {
|
||||||
|
path: '/test.TestService/TestStreamBoth',
|
||||||
|
requestStream: true,
|
||||||
|
responseStream: true,
|
||||||
|
requestType: test_pb.TestStreamBothRequest,
|
||||||
|
responseType: test_pb.TestStreamBothReply,
|
||||||
|
requestSerialize: serialize_test_TestStreamBothRequest,
|
||||||
|
requestDeserialize: deserialize_test_TestStreamBothRequest,
|
||||||
|
responseSerialize: serialize_test_TestStreamBothReply,
|
||||||
|
responseDeserialize: deserialize_test_TestStreamBothReply,
|
||||||
|
},
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -11,6 +11,11 @@ service TestService {
|
|||||||
rpc TestStreamBoth(stream TestStreamBothRequest) returns (stream TestStreamBothReply);
|
rpc TestStreamBoth(stream TestStreamBothRequest) returns (stream TestStreamBothReply);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum TestEnum {
|
||||||
|
ELEMENT_A = 0;
|
||||||
|
ELEMENT_B = 1;
|
||||||
|
}
|
||||||
|
|
||||||
message TestMessage {
|
message TestMessage {
|
||||||
string a = 1;
|
string a = 1;
|
||||||
int32 b = 2;
|
int32 b = 2;
|
||||||
@@ -22,6 +27,19 @@ message TestMessage {
|
|||||||
repeated int64 p = 16;
|
repeated int64 p = 16;
|
||||||
repeated float q = 17;
|
repeated float q = 17;
|
||||||
repeated double r = 18;
|
repeated double r = 18;
|
||||||
|
|
||||||
|
message TestNestedMessage {
|
||||||
|
string s = 1;
|
||||||
|
int32 t = 2;
|
||||||
|
}
|
||||||
|
enum TestNestedEnum {
|
||||||
|
ELEMENT_C = 0;
|
||||||
|
ELEMENT_D = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
TestEnum u = 19;
|
||||||
|
TestNestedEnum v = 20;
|
||||||
|
repeated TestNestedMessage w = 21;
|
||||||
}
|
}
|
||||||
|
|
||||||
message TestNoStreamRequest { TestMessage message = 1; }
|
message TestNoStreamRequest { TestMessage message = 1; }
|
@@ -0,0 +1,110 @@
|
|||||||
|
// @flow
|
||||||
|
// GENERATED CODE -- DO NOT EDIT!
|
||||||
|
|
||||||
|
{{- $Package:=.File.Package}}
|
||||||
|
|
||||||
|
import base64 from 'base64-js'
|
||||||
|
import {{$Package}}_pb from './{{$Package}}_pb'
|
||||||
|
|
||||||
|
{{- range .File.Dependency}}
|
||||||
|
import {{. | replace "/" "_" | trimSuffix ".proto" }}_pb from '../{{. | trimSuffix ".proto" }}_pb'
|
||||||
|
{{- end}}
|
||||||
|
|
||||||
|
{{- define "fieldMethods"}}
|
||||||
|
{{- if isFieldRepeated .}}
|
||||||
|
get{{.Name | camelCase}}List?: () => {{. | jsType}};
|
||||||
|
set{{.Name | camelCase}}List?: ({{.Name}}: {{. | jsType}}) => void;
|
||||||
|
add{{.Name | camelCase}}?: ({{.Name}}: {{. | jsType | trimPrefix "Array<" | trimSuffix ">"}}) => void;
|
||||||
|
clear{{.Name | camelCase}}List?: () => void;
|
||||||
|
{{- else}}
|
||||||
|
get{{.Name | camelCase}}?: () => {{. | jsType}};
|
||||||
|
set{{.Name | camelCase}}?: ({{.Name}}: {{. | jsType}}) => void;
|
||||||
|
|
||||||
|
{{- if isFieldMessage .}}
|
||||||
|
clear{{.Name | camelCase}}?: () => void;
|
||||||
|
has{{.Name | camelCase}}?: () => boolean;
|
||||||
|
{{- end}}
|
||||||
|
{{- end}}
|
||||||
|
{{- end}}
|
||||||
|
|
||||||
|
{{range .File.EnumType}}
|
||||||
|
export type {{.Name}} = {|
|
||||||
|
{{- range .Value}}
|
||||||
|
{{.Name}}?: {{.Number}};
|
||||||
|
{{- end}}
|
||||||
|
|};
|
||||||
|
{{- end}}
|
||||||
|
|
||||||
|
{{- range .File.MessageType}}
|
||||||
|
{{- $MessageType := .Name}}
|
||||||
|
|
||||||
|
{{range .EnumType}}
|
||||||
|
export type {{$MessageType}}${{.Name}} = {|
|
||||||
|
{{- range .Value}}
|
||||||
|
{{.Name}}?: {{.Number}};
|
||||||
|
{{- end}}
|
||||||
|
|};
|
||||||
|
{{- end}}
|
||||||
|
|
||||||
|
{{range .NestedType}}
|
||||||
|
export type {{$MessageType}}${{.Name}} = {
|
||||||
|
{{- range .Field}}
|
||||||
|
{{- template "fieldMethods" .}}
|
||||||
|
{{- end}}
|
||||||
|
};
|
||||||
|
{{- end}}
|
||||||
|
|
||||||
|
export type {{.Name}} = {
|
||||||
|
{{- range .Field}}
|
||||||
|
{{- template "fieldMethods" .}}
|
||||||
|
{{- end}}
|
||||||
|
};
|
||||||
|
|
||||||
|
{{- end}}
|
||||||
|
|
||||||
|
const serializeToBase64 = (byteArray: Uint8Array): string => base64.fromByteArray(byteArray)
|
||||||
|
const deserializeFromBase64 = (base64Encoded: string): Uint8Array => new Uint8Array(base64.toByteArray(base64Encoded))
|
||||||
|
|
||||||
|
{{range .File.Service}}{{range .Method}}
|
||||||
|
function serialize_{{$Package}}_{{.InputType | shortType}}(arg : {{.InputType | shortType}}): string {
|
||||||
|
if (!(arg instanceof {{$Package}}_pb.{{.InputType | shortType}})) {
|
||||||
|
throw new Error('Expected argument of type {{.InputType | shortType}}')
|
||||||
|
}
|
||||||
|
return serializeToBase64(arg.serializeBinary())
|
||||||
|
}
|
||||||
|
|
||||||
|
function deserialize_{{$Package}}_{{.InputType | shortType}}(base64Encoded: string): {{.InputType | shortType}} {
|
||||||
|
return {{$Package}}_pb.{{.InputType | shortType}}.deserializeBinary(deserializeFromBase64(base64Encoded))
|
||||||
|
}
|
||||||
|
|
||||||
|
function serialize_{{$Package}}_{{.OutputType | shortType}}(arg : {{.OutputType | shortType}}): string {
|
||||||
|
if (!(arg instanceof {{$Package}}_pb.{{.OutputType | shortType}})) {
|
||||||
|
throw new Error('Expected argument of type {{.OutputType | shortType}}')
|
||||||
|
}
|
||||||
|
return serializeToBase64(arg.serializeBinary())
|
||||||
|
}
|
||||||
|
|
||||||
|
function deserialize_{{$Package}}_{{.OutputType | shortType}}(base64Encoded: string): {{.OutputType | shortType}} {
|
||||||
|
return {{$Package}}_pb.{{.OutputType | shortType}}.deserializeBinary(deserializeFromBase64(base64Encoded))
|
||||||
|
}
|
||||||
|
|
||||||
|
{{end}}{{end}}
|
||||||
|
export default {
|
||||||
|
{{range .File.Service}}
|
||||||
|
{{.Name}}: {
|
||||||
|
{{$serviceName:=.Name}}
|
||||||
|
{{range .Method}}{{.Name | lowerCamelCase}}: {
|
||||||
|
path: '/{{$Package}}.{{$serviceName}}/{{.Name}}',
|
||||||
|
requestStream: {{.ClientStreaming | default "false"}},
|
||||||
|
responseStream: {{.ServerStreaming | default "false"}},
|
||||||
|
requestType: {{$Package}}_pb.{{.InputType | shortType}},
|
||||||
|
responseType: {{$Package}}_pb.{{.OutputType | shortType}},
|
||||||
|
requestSerialize: serialize_{{$Package}}_{{.InputType | shortType}},
|
||||||
|
requestDeserialize: deserialize_{{$Package}}_{{.InputType | shortType}},
|
||||||
|
responseSerialize: serialize_{{$Package}}_{{.OutputType | shortType}},
|
||||||
|
responseDeserialize: deserialize_{{$Package}}_{{.OutputType | shortType}},
|
||||||
|
},
|
||||||
|
{{end}}
|
||||||
|
}
|
||||||
|
{{end}}
|
||||||
|
}
|
2
examples/go-generate/Makefile
Normal file
2
examples/go-generate/Makefile
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
all:
|
||||||
|
go generate
|
4
examples/go-generate/example.go
Normal file
4
examples/go-generate/example.go
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package example
|
||||||
|
|
||||||
|
//go:generate protoc --go_out=./gen/ example.proto
|
||||||
|
//go:generate protoc --gotemplate_out=./gen/ example.proto
|
16
examples/go-generate/example.proto
Normal file
16
examples/go-generate/example.proto
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package example;
|
||||||
|
|
||||||
|
service Sum {
|
||||||
|
rpc Sum(SumRequest) returns (SumReply) {};
|
||||||
|
}
|
||||||
|
|
||||||
|
message SumRequest {
|
||||||
|
int32 a = 1;
|
||||||
|
int32 b = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message SumReply {
|
||||||
|
int32 c = 1;
|
||||||
|
}
|
11
examples/go-generate/gen/doc.gen.go
Normal file
11
examples/go-generate/gen/doc.gen.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
// Code generated by protoc-gen-gotemplate
|
||||||
|
package example
|
||||||
|
|
||||||
|
// Methods
|
||||||
|
// -------
|
||||||
|
// * Sum
|
||||||
|
//
|
||||||
|
// Message types
|
||||||
|
// -------------
|
||||||
|
// * SumRequest
|
||||||
|
// * SumReply
|
89
examples/go-generate/gen/example.pb.go
Normal file
89
examples/go-generate/gen/example.pb.go
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
// Code generated by protoc-gen-go.
|
||||||
|
// source: example.proto
|
||||||
|
// DO NOT EDIT!
|
||||||
|
|
||||||
|
/*
|
||||||
|
Package example is a generated protocol buffer package.
|
||||||
|
|
||||||
|
It is generated from these files:
|
||||||
|
example.proto
|
||||||
|
|
||||||
|
It has these top-level messages:
|
||||||
|
SumRequest
|
||||||
|
SumReply
|
||||||
|
*/
|
||||||
|
package example
|
||||||
|
|
||||||
|
import proto "github.com/golang/protobuf/proto"
|
||||||
|
import fmt "fmt"
|
||||||
|
import math "math"
|
||||||
|
|
||||||
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
|
var _ = proto.Marshal
|
||||||
|
var _ = fmt.Errorf
|
||||||
|
var _ = math.Inf
|
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the proto package it is being compiled against.
|
||||||
|
// A compilation error at this line likely means your copy of the
|
||||||
|
// proto package needs to be updated.
|
||||||
|
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||||
|
|
||||||
|
type SumRequest struct {
|
||||||
|
A int32 `protobuf:"varint,1,opt,name=a" json:"a,omitempty"`
|
||||||
|
B int32 `protobuf:"varint,2,opt,name=b" json:"b,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *SumRequest) Reset() { *m = SumRequest{} }
|
||||||
|
func (m *SumRequest) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*SumRequest) ProtoMessage() {}
|
||||||
|
func (*SumRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
|
||||||
|
|
||||||
|
func (m *SumRequest) GetA() int32 {
|
||||||
|
if m != nil {
|
||||||
|
return m.A
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *SumRequest) GetB() int32 {
|
||||||
|
if m != nil {
|
||||||
|
return m.B
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
type SumReply struct {
|
||||||
|
C int32 `protobuf:"varint,1,opt,name=c" json:"c,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *SumReply) Reset() { *m = SumReply{} }
|
||||||
|
func (m *SumReply) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*SumReply) ProtoMessage() {}
|
||||||
|
func (*SumReply) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
|
||||||
|
|
||||||
|
func (m *SumReply) GetC() int32 {
|
||||||
|
if m != nil {
|
||||||
|
return m.C
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
proto.RegisterType((*SumRequest)(nil), "example.SumRequest")
|
||||||
|
proto.RegisterType((*SumReply)(nil), "example.SumReply")
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() { proto.RegisterFile("example.proto", fileDescriptor0) }
|
||||||
|
|
||||||
|
var fileDescriptor0 = []byte{
|
||||||
|
// 124 bytes of a gzipped FileDescriptorProto
|
||||||
|
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x4d, 0xad, 0x48, 0xcc,
|
||||||
|
0x2d, 0xc8, 0x49, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x87, 0x72, 0x95, 0x34, 0xb8,
|
||||||
|
0xb8, 0x82, 0x4b, 0x73, 0x83, 0x52, 0x0b, 0x4b, 0x53, 0x8b, 0x4b, 0x84, 0x78, 0xb8, 0x18, 0x13,
|
||||||
|
0x25, 0x18, 0x15, 0x18, 0x35, 0x58, 0x83, 0x18, 0x13, 0x41, 0xbc, 0x24, 0x09, 0x26, 0x08, 0x2f,
|
||||||
|
0x49, 0x49, 0x82, 0x8b, 0x03, 0xac, 0xb2, 0x20, 0xa7, 0x12, 0x24, 0x93, 0x0c, 0x53, 0x97, 0x6c,
|
||||||
|
0x64, 0xc6, 0xc5, 0x1c, 0x5c, 0x9a, 0x2b, 0xa4, 0x0f, 0xa1, 0x84, 0xf5, 0x60, 0x56, 0x21, 0x0c,
|
||||||
|
0x96, 0x12, 0x44, 0x15, 0x2c, 0xc8, 0xa9, 0x54, 0x62, 0x48, 0x62, 0x03, 0xbb, 0xc5, 0x18, 0x10,
|
||||||
|
0x00, 0x00, 0xff, 0xff, 0x2b, 0xf1, 0xe9, 0x56, 0x9c, 0x00, 0x00, 0x00,
|
||||||
|
}
|
14
examples/go-generate/templates/doc.gen.go.tmpl
Normal file
14
examples/go-generate/templates/doc.gen.go.tmpl
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
// Code generated by protoc-gen-gotemplate
|
||||||
|
package {{.File.Package}}
|
||||||
|
|
||||||
|
// Methods
|
||||||
|
// -------
|
||||||
|
{{- range .Service.Method}}
|
||||||
|
// * {{.Name}}
|
||||||
|
{{- end}}
|
||||||
|
//
|
||||||
|
// Message types
|
||||||
|
// -------------
|
||||||
|
{{- range .File.MessageType}}
|
||||||
|
// * {{.Name}}
|
||||||
|
{{- end}}
|
1
examples/go-kit/.gitignore
vendored
1
examples/go-kit/.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
|
/vendor/
|
||||||
/server
|
/server
|
||||||
|
@@ -9,10 +9,11 @@ service_name = $(word 2,$(subst /, ,$1))
|
|||||||
build: server
|
build: server
|
||||||
|
|
||||||
server: $(TARGETS_GO) $(TARGETS_TMPL)
|
server: $(TARGETS_GO) $(TARGETS_TMPL)
|
||||||
|
glide install
|
||||||
go build -o server .
|
go build -o server .
|
||||||
|
|
||||||
$(TARGETS_GO): %_go:
|
$(TARGETS_GO): %_go:
|
||||||
protoc --gogo_out=plugins=grpc:. "$*"
|
protoc --go_out=plugins=grpc:. "$*"
|
||||||
@mkdir -p services/$(call service_name,$*)/gen/pb
|
@mkdir -p services/$(call service_name,$*)/gen/pb
|
||||||
@mv ./services/$(call service_name,$*)/$(call service_name,$*).pb.go ./services/$(call service_name,$*)/gen/pb/pb.go
|
@mv ./services/$(call service_name,$*)/$(call service_name,$*).pb.go ./services/$(call service_name,$*)/gen/pb/pb.go
|
||||||
|
|
||||||
|
48
examples/go-kit/glide.lock
generated
Normal file
48
examples/go-kit/glide.lock
generated
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
hash: e225ab17b49f8d6601b2bd813d43ad34ee04380d29c278c11919efd454c7b2d7
|
||||||
|
updated: 2017-03-16T17:03:34.437968115+01:00
|
||||||
|
imports:
|
||||||
|
- name: github.com/dgrijalva/jwt-go
|
||||||
|
version: 2268707a8f0843315e2004ee4f1d021dc08baedf
|
||||||
|
- name: github.com/go-kit/kit
|
||||||
|
version: fadad6fffe0466b19df9efd9acde5c9a52df5fa4
|
||||||
|
subpackages:
|
||||||
|
- auth/jwt
|
||||||
|
- endpoint
|
||||||
|
- log
|
||||||
|
- transport/grpc
|
||||||
|
- transport/http
|
||||||
|
- name: github.com/go-logfmt/logfmt
|
||||||
|
version: 390ab7935ee28ec6b286364bba9b4dd6410cb3d5
|
||||||
|
- name: github.com/go-stack/stack
|
||||||
|
version: 100eb0c0a9c5b306ca2fb4f165df21d80ada4b82
|
||||||
|
- name: github.com/golang/protobuf
|
||||||
|
version: c9c7427a2a70d2eb3bafa0ab2dc163e45f143317
|
||||||
|
subpackages:
|
||||||
|
- proto
|
||||||
|
- name: github.com/gorilla/handlers
|
||||||
|
version: 3a5767ca75ece5f7f1440b1d16975247f8d8b221
|
||||||
|
- name: github.com/kr/logfmt
|
||||||
|
version: b84e30acd515aadc4b783ad4ff83aff3299bdfe0
|
||||||
|
- name: golang.org/x/net
|
||||||
|
version: a6577fac2d73be281a500b310739095313165611
|
||||||
|
subpackages:
|
||||||
|
- context
|
||||||
|
- context/ctxhttp
|
||||||
|
- http2
|
||||||
|
- http2/hpack
|
||||||
|
- idna
|
||||||
|
- internal/timeseries
|
||||||
|
- lex/httplex
|
||||||
|
- trace
|
||||||
|
- name: google.golang.org/grpc
|
||||||
|
version: 777daa17ff9b5daef1cfdf915088a2ada3332bf0
|
||||||
|
subpackages:
|
||||||
|
- codes
|
||||||
|
- credentials
|
||||||
|
- grpclog
|
||||||
|
- internal
|
||||||
|
- metadata
|
||||||
|
- naming
|
||||||
|
- peer
|
||||||
|
- transport
|
||||||
|
testImports: []
|
20
examples/go-kit/glide.yaml
Normal file
20
examples/go-kit/glide.yaml
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package: github.com/moul/protoc-gen-gotemplate/examples/go-kit
|
||||||
|
import:
|
||||||
|
- package: github.com/go-kit/kit
|
||||||
|
subpackages:
|
||||||
|
- auth/jwt
|
||||||
|
- endpoint
|
||||||
|
- log
|
||||||
|
- transport/grpc
|
||||||
|
- transport/http
|
||||||
|
- package: github.com/golang/protobuf
|
||||||
|
subpackages:
|
||||||
|
- proto
|
||||||
|
version: c9c7427a2a70d2eb3bafa0ab2dc163e45f143317
|
||||||
|
- package: github.com/gorilla/handlers
|
||||||
|
- package: golang.org/x/net
|
||||||
|
subpackages:
|
||||||
|
- context
|
||||||
|
- http2
|
||||||
|
version: a6577fac2d73be281a500b310739095313165611
|
||||||
|
- package: google.golang.org/grpc
|
@@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -34,37 +33,36 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
ctx := context.Background()
|
|
||||||
errc := make(chan error)
|
errc := make(chan error)
|
||||||
s := grpc.NewServer()
|
s := grpc.NewServer()
|
||||||
var logger log.Logger
|
var logger log.Logger
|
||||||
{
|
{
|
||||||
logger = log.NewLogfmtLogger(os.Stdout)
|
logger = log.NewLogfmtLogger(os.Stdout)
|
||||||
logger = log.NewContext(logger).With("ts", log.DefaultTimestampUTC)
|
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
|
||||||
logger = log.NewContext(logger).With("caller", log.DefaultCaller)
|
logger = log.With(logger, "caller", log.DefaultCaller)
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialize services
|
// initialize services
|
||||||
{
|
{
|
||||||
svc := session_svc.New()
|
svc := session_svc.New()
|
||||||
endpoints := session_endpoints.MakeEndpoints(svc)
|
endpoints := session_endpoints.MakeEndpoints(svc)
|
||||||
srv := session_grpctransport.MakeGRPCServer(ctx, endpoints)
|
srv := session_grpctransport.MakeGRPCServer(endpoints)
|
||||||
session_pb.RegisterSessionServiceServer(s, srv)
|
session_pb.RegisterSessionServiceServer(s, srv)
|
||||||
session_httptransport.RegisterHandlers(ctx, svc, mux, endpoints)
|
session_httptransport.RegisterHandlers(svc, mux, endpoints)
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
svc := sprint_svc.New()
|
svc := sprint_svc.New()
|
||||||
endpoints := sprint_endpoints.MakeEndpoints(svc)
|
endpoints := sprint_endpoints.MakeEndpoints(svc)
|
||||||
srv := sprint_grpctransport.MakeGRPCServer(ctx, endpoints)
|
srv := sprint_grpctransport.MakeGRPCServer(endpoints)
|
||||||
sprint_pb.RegisterSprintServiceServer(s, srv)
|
sprint_pb.RegisterSprintServiceServer(s, srv)
|
||||||
sprint_httptransport.RegisterHandlers(ctx, svc, mux, endpoints)
|
sprint_httptransport.RegisterHandlers(svc, mux, endpoints)
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
svc := user_svc.New()
|
svc := user_svc.New()
|
||||||
endpoints := user_endpoints.MakeEndpoints(svc)
|
endpoints := user_endpoints.MakeEndpoints(svc)
|
||||||
srv := user_grpctransport.MakeGRPCServer(ctx, endpoints)
|
srv := user_grpctransport.MakeGRPCServer(endpoints)
|
||||||
user_pb.RegisterUserServiceServer(s, srv)
|
user_pb.RegisterUserServiceServer(s, srv)
|
||||||
user_httptransport.RegisterHandlers(ctx, svc, mux, endpoints)
|
user_httptransport.RegisterHandlers(svc, mux, endpoints)
|
||||||
}
|
}
|
||||||
|
|
||||||
// start servers
|
// start servers
|
||||||
@@ -75,13 +73,13 @@ func main() {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
logger := log.NewContext(logger).With("transport", "HTTP")
|
logger := log.With(logger, "transport", "HTTP")
|
||||||
logger.Log("addr", ":8000")
|
logger.Log("addr", ":8000")
|
||||||
errc <- http.ListenAndServe(":8000", handlers.LoggingHandler(os.Stderr, mux))
|
errc <- http.ListenAndServe(":8000", handlers.LoggingHandler(os.Stderr, mux))
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
logger := log.NewContext(logger).With("transport", "gRPC")
|
logger := log.With(logger, "transport", "gRPC")
|
||||||
ln, err := net.Listen("tcp", ":9000")
|
ln, err := net.Listen("tcp", ":9000")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errc <- err
|
errc <- err
|
||||||
|
@@ -1,11 +1,12 @@
|
|||||||
package session_clientgrpc
|
package session_clientgrpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
context "context"
|
||||||
|
|
||||||
jwt "github.com/go-kit/kit/auth/jwt"
|
jwt "github.com/go-kit/kit/auth/jwt"
|
||||||
"github.com/go-kit/kit/endpoint"
|
"github.com/go-kit/kit/endpoint"
|
||||||
"github.com/go-kit/kit/log"
|
"github.com/go-kit/kit/log"
|
||||||
grpctransport "github.com/go-kit/kit/transport/grpc"
|
grpctransport "github.com/go-kit/kit/transport/grpc"
|
||||||
context "golang.org/x/net/context"
|
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
|
||||||
endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/endpoints"
|
endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/endpoints"
|
||||||
|
@@ -1,14 +1,17 @@
|
|||||||
package session_endpoints
|
package session_endpoints
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
context "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/go-kit/kit/endpoint"
|
"github.com/go-kit/kit/endpoint"
|
||||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/pb"
|
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/pb"
|
||||||
context "golang.org/x/net/context"
|
oldcontext "golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ = endpoint.Chain
|
||||||
var _ = fmt.Errorf
|
var _ = fmt.Errorf
|
||||||
|
var _ = context.Background
|
||||||
|
|
||||||
type StreamEndpoint func(server interface{}, req interface{}) (err error)
|
type StreamEndpoint func(server interface{}, req interface{}) (err error)
|
||||||
|
|
||||||
@@ -16,7 +19,7 @@ type Endpoints struct {
|
|||||||
LoginEndpoint endpoint.Endpoint
|
LoginEndpoint endpoint.Endpoint
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Endpoints) Login(ctx context.Context, in *pb.LoginRequest) (*pb.LoginResponse, error) {
|
func (e *Endpoints) Login(ctx oldcontext.Context, in *pb.LoginRequest) (*pb.LoginResponse, error) {
|
||||||
out, err := e.LoginEndpoint(ctx, in)
|
out, err := e.LoginEndpoint(ctx, in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &pb.LoginResponse{ErrMsg: err.Error()}, err
|
return &pb.LoginResponse{ErrMsg: err.Error()}, err
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
// Code generated by protoc-gen-gogo.
|
// Code generated by protoc-gen-go.
|
||||||
// source: services/session/session.proto
|
// source: services/session/session.proto
|
||||||
// DO NOT EDIT!
|
// DO NOT EDIT!
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@ It has these top-level messages:
|
|||||||
*/
|
*/
|
||||||
package session
|
package session
|
||||||
|
|
||||||
import proto "github.com/gogo/protobuf/proto"
|
import proto "github.com/golang/protobuf/proto"
|
||||||
import fmt "fmt"
|
import fmt "fmt"
|
||||||
import math "math"
|
import math "math"
|
||||||
|
|
||||||
@@ -32,17 +32,17 @@ var _ = math.Inf
|
|||||||
// is compatible with the proto package it is being compiled against.
|
// is compatible with the proto package it is being compiled against.
|
||||||
// A compilation error at this line likely means your copy of the
|
// A compilation error at this line likely means your copy of the
|
||||||
// proto package needs to be updated.
|
// proto package needs to be updated.
|
||||||
const _ = proto.GoGoProtoPackageIsVersion2 // please upgrade the proto package
|
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||||
|
|
||||||
type LoginRequest struct {
|
type LoginRequest struct {
|
||||||
Username string `protobuf:"bytes,1,opt,name=username,proto3" json:"username,omitempty"`
|
Username string `protobuf:"bytes,1,opt,name=username" json:"username,omitempty"`
|
||||||
Password string `protobuf:"bytes,2,opt,name=password,proto3" json:"password,omitempty"`
|
Password string `protobuf:"bytes,2,opt,name=password" json:"password,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *LoginRequest) Reset() { *m = LoginRequest{} }
|
func (m *LoginRequest) Reset() { *m = LoginRequest{} }
|
||||||
func (m *LoginRequest) String() string { return proto.CompactTextString(m) }
|
func (m *LoginRequest) String() string { return proto.CompactTextString(m) }
|
||||||
func (*LoginRequest) ProtoMessage() {}
|
func (*LoginRequest) ProtoMessage() {}
|
||||||
func (*LoginRequest) Descriptor() ([]byte, []int) { return fileDescriptorSession, []int{0} }
|
func (*LoginRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
|
||||||
|
|
||||||
func (m *LoginRequest) GetUsername() string {
|
func (m *LoginRequest) GetUsername() string {
|
||||||
if m != nil {
|
if m != nil {
|
||||||
@@ -59,14 +59,14 @@ func (m *LoginRequest) GetPassword() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type LoginResponse struct {
|
type LoginResponse struct {
|
||||||
Token string `protobuf:"bytes,1,opt,name=token,proto3" json:"token,omitempty"`
|
Token string `protobuf:"bytes,1,opt,name=token" json:"token,omitempty"`
|
||||||
ErrMsg string `protobuf:"bytes,2,opt,name=err_msg,json=errMsg,proto3" json:"err_msg,omitempty"`
|
ErrMsg string `protobuf:"bytes,2,opt,name=err_msg,json=errMsg" json:"err_msg,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *LoginResponse) Reset() { *m = LoginResponse{} }
|
func (m *LoginResponse) Reset() { *m = LoginResponse{} }
|
||||||
func (m *LoginResponse) String() string { return proto.CompactTextString(m) }
|
func (m *LoginResponse) String() string { return proto.CompactTextString(m) }
|
||||||
func (*LoginResponse) ProtoMessage() {}
|
func (*LoginResponse) ProtoMessage() {}
|
||||||
func (*LoginResponse) Descriptor() ([]byte, []int) { return fileDescriptorSession, []int{1} }
|
func (*LoginResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
|
||||||
|
|
||||||
func (m *LoginResponse) GetToken() string {
|
func (m *LoginResponse) GetToken() string {
|
||||||
if m != nil {
|
if m != nil {
|
||||||
@@ -159,9 +159,9 @@ var _SessionService_serviceDesc = grpc.ServiceDesc{
|
|||||||
Metadata: "services/session/session.proto",
|
Metadata: "services/session/session.proto",
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() { proto.RegisterFile("services/session/session.proto", fileDescriptorSession) }
|
func init() { proto.RegisterFile("services/session/session.proto", fileDescriptor0) }
|
||||||
|
|
||||||
var fileDescriptorSession = []byte{
|
var fileDescriptor0 = []byte{
|
||||||
// 188 bytes of a gzipped FileDescriptorProto
|
// 188 bytes of a gzipped FileDescriptorProto
|
||||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x92, 0x2b, 0x4e, 0x2d, 0x2a,
|
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x92, 0x2b, 0x4e, 0x2d, 0x2a,
|
||||||
0xcb, 0x4c, 0x4e, 0x2d, 0xd6, 0x2f, 0x4e, 0x2d, 0x2e, 0xce, 0xcc, 0xcf, 0x83, 0xd1, 0x7a, 0x05,
|
0xcb, 0x4c, 0x4e, 0x2d, 0xd6, 0x2f, 0x4e, 0x2d, 0x2e, 0xce, 0xcc, 0xcf, 0x83, 0xd1, 0x7a, 0x05,
|
||||||
|
@@ -1,10 +1,11 @@
|
|||||||
package session_grpctransport
|
package session_grpctransport
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
context "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
grpctransport "github.com/go-kit/kit/transport/grpc"
|
grpctransport "github.com/go-kit/kit/transport/grpc"
|
||||||
context "golang.org/x/net/context"
|
oldcontext "golang.org/x/net/context"
|
||||||
|
|
||||||
endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/endpoints"
|
endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/endpoints"
|
||||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/pb"
|
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/pb"
|
||||||
@@ -13,12 +14,12 @@ import (
|
|||||||
// avoid import errors
|
// avoid import errors
|
||||||
var _ = fmt.Errorf
|
var _ = fmt.Errorf
|
||||||
|
|
||||||
func MakeGRPCServer(ctx context.Context, endpoints endpoints.Endpoints) pb.SessionServiceServer {
|
func MakeGRPCServer(endpoints endpoints.Endpoints) pb.SessionServiceServer {
|
||||||
options := []grpctransport.ServerOption{}
|
var options []grpctransport.ServerOption
|
||||||
|
_ = options
|
||||||
return &grpcServer{
|
return &grpcServer{
|
||||||
|
|
||||||
login: grpctransport.NewServer(
|
login: grpctransport.NewServer(
|
||||||
ctx,
|
|
||||||
endpoints.LoginEndpoint,
|
endpoints.LoginEndpoint,
|
||||||
decodeRequest,
|
decodeRequest,
|
||||||
encodeLoginResponse,
|
encodeLoginResponse,
|
||||||
@@ -31,7 +32,7 @@ type grpcServer struct {
|
|||||||
login grpctransport.Handler
|
login grpctransport.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *grpcServer) Login(ctx context.Context, req *pb.LoginRequest) (*pb.LoginResponse, error) {
|
func (s *grpcServer) Login(ctx oldcontext.Context, req *pb.LoginRequest) (*pb.LoginResponse, error) {
|
||||||
_, rep, err := s.login.ServeGRPC(ctx, req)
|
_, rep, err := s.login.ServeGRPC(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@@ -1,8 +1,8 @@
|
|||||||
package session_httptransport
|
package session_httptransport
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
context "golang.org/x/net/context"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -12,9 +12,12 @@ import (
|
|||||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/pb"
|
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/pb"
|
||||||
)
|
)
|
||||||
|
|
||||||
func MakeLoginHandler(ctx context.Context, svc pb.SessionServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
var _ = log.Printf
|
||||||
|
var _ = gokit_endpoint.Chain
|
||||||
|
var _ = httptransport.NewClient
|
||||||
|
|
||||||
|
func MakeLoginHandler(svc pb.SessionServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
||||||
return httptransport.NewServer(
|
return httptransport.NewServer(
|
||||||
ctx,
|
|
||||||
endpoint,
|
endpoint,
|
||||||
decodeLoginRequest,
|
decodeLoginRequest,
|
||||||
encodeResponse,
|
encodeResponse,
|
||||||
@@ -34,10 +37,10 @@ func encodeResponse(ctx context.Context, w http.ResponseWriter, response interfa
|
|||||||
return json.NewEncoder(w).Encode(response)
|
return json.NewEncoder(w).Encode(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterHandlers(ctx context.Context, svc pb.SessionServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error {
|
func RegisterHandlers(svc pb.SessionServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error {
|
||||||
|
|
||||||
log.Println("new HTTP endpoint: \"/Login\" (service=Session)")
|
log.Println("new HTTP endpoint: \"/Login\" (service=Session)")
|
||||||
mux.Handle("/Login", MakeLoginHandler(ctx, svc, endpoints.LoginEndpoint))
|
mux.Handle("/Login", MakeLoginHandler(svc, endpoints.LoginEndpoint))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -1,11 +1,12 @@
|
|||||||
package sprint_clientgrpc
|
package sprint_clientgrpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
context "context"
|
||||||
|
|
||||||
jwt "github.com/go-kit/kit/auth/jwt"
|
jwt "github.com/go-kit/kit/auth/jwt"
|
||||||
"github.com/go-kit/kit/endpoint"
|
"github.com/go-kit/kit/endpoint"
|
||||||
"github.com/go-kit/kit/log"
|
"github.com/go-kit/kit/log"
|
||||||
grpctransport "github.com/go-kit/kit/transport/grpc"
|
grpctransport "github.com/go-kit/kit/transport/grpc"
|
||||||
context "golang.org/x/net/context"
|
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
|
||||||
endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/endpoints"
|
endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/endpoints"
|
||||||
|
@@ -1,14 +1,17 @@
|
|||||||
package sprint_endpoints
|
package sprint_endpoints
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
context "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/go-kit/kit/endpoint"
|
"github.com/go-kit/kit/endpoint"
|
||||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/pb"
|
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/pb"
|
||||||
context "golang.org/x/net/context"
|
oldcontext "golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ = endpoint.Chain
|
||||||
var _ = fmt.Errorf
|
var _ = fmt.Errorf
|
||||||
|
var _ = context.Background
|
||||||
|
|
||||||
type StreamEndpoint func(server interface{}, req interface{}) (err error)
|
type StreamEndpoint func(server interface{}, req interface{}) (err error)
|
||||||
|
|
||||||
@@ -20,7 +23,7 @@ type Endpoints struct {
|
|||||||
GetSprintEndpoint endpoint.Endpoint
|
GetSprintEndpoint endpoint.Endpoint
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Endpoints) AddSprint(ctx context.Context, in *pb.AddSprintRequest) (*pb.AddSprintResponse, error) {
|
func (e *Endpoints) AddSprint(ctx oldcontext.Context, in *pb.AddSprintRequest) (*pb.AddSprintResponse, error) {
|
||||||
out, err := e.AddSprintEndpoint(ctx, in)
|
out, err := e.AddSprintEndpoint(ctx, in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &pb.AddSprintResponse{ErrMsg: err.Error()}, err
|
return &pb.AddSprintResponse{ErrMsg: err.Error()}, err
|
||||||
@@ -28,7 +31,7 @@ func (e *Endpoints) AddSprint(ctx context.Context, in *pb.AddSprintRequest) (*pb
|
|||||||
return out.(*pb.AddSprintResponse), err
|
return out.(*pb.AddSprintResponse), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Endpoints) CloseSprint(ctx context.Context, in *pb.CloseSprintRequest) (*pb.CloseSprintResponse, error) {
|
func (e *Endpoints) CloseSprint(ctx oldcontext.Context, in *pb.CloseSprintRequest) (*pb.CloseSprintResponse, error) {
|
||||||
out, err := e.CloseSprintEndpoint(ctx, in)
|
out, err := e.CloseSprintEndpoint(ctx, in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &pb.CloseSprintResponse{ErrMsg: err.Error()}, err
|
return &pb.CloseSprintResponse{ErrMsg: err.Error()}, err
|
||||||
@@ -36,7 +39,7 @@ func (e *Endpoints) CloseSprint(ctx context.Context, in *pb.CloseSprintRequest)
|
|||||||
return out.(*pb.CloseSprintResponse), err
|
return out.(*pb.CloseSprintResponse), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Endpoints) GetSprint(ctx context.Context, in *pb.GetSprintRequest) (*pb.GetSprintResponse, error) {
|
func (e *Endpoints) GetSprint(ctx oldcontext.Context, in *pb.GetSprintRequest) (*pb.GetSprintResponse, error) {
|
||||||
out, err := e.GetSprintEndpoint(ctx, in)
|
out, err := e.GetSprintEndpoint(ctx, in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &pb.GetSprintResponse{ErrMsg: err.Error()}, err
|
return &pb.GetSprintResponse{ErrMsg: err.Error()}, err
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
// Code generated by protoc-gen-gogo.
|
// Code generated by protoc-gen-go.
|
||||||
// source: services/sprint/sprint.proto
|
// source: services/sprint/sprint.proto
|
||||||
// DO NOT EDIT!
|
// DO NOT EDIT!
|
||||||
|
|
||||||
@@ -19,7 +19,7 @@ It has these top-level messages:
|
|||||||
*/
|
*/
|
||||||
package sprint
|
package sprint
|
||||||
|
|
||||||
import proto "github.com/gogo/protobuf/proto"
|
import proto "github.com/golang/protobuf/proto"
|
||||||
import fmt "fmt"
|
import fmt "fmt"
|
||||||
import math "math"
|
import math "math"
|
||||||
|
|
||||||
@@ -37,16 +37,16 @@ var _ = math.Inf
|
|||||||
// is compatible with the proto package it is being compiled against.
|
// is compatible with the proto package it is being compiled against.
|
||||||
// A compilation error at this line likely means your copy of the
|
// A compilation error at this line likely means your copy of the
|
||||||
// proto package needs to be updated.
|
// proto package needs to be updated.
|
||||||
const _ = proto.GoGoProtoPackageIsVersion2 // please upgrade the proto package
|
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||||
|
|
||||||
type AddSprintRequest struct {
|
type AddSprintRequest struct {
|
||||||
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
|
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *AddSprintRequest) Reset() { *m = AddSprintRequest{} }
|
func (m *AddSprintRequest) Reset() { *m = AddSprintRequest{} }
|
||||||
func (m *AddSprintRequest) String() string { return proto.CompactTextString(m) }
|
func (m *AddSprintRequest) String() string { return proto.CompactTextString(m) }
|
||||||
func (*AddSprintRequest) ProtoMessage() {}
|
func (*AddSprintRequest) ProtoMessage() {}
|
||||||
func (*AddSprintRequest) Descriptor() ([]byte, []int) { return fileDescriptorSprint, []int{0} }
|
func (*AddSprintRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
|
||||||
|
|
||||||
func (m *AddSprintRequest) GetName() string {
|
func (m *AddSprintRequest) GetName() string {
|
||||||
if m != nil {
|
if m != nil {
|
||||||
@@ -57,13 +57,13 @@ func (m *AddSprintRequest) GetName() string {
|
|||||||
|
|
||||||
type AddSprintResponse struct {
|
type AddSprintResponse struct {
|
||||||
Sprint *Sprint `protobuf:"bytes,1,opt,name=sprint" json:"sprint,omitempty"`
|
Sprint *Sprint `protobuf:"bytes,1,opt,name=sprint" json:"sprint,omitempty"`
|
||||||
ErrMsg string `protobuf:"bytes,2,opt,name=err_msg,json=errMsg,proto3" json:"err_msg,omitempty"`
|
ErrMsg string `protobuf:"bytes,2,opt,name=err_msg,json=errMsg" json:"err_msg,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *AddSprintResponse) Reset() { *m = AddSprintResponse{} }
|
func (m *AddSprintResponse) Reset() { *m = AddSprintResponse{} }
|
||||||
func (m *AddSprintResponse) String() string { return proto.CompactTextString(m) }
|
func (m *AddSprintResponse) String() string { return proto.CompactTextString(m) }
|
||||||
func (*AddSprintResponse) ProtoMessage() {}
|
func (*AddSprintResponse) ProtoMessage() {}
|
||||||
func (*AddSprintResponse) Descriptor() ([]byte, []int) { return fileDescriptorSprint, []int{1} }
|
func (*AddSprintResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
|
||||||
|
|
||||||
func (m *AddSprintResponse) GetSprint() *Sprint {
|
func (m *AddSprintResponse) GetSprint() *Sprint {
|
||||||
if m != nil {
|
if m != nil {
|
||||||
@@ -80,13 +80,13 @@ func (m *AddSprintResponse) GetErrMsg() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type CloseSprintRequest struct {
|
type CloseSprintRequest struct {
|
||||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
Id string `protobuf:"bytes,1,opt,name=id" json:"id,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *CloseSprintRequest) Reset() { *m = CloseSprintRequest{} }
|
func (m *CloseSprintRequest) Reset() { *m = CloseSprintRequest{} }
|
||||||
func (m *CloseSprintRequest) String() string { return proto.CompactTextString(m) }
|
func (m *CloseSprintRequest) String() string { return proto.CompactTextString(m) }
|
||||||
func (*CloseSprintRequest) ProtoMessage() {}
|
func (*CloseSprintRequest) ProtoMessage() {}
|
||||||
func (*CloseSprintRequest) Descriptor() ([]byte, []int) { return fileDescriptorSprint, []int{2} }
|
func (*CloseSprintRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} }
|
||||||
|
|
||||||
func (m *CloseSprintRequest) GetId() string {
|
func (m *CloseSprintRequest) GetId() string {
|
||||||
if m != nil {
|
if m != nil {
|
||||||
@@ -96,13 +96,13 @@ func (m *CloseSprintRequest) GetId() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type CloseSprintResponse struct {
|
type CloseSprintResponse struct {
|
||||||
ErrMsg string `protobuf:"bytes,1,opt,name=err_msg,json=errMsg,proto3" json:"err_msg,omitempty"`
|
ErrMsg string `protobuf:"bytes,1,opt,name=err_msg,json=errMsg" json:"err_msg,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *CloseSprintResponse) Reset() { *m = CloseSprintResponse{} }
|
func (m *CloseSprintResponse) Reset() { *m = CloseSprintResponse{} }
|
||||||
func (m *CloseSprintResponse) String() string { return proto.CompactTextString(m) }
|
func (m *CloseSprintResponse) String() string { return proto.CompactTextString(m) }
|
||||||
func (*CloseSprintResponse) ProtoMessage() {}
|
func (*CloseSprintResponse) ProtoMessage() {}
|
||||||
func (*CloseSprintResponse) Descriptor() ([]byte, []int) { return fileDescriptorSprint, []int{3} }
|
func (*CloseSprintResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{3} }
|
||||||
|
|
||||||
func (m *CloseSprintResponse) GetErrMsg() string {
|
func (m *CloseSprintResponse) GetErrMsg() string {
|
||||||
if m != nil {
|
if m != nil {
|
||||||
@@ -112,13 +112,13 @@ func (m *CloseSprintResponse) GetErrMsg() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type GetSprintRequest struct {
|
type GetSprintRequest struct {
|
||||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
Id string `protobuf:"bytes,1,opt,name=id" json:"id,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *GetSprintRequest) Reset() { *m = GetSprintRequest{} }
|
func (m *GetSprintRequest) Reset() { *m = GetSprintRequest{} }
|
||||||
func (m *GetSprintRequest) String() string { return proto.CompactTextString(m) }
|
func (m *GetSprintRequest) String() string { return proto.CompactTextString(m) }
|
||||||
func (*GetSprintRequest) ProtoMessage() {}
|
func (*GetSprintRequest) ProtoMessage() {}
|
||||||
func (*GetSprintRequest) Descriptor() ([]byte, []int) { return fileDescriptorSprint, []int{4} }
|
func (*GetSprintRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{4} }
|
||||||
|
|
||||||
func (m *GetSprintRequest) GetId() string {
|
func (m *GetSprintRequest) GetId() string {
|
||||||
if m != nil {
|
if m != nil {
|
||||||
@@ -129,13 +129,13 @@ func (m *GetSprintRequest) GetId() string {
|
|||||||
|
|
||||||
type GetSprintResponse struct {
|
type GetSprintResponse struct {
|
||||||
Sprint *Sprint `protobuf:"bytes,1,opt,name=sprint" json:"sprint,omitempty"`
|
Sprint *Sprint `protobuf:"bytes,1,opt,name=sprint" json:"sprint,omitempty"`
|
||||||
ErrMsg string `protobuf:"bytes,2,opt,name=err_msg,json=errMsg,proto3" json:"err_msg,omitempty"`
|
ErrMsg string `protobuf:"bytes,2,opt,name=err_msg,json=errMsg" json:"err_msg,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *GetSprintResponse) Reset() { *m = GetSprintResponse{} }
|
func (m *GetSprintResponse) Reset() { *m = GetSprintResponse{} }
|
||||||
func (m *GetSprintResponse) String() string { return proto.CompactTextString(m) }
|
func (m *GetSprintResponse) String() string { return proto.CompactTextString(m) }
|
||||||
func (*GetSprintResponse) ProtoMessage() {}
|
func (*GetSprintResponse) ProtoMessage() {}
|
||||||
func (*GetSprintResponse) Descriptor() ([]byte, []int) { return fileDescriptorSprint, []int{5} }
|
func (*GetSprintResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{5} }
|
||||||
|
|
||||||
func (m *GetSprintResponse) GetSprint() *Sprint {
|
func (m *GetSprintResponse) GetSprint() *Sprint {
|
||||||
if m != nil {
|
if m != nil {
|
||||||
@@ -152,15 +152,15 @@ func (m *GetSprintResponse) GetErrMsg() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Sprint struct {
|
type Sprint struct {
|
||||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
Id string `protobuf:"bytes,1,opt,name=id" json:"id,omitempty"`
|
||||||
CreatedAt uint32 `protobuf:"varint,2,opt,name=created_at,json=createdAt,proto3" json:"created_at,omitempty"`
|
CreatedAt uint32 `protobuf:"varint,2,opt,name=created_at,json=createdAt" json:"created_at,omitempty"`
|
||||||
Name string `protobuf:"bytes,3,opt,name=name,proto3" json:"name,omitempty"`
|
Name string `protobuf:"bytes,3,opt,name=name" json:"name,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Sprint) Reset() { *m = Sprint{} }
|
func (m *Sprint) Reset() { *m = Sprint{} }
|
||||||
func (m *Sprint) String() string { return proto.CompactTextString(m) }
|
func (m *Sprint) String() string { return proto.CompactTextString(m) }
|
||||||
func (*Sprint) ProtoMessage() {}
|
func (*Sprint) ProtoMessage() {}
|
||||||
func (*Sprint) Descriptor() ([]byte, []int) { return fileDescriptorSprint, []int{6} }
|
func (*Sprint) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{6} }
|
||||||
|
|
||||||
func (m *Sprint) GetId() string {
|
func (m *Sprint) GetId() string {
|
||||||
if m != nil {
|
if m != nil {
|
||||||
@@ -331,9 +331,9 @@ var _SprintService_serviceDesc = grpc.ServiceDesc{
|
|||||||
Metadata: "services/sprint/sprint.proto",
|
Metadata: "services/sprint/sprint.proto",
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() { proto.RegisterFile("services/sprint/sprint.proto", fileDescriptorSprint) }
|
func init() { proto.RegisterFile("services/sprint/sprint.proto", fileDescriptor0) }
|
||||||
|
|
||||||
var fileDescriptorSprint = []byte{
|
var fileDescriptor0 = []byte{
|
||||||
// 290 bytes of a gzipped FileDescriptorProto
|
// 290 bytes of a gzipped FileDescriptorProto
|
||||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xac, 0x52, 0x4d, 0x4b, 0xc3, 0x40,
|
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xac, 0x52, 0x4d, 0x4b, 0xc3, 0x40,
|
||||||
0x10, 0x6d, 0xaa, 0x44, 0x32, 0xa5, 0xa5, 0x1d, 0x0f, 0xc6, 0xa8, 0x20, 0x8b, 0x14, 0x4f, 0x11,
|
0x10, 0x6d, 0xaa, 0x44, 0x32, 0xa5, 0xa5, 0x1d, 0x0f, 0xc6, 0xa8, 0x20, 0x8b, 0x14, 0x4f, 0x11,
|
||||||
|
@@ -1,10 +1,11 @@
|
|||||||
package sprint_grpctransport
|
package sprint_grpctransport
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
context "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
grpctransport "github.com/go-kit/kit/transport/grpc"
|
grpctransport "github.com/go-kit/kit/transport/grpc"
|
||||||
context "golang.org/x/net/context"
|
oldcontext "golang.org/x/net/context"
|
||||||
|
|
||||||
endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/endpoints"
|
endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/endpoints"
|
||||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/pb"
|
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/pb"
|
||||||
@@ -13,12 +14,12 @@ import (
|
|||||||
// avoid import errors
|
// avoid import errors
|
||||||
var _ = fmt.Errorf
|
var _ = fmt.Errorf
|
||||||
|
|
||||||
func MakeGRPCServer(ctx context.Context, endpoints endpoints.Endpoints) pb.SprintServiceServer {
|
func MakeGRPCServer(endpoints endpoints.Endpoints) pb.SprintServiceServer {
|
||||||
options := []grpctransport.ServerOption{}
|
var options []grpctransport.ServerOption
|
||||||
|
_ = options
|
||||||
return &grpcServer{
|
return &grpcServer{
|
||||||
|
|
||||||
addsprint: grpctransport.NewServer(
|
addsprint: grpctransport.NewServer(
|
||||||
ctx,
|
|
||||||
endpoints.AddSprintEndpoint,
|
endpoints.AddSprintEndpoint,
|
||||||
decodeRequest,
|
decodeRequest,
|
||||||
encodeAddSprintResponse,
|
encodeAddSprintResponse,
|
||||||
@@ -26,7 +27,6 @@ func MakeGRPCServer(ctx context.Context, endpoints endpoints.Endpoints) pb.Sprin
|
|||||||
),
|
),
|
||||||
|
|
||||||
closesprint: grpctransport.NewServer(
|
closesprint: grpctransport.NewServer(
|
||||||
ctx,
|
|
||||||
endpoints.CloseSprintEndpoint,
|
endpoints.CloseSprintEndpoint,
|
||||||
decodeRequest,
|
decodeRequest,
|
||||||
encodeCloseSprintResponse,
|
encodeCloseSprintResponse,
|
||||||
@@ -34,7 +34,6 @@ func MakeGRPCServer(ctx context.Context, endpoints endpoints.Endpoints) pb.Sprin
|
|||||||
),
|
),
|
||||||
|
|
||||||
getsprint: grpctransport.NewServer(
|
getsprint: grpctransport.NewServer(
|
||||||
ctx,
|
|
||||||
endpoints.GetSprintEndpoint,
|
endpoints.GetSprintEndpoint,
|
||||||
decodeRequest,
|
decodeRequest,
|
||||||
encodeGetSprintResponse,
|
encodeGetSprintResponse,
|
||||||
@@ -51,7 +50,7 @@ type grpcServer struct {
|
|||||||
getsprint grpctransport.Handler
|
getsprint grpctransport.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *grpcServer) AddSprint(ctx context.Context, req *pb.AddSprintRequest) (*pb.AddSprintResponse, error) {
|
func (s *grpcServer) AddSprint(ctx oldcontext.Context, req *pb.AddSprintRequest) (*pb.AddSprintResponse, error) {
|
||||||
_, rep, err := s.addsprint.ServeGRPC(ctx, req)
|
_, rep, err := s.addsprint.ServeGRPC(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -64,7 +63,7 @@ func encodeAddSprintResponse(ctx context.Context, response interface{}) (interfa
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *grpcServer) CloseSprint(ctx context.Context, req *pb.CloseSprintRequest) (*pb.CloseSprintResponse, error) {
|
func (s *grpcServer) CloseSprint(ctx oldcontext.Context, req *pb.CloseSprintRequest) (*pb.CloseSprintResponse, error) {
|
||||||
_, rep, err := s.closesprint.ServeGRPC(ctx, req)
|
_, rep, err := s.closesprint.ServeGRPC(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -77,7 +76,7 @@ func encodeCloseSprintResponse(ctx context.Context, response interface{}) (inter
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *grpcServer) GetSprint(ctx context.Context, req *pb.GetSprintRequest) (*pb.GetSprintResponse, error) {
|
func (s *grpcServer) GetSprint(ctx oldcontext.Context, req *pb.GetSprintRequest) (*pb.GetSprintResponse, error) {
|
||||||
_, rep, err := s.getsprint.ServeGRPC(ctx, req)
|
_, rep, err := s.getsprint.ServeGRPC(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@@ -1,8 +1,8 @@
|
|||||||
package sprint_httptransport
|
package sprint_httptransport
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
context "golang.org/x/net/context"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -12,9 +12,12 @@ import (
|
|||||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/pb"
|
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/pb"
|
||||||
)
|
)
|
||||||
|
|
||||||
func MakeAddSprintHandler(ctx context.Context, svc pb.SprintServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
var _ = log.Printf
|
||||||
|
var _ = gokit_endpoint.Chain
|
||||||
|
var _ = httptransport.NewClient
|
||||||
|
|
||||||
|
func MakeAddSprintHandler(svc pb.SprintServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
||||||
return httptransport.NewServer(
|
return httptransport.NewServer(
|
||||||
ctx,
|
|
||||||
endpoint,
|
endpoint,
|
||||||
decodeAddSprintRequest,
|
decodeAddSprintRequest,
|
||||||
encodeResponse,
|
encodeResponse,
|
||||||
@@ -30,9 +33,8 @@ func decodeAddSprintRequest(ctx context.Context, r *http.Request) (interface{},
|
|||||||
return &req, nil
|
return &req, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func MakeCloseSprintHandler(ctx context.Context, svc pb.SprintServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
func MakeCloseSprintHandler(svc pb.SprintServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
||||||
return httptransport.NewServer(
|
return httptransport.NewServer(
|
||||||
ctx,
|
|
||||||
endpoint,
|
endpoint,
|
||||||
decodeCloseSprintRequest,
|
decodeCloseSprintRequest,
|
||||||
encodeResponse,
|
encodeResponse,
|
||||||
@@ -48,9 +50,8 @@ func decodeCloseSprintRequest(ctx context.Context, r *http.Request) (interface{}
|
|||||||
return &req, nil
|
return &req, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func MakeGetSprintHandler(ctx context.Context, svc pb.SprintServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
func MakeGetSprintHandler(svc pb.SprintServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
||||||
return httptransport.NewServer(
|
return httptransport.NewServer(
|
||||||
ctx,
|
|
||||||
endpoint,
|
endpoint,
|
||||||
decodeGetSprintRequest,
|
decodeGetSprintRequest,
|
||||||
encodeResponse,
|
encodeResponse,
|
||||||
@@ -70,16 +71,16 @@ func encodeResponse(ctx context.Context, w http.ResponseWriter, response interfa
|
|||||||
return json.NewEncoder(w).Encode(response)
|
return json.NewEncoder(w).Encode(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterHandlers(ctx context.Context, svc pb.SprintServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error {
|
func RegisterHandlers(svc pb.SprintServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error {
|
||||||
|
|
||||||
log.Println("new HTTP endpoint: \"/AddSprint\" (service=Sprint)")
|
log.Println("new HTTP endpoint: \"/AddSprint\" (service=Sprint)")
|
||||||
mux.Handle("/AddSprint", MakeAddSprintHandler(ctx, svc, endpoints.AddSprintEndpoint))
|
mux.Handle("/AddSprint", MakeAddSprintHandler(svc, endpoints.AddSprintEndpoint))
|
||||||
|
|
||||||
log.Println("new HTTP endpoint: \"/CloseSprint\" (service=Sprint)")
|
log.Println("new HTTP endpoint: \"/CloseSprint\" (service=Sprint)")
|
||||||
mux.Handle("/CloseSprint", MakeCloseSprintHandler(ctx, svc, endpoints.CloseSprintEndpoint))
|
mux.Handle("/CloseSprint", MakeCloseSprintHandler(svc, endpoints.CloseSprintEndpoint))
|
||||||
|
|
||||||
log.Println("new HTTP endpoint: \"/GetSprint\" (service=Sprint)")
|
log.Println("new HTTP endpoint: \"/GetSprint\" (service=Sprint)")
|
||||||
mux.Handle("/GetSprint", MakeGetSprintHandler(ctx, svc, endpoints.GetSprintEndpoint))
|
mux.Handle("/GetSprint", MakeGetSprintHandler(svc, endpoints.GetSprintEndpoint))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -1,11 +1,12 @@
|
|||||||
package user_clientgrpc
|
package user_clientgrpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
context "context"
|
||||||
|
|
||||||
jwt "github.com/go-kit/kit/auth/jwt"
|
jwt "github.com/go-kit/kit/auth/jwt"
|
||||||
"github.com/go-kit/kit/endpoint"
|
"github.com/go-kit/kit/endpoint"
|
||||||
"github.com/go-kit/kit/log"
|
"github.com/go-kit/kit/log"
|
||||||
grpctransport "github.com/go-kit/kit/transport/grpc"
|
grpctransport "github.com/go-kit/kit/transport/grpc"
|
||||||
context "golang.org/x/net/context"
|
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
|
||||||
endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/endpoints"
|
endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/endpoints"
|
||||||
|
@@ -1,14 +1,17 @@
|
|||||||
package user_endpoints
|
package user_endpoints
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
context "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/go-kit/kit/endpoint"
|
"github.com/go-kit/kit/endpoint"
|
||||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/pb"
|
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/pb"
|
||||||
context "golang.org/x/net/context"
|
oldcontext "golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ = endpoint.Chain
|
||||||
var _ = fmt.Errorf
|
var _ = fmt.Errorf
|
||||||
|
var _ = context.Background
|
||||||
|
|
||||||
type StreamEndpoint func(server interface{}, req interface{}) (err error)
|
type StreamEndpoint func(server interface{}, req interface{}) (err error)
|
||||||
|
|
||||||
@@ -18,7 +21,7 @@ type Endpoints struct {
|
|||||||
GetUserEndpoint endpoint.Endpoint
|
GetUserEndpoint endpoint.Endpoint
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Endpoints) CreateUser(ctx context.Context, in *pb.CreateUserRequest) (*pb.CreateUserResponse, error) {
|
func (e *Endpoints) CreateUser(ctx oldcontext.Context, in *pb.CreateUserRequest) (*pb.CreateUserResponse, error) {
|
||||||
out, err := e.CreateUserEndpoint(ctx, in)
|
out, err := e.CreateUserEndpoint(ctx, in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &pb.CreateUserResponse{ErrMsg: err.Error()}, err
|
return &pb.CreateUserResponse{ErrMsg: err.Error()}, err
|
||||||
@@ -26,7 +29,7 @@ func (e *Endpoints) CreateUser(ctx context.Context, in *pb.CreateUserRequest) (*
|
|||||||
return out.(*pb.CreateUserResponse), err
|
return out.(*pb.CreateUserResponse), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Endpoints) GetUser(ctx context.Context, in *pb.GetUserRequest) (*pb.GetUserResponse, error) {
|
func (e *Endpoints) GetUser(ctx oldcontext.Context, in *pb.GetUserRequest) (*pb.GetUserResponse, error) {
|
||||||
out, err := e.GetUserEndpoint(ctx, in)
|
out, err := e.GetUserEndpoint(ctx, in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &pb.GetUserResponse{ErrMsg: err.Error()}, err
|
return &pb.GetUserResponse{ErrMsg: err.Error()}, err
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
// Code generated by protoc-gen-gogo.
|
// Code generated by protoc-gen-go.
|
||||||
// source: services/user/user.proto
|
// source: services/user/user.proto
|
||||||
// DO NOT EDIT!
|
// DO NOT EDIT!
|
||||||
|
|
||||||
@@ -17,7 +17,7 @@ It has these top-level messages:
|
|||||||
*/
|
*/
|
||||||
package user
|
package user
|
||||||
|
|
||||||
import proto "github.com/gogo/protobuf/proto"
|
import proto "github.com/golang/protobuf/proto"
|
||||||
import fmt "fmt"
|
import fmt "fmt"
|
||||||
import math "math"
|
import math "math"
|
||||||
|
|
||||||
@@ -35,16 +35,16 @@ var _ = math.Inf
|
|||||||
// is compatible with the proto package it is being compiled against.
|
// is compatible with the proto package it is being compiled against.
|
||||||
// A compilation error at this line likely means your copy of the
|
// A compilation error at this line likely means your copy of the
|
||||||
// proto package needs to be updated.
|
// proto package needs to be updated.
|
||||||
const _ = proto.GoGoProtoPackageIsVersion2 // please upgrade the proto package
|
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||||
|
|
||||||
type CreateUserRequest struct {
|
type CreateUserRequest struct {
|
||||||
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
|
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *CreateUserRequest) Reset() { *m = CreateUserRequest{} }
|
func (m *CreateUserRequest) Reset() { *m = CreateUserRequest{} }
|
||||||
func (m *CreateUserRequest) String() string { return proto.CompactTextString(m) }
|
func (m *CreateUserRequest) String() string { return proto.CompactTextString(m) }
|
||||||
func (*CreateUserRequest) ProtoMessage() {}
|
func (*CreateUserRequest) ProtoMessage() {}
|
||||||
func (*CreateUserRequest) Descriptor() ([]byte, []int) { return fileDescriptorUser, []int{0} }
|
func (*CreateUserRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
|
||||||
|
|
||||||
func (m *CreateUserRequest) GetName() string {
|
func (m *CreateUserRequest) GetName() string {
|
||||||
if m != nil {
|
if m != nil {
|
||||||
@@ -55,13 +55,13 @@ func (m *CreateUserRequest) GetName() string {
|
|||||||
|
|
||||||
type CreateUserResponse struct {
|
type CreateUserResponse struct {
|
||||||
User *User `protobuf:"bytes,1,opt,name=user" json:"user,omitempty"`
|
User *User `protobuf:"bytes,1,opt,name=user" json:"user,omitempty"`
|
||||||
ErrMsg string `protobuf:"bytes,2,opt,name=err_msg,json=errMsg,proto3" json:"err_msg,omitempty"`
|
ErrMsg string `protobuf:"bytes,2,opt,name=err_msg,json=errMsg" json:"err_msg,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *CreateUserResponse) Reset() { *m = CreateUserResponse{} }
|
func (m *CreateUserResponse) Reset() { *m = CreateUserResponse{} }
|
||||||
func (m *CreateUserResponse) String() string { return proto.CompactTextString(m) }
|
func (m *CreateUserResponse) String() string { return proto.CompactTextString(m) }
|
||||||
func (*CreateUserResponse) ProtoMessage() {}
|
func (*CreateUserResponse) ProtoMessage() {}
|
||||||
func (*CreateUserResponse) Descriptor() ([]byte, []int) { return fileDescriptorUser, []int{1} }
|
func (*CreateUserResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
|
||||||
|
|
||||||
func (m *CreateUserResponse) GetUser() *User {
|
func (m *CreateUserResponse) GetUser() *User {
|
||||||
if m != nil {
|
if m != nil {
|
||||||
@@ -78,13 +78,13 @@ func (m *CreateUserResponse) GetErrMsg() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type GetUserRequest struct {
|
type GetUserRequest struct {
|
||||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
Id string `protobuf:"bytes,1,opt,name=id" json:"id,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *GetUserRequest) Reset() { *m = GetUserRequest{} }
|
func (m *GetUserRequest) Reset() { *m = GetUserRequest{} }
|
||||||
func (m *GetUserRequest) String() string { return proto.CompactTextString(m) }
|
func (m *GetUserRequest) String() string { return proto.CompactTextString(m) }
|
||||||
func (*GetUserRequest) ProtoMessage() {}
|
func (*GetUserRequest) ProtoMessage() {}
|
||||||
func (*GetUserRequest) Descriptor() ([]byte, []int) { return fileDescriptorUser, []int{2} }
|
func (*GetUserRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} }
|
||||||
|
|
||||||
func (m *GetUserRequest) GetId() string {
|
func (m *GetUserRequest) GetId() string {
|
||||||
if m != nil {
|
if m != nil {
|
||||||
@@ -95,13 +95,13 @@ func (m *GetUserRequest) GetId() string {
|
|||||||
|
|
||||||
type GetUserResponse struct {
|
type GetUserResponse struct {
|
||||||
User *User `protobuf:"bytes,1,opt,name=user" json:"user,omitempty"`
|
User *User `protobuf:"bytes,1,opt,name=user" json:"user,omitempty"`
|
||||||
ErrMsg string `protobuf:"bytes,2,opt,name=err_msg,json=errMsg,proto3" json:"err_msg,omitempty"`
|
ErrMsg string `protobuf:"bytes,2,opt,name=err_msg,json=errMsg" json:"err_msg,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *GetUserResponse) Reset() { *m = GetUserResponse{} }
|
func (m *GetUserResponse) Reset() { *m = GetUserResponse{} }
|
||||||
func (m *GetUserResponse) String() string { return proto.CompactTextString(m) }
|
func (m *GetUserResponse) String() string { return proto.CompactTextString(m) }
|
||||||
func (*GetUserResponse) ProtoMessage() {}
|
func (*GetUserResponse) ProtoMessage() {}
|
||||||
func (*GetUserResponse) Descriptor() ([]byte, []int) { return fileDescriptorUser, []int{3} }
|
func (*GetUserResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{3} }
|
||||||
|
|
||||||
func (m *GetUserResponse) GetUser() *User {
|
func (m *GetUserResponse) GetUser() *User {
|
||||||
if m != nil {
|
if m != nil {
|
||||||
@@ -118,14 +118,14 @@ func (m *GetUserResponse) GetErrMsg() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
Id string `protobuf:"bytes,1,opt,name=id" json:"id,omitempty"`
|
||||||
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
|
Name string `protobuf:"bytes,2,opt,name=name" json:"name,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *User) Reset() { *m = User{} }
|
func (m *User) Reset() { *m = User{} }
|
||||||
func (m *User) String() string { return proto.CompactTextString(m) }
|
func (m *User) String() string { return proto.CompactTextString(m) }
|
||||||
func (*User) ProtoMessage() {}
|
func (*User) ProtoMessage() {}
|
||||||
func (*User) Descriptor() ([]byte, []int) { return fileDescriptorUser, []int{4} }
|
func (*User) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{4} }
|
||||||
|
|
||||||
func (m *User) GetId() string {
|
func (m *User) GetId() string {
|
||||||
if m != nil {
|
if m != nil {
|
||||||
@@ -254,9 +254,9 @@ var _UserService_serviceDesc = grpc.ServiceDesc{
|
|||||||
Metadata: "services/user/user.proto",
|
Metadata: "services/user/user.proto",
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() { proto.RegisterFile("services/user/user.proto", fileDescriptorUser) }
|
func init() { proto.RegisterFile("services/user/user.proto", fileDescriptor0) }
|
||||||
|
|
||||||
var fileDescriptorUser = []byte{
|
var fileDescriptor0 = []byte{
|
||||||
// 236 bytes of a gzipped FileDescriptorProto
|
// 236 bytes of a gzipped FileDescriptorProto
|
||||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x92, 0x28, 0x4e, 0x2d, 0x2a,
|
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x92, 0x28, 0x4e, 0x2d, 0x2a,
|
||||||
0xcb, 0x4c, 0x4e, 0x2d, 0xd6, 0x2f, 0x2d, 0x4e, 0x2d, 0x02, 0x13, 0x7a, 0x05, 0x45, 0xf9, 0x25,
|
0xcb, 0x4c, 0x4e, 0x2d, 0xd6, 0x2f, 0x2d, 0x4e, 0x2d, 0x02, 0x13, 0x7a, 0x05, 0x45, 0xf9, 0x25,
|
||||||
|
@@ -1,10 +1,11 @@
|
|||||||
package user_grpctransport
|
package user_grpctransport
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
context "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
grpctransport "github.com/go-kit/kit/transport/grpc"
|
grpctransport "github.com/go-kit/kit/transport/grpc"
|
||||||
context "golang.org/x/net/context"
|
oldcontext "golang.org/x/net/context"
|
||||||
|
|
||||||
endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/endpoints"
|
endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/endpoints"
|
||||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/pb"
|
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/pb"
|
||||||
@@ -13,12 +14,12 @@ import (
|
|||||||
// avoid import errors
|
// avoid import errors
|
||||||
var _ = fmt.Errorf
|
var _ = fmt.Errorf
|
||||||
|
|
||||||
func MakeGRPCServer(ctx context.Context, endpoints endpoints.Endpoints) pb.UserServiceServer {
|
func MakeGRPCServer(endpoints endpoints.Endpoints) pb.UserServiceServer {
|
||||||
options := []grpctransport.ServerOption{}
|
var options []grpctransport.ServerOption
|
||||||
|
_ = options
|
||||||
return &grpcServer{
|
return &grpcServer{
|
||||||
|
|
||||||
createuser: grpctransport.NewServer(
|
createuser: grpctransport.NewServer(
|
||||||
ctx,
|
|
||||||
endpoints.CreateUserEndpoint,
|
endpoints.CreateUserEndpoint,
|
||||||
decodeRequest,
|
decodeRequest,
|
||||||
encodeCreateUserResponse,
|
encodeCreateUserResponse,
|
||||||
@@ -26,7 +27,6 @@ func MakeGRPCServer(ctx context.Context, endpoints endpoints.Endpoints) pb.UserS
|
|||||||
),
|
),
|
||||||
|
|
||||||
getuser: grpctransport.NewServer(
|
getuser: grpctransport.NewServer(
|
||||||
ctx,
|
|
||||||
endpoints.GetUserEndpoint,
|
endpoints.GetUserEndpoint,
|
||||||
decodeRequest,
|
decodeRequest,
|
||||||
encodeGetUserResponse,
|
encodeGetUserResponse,
|
||||||
@@ -41,7 +41,7 @@ type grpcServer struct {
|
|||||||
getuser grpctransport.Handler
|
getuser grpctransport.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *grpcServer) CreateUser(ctx context.Context, req *pb.CreateUserRequest) (*pb.CreateUserResponse, error) {
|
func (s *grpcServer) CreateUser(ctx oldcontext.Context, req *pb.CreateUserRequest) (*pb.CreateUserResponse, error) {
|
||||||
_, rep, err := s.createuser.ServeGRPC(ctx, req)
|
_, rep, err := s.createuser.ServeGRPC(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -54,7 +54,7 @@ func encodeCreateUserResponse(ctx context.Context, response interface{}) (interf
|
|||||||
return resp, nil
|
return resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *grpcServer) GetUser(ctx context.Context, req *pb.GetUserRequest) (*pb.GetUserResponse, error) {
|
func (s *grpcServer) GetUser(ctx oldcontext.Context, req *pb.GetUserRequest) (*pb.GetUserResponse, error) {
|
||||||
_, rep, err := s.getuser.ServeGRPC(ctx, req)
|
_, rep, err := s.getuser.ServeGRPC(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@@ -1,8 +1,8 @@
|
|||||||
package user_httptransport
|
package user_httptransport
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
context "golang.org/x/net/context"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -12,9 +12,12 @@ import (
|
|||||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/pb"
|
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/pb"
|
||||||
)
|
)
|
||||||
|
|
||||||
func MakeCreateUserHandler(ctx context.Context, svc pb.UserServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
var _ = log.Printf
|
||||||
|
var _ = gokit_endpoint.Chain
|
||||||
|
var _ = httptransport.NewClient
|
||||||
|
|
||||||
|
func MakeCreateUserHandler(svc pb.UserServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
||||||
return httptransport.NewServer(
|
return httptransport.NewServer(
|
||||||
ctx,
|
|
||||||
endpoint,
|
endpoint,
|
||||||
decodeCreateUserRequest,
|
decodeCreateUserRequest,
|
||||||
encodeResponse,
|
encodeResponse,
|
||||||
@@ -30,9 +33,8 @@ func decodeCreateUserRequest(ctx context.Context, r *http.Request) (interface{},
|
|||||||
return &req, nil
|
return &req, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func MakeGetUserHandler(ctx context.Context, svc pb.UserServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
func MakeGetUserHandler(svc pb.UserServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
||||||
return httptransport.NewServer(
|
return httptransport.NewServer(
|
||||||
ctx,
|
|
||||||
endpoint,
|
endpoint,
|
||||||
decodeGetUserRequest,
|
decodeGetUserRequest,
|
||||||
encodeResponse,
|
encodeResponse,
|
||||||
@@ -52,13 +54,13 @@ func encodeResponse(ctx context.Context, w http.ResponseWriter, response interfa
|
|||||||
return json.NewEncoder(w).Encode(response)
|
return json.NewEncoder(w).Encode(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterHandlers(ctx context.Context, svc pb.UserServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error {
|
func RegisterHandlers(svc pb.UserServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error {
|
||||||
|
|
||||||
log.Println("new HTTP endpoint: \"/CreateUser\" (service=User)")
|
log.Println("new HTTP endpoint: \"/CreateUser\" (service=User)")
|
||||||
mux.Handle("/CreateUser", MakeCreateUserHandler(ctx, svc, endpoints.CreateUserEndpoint))
|
mux.Handle("/CreateUser", MakeCreateUserHandler(svc, endpoints.CreateUserEndpoint))
|
||||||
|
|
||||||
log.Println("new HTTP endpoint: \"/GetUser\" (service=User)")
|
log.Println("new HTTP endpoint: \"/GetUser\" (service=User)")
|
||||||
mux.Handle("/GetUser", MakeGetUserHandler(ctx, svc, endpoints.GetUserEndpoint))
|
mux.Handle("/GetUser", MakeGetUserHandler(svc, endpoints.GetUserEndpoint))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -1,8 +1,9 @@
|
|||||||
package {{.File.Package}}_clientgrpc
|
package {{.File.Package}}_clientgrpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/go-kit/kit/log"
|
context "context"
|
||||||
context "golang.org/x/net/context"
|
|
||||||
|
"github.com/go-kit/kit/log"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
grpctransport "github.com/go-kit/kit/transport/grpc"
|
grpctransport "github.com/go-kit/kit/transport/grpc"
|
||||||
"github.com/go-kit/kit/endpoint"
|
"github.com/go-kit/kit/endpoint"
|
||||||
|
@@ -3,9 +3,10 @@ package {{.File.Package}}_endpoints
|
|||||||
{{$file := .File}}
|
{{$file := .File}}
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
context "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
context "golang.org/x/net/context"
|
oldcontext "golang.org/x/net/context"
|
||||||
pb "{{cat .GoPWD "/" .DestinationDir | nospace | clean}}/pb"
|
pb "{{cat .GoPWD "/" .DestinationDir | nospace | clean}}/pb"
|
||||||
"github.com/go-kit/kit/endpoint"
|
"github.com/go-kit/kit/endpoint"
|
||||||
)
|
)
|
||||||
@@ -43,7 +44,7 @@ type Endpoints struct {
|
|||||||
return fmt.Errorf("not implemented")
|
return fmt.Errorf("not implemented")
|
||||||
}
|
}
|
||||||
{{else}}
|
{{else}}
|
||||||
func (e *Endpoints){{.Name}}(ctx context.Context, in *pb.{{.InputType | splitArray "." | last}}) (*pb.{{.OutputType | splitArray "." | last}}, error) {
|
func (e *Endpoints){{.Name}}(ctx oldcontext.Context, in *pb.{{.InputType | splitArray "." | last}}) (*pb.{{.OutputType | splitArray "." | last}}, error) {
|
||||||
out, err := e.{{.Name}}Endpoint(ctx, in)
|
out, err := e.{{.Name}}Endpoint(ctx, in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &pb.{{.OutputType | splitArray "." | last}}{ErrMsg: err.Error()}, err
|
return &pb.{{.OutputType | splitArray "." | last}}{ErrMsg: err.Error()}, err
|
||||||
|
@@ -3,9 +3,10 @@ package {{.File.Package}}_grpctransport
|
|||||||
{{$file := .File}}
|
{{$file := .File}}
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
context "context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
context "golang.org/x/net/context"
|
oldcontext "golang.org/x/net/context"
|
||||||
grpctransport "github.com/go-kit/kit/transport/grpc"
|
grpctransport "github.com/go-kit/kit/transport/grpc"
|
||||||
|
|
||||||
pb "{{cat .GoPWD "/" .DestinationDir | nospace | clean}}/pb"
|
pb "{{cat .GoPWD "/" .DestinationDir | nospace | clean}}/pb"
|
||||||
@@ -15,7 +16,7 @@ import (
|
|||||||
// avoid import errors
|
// avoid import errors
|
||||||
var _ = fmt.Errorf
|
var _ = fmt.Errorf
|
||||||
|
|
||||||
func MakeGRPCServer(ctx context.Context, endpoints endpoints.Endpoints) pb.{{.File.Package | title}}ServiceServer {
|
func MakeGRPCServer(endpoints endpoints.Endpoints) pb.{{.File.Package | title}}ServiceServer {
|
||||||
var options []grpctransport.ServerOption
|
var options []grpctransport.ServerOption
|
||||||
_ = options
|
_ = options
|
||||||
return &grpcServer{
|
return &grpcServer{
|
||||||
@@ -26,7 +27,6 @@ func MakeGRPCServer(ctx context.Context, endpoints endpoints.Endpoints) pb.{{.Fi
|
|||||||
},
|
},
|
||||||
{{else}}
|
{{else}}
|
||||||
{{.Name | lower}}: grpctransport.NewServer(
|
{{.Name | lower}}: grpctransport.NewServer(
|
||||||
ctx,
|
|
||||||
endpoints.{{.Name}}Endpoint,
|
endpoints.{{.Name}}Endpoint,
|
||||||
decodeRequest,
|
decodeRequest,
|
||||||
encode{{.Name}}Response,
|
encode{{.Name}}Response,
|
||||||
@@ -57,7 +57,7 @@ type grpcServer struct {
|
|||||||
return s.{{.Name | lower}}.Do(server, req)
|
return s.{{.Name | lower}}.Do(server, req)
|
||||||
}
|
}
|
||||||
{{else}}
|
{{else}}
|
||||||
func (s *grpcServer) {{.Name}}(ctx context.Context, req *pb.{{.InputType | splitArray "." | last}}) (*pb.{{.OutputType | splitArray "." | last}}, error) {
|
func (s *grpcServer) {{.Name}}(ctx oldcontext.Context, req *pb.{{.InputType | splitArray "." | last}}) (*pb.{{.OutputType | splitArray "." | last}}, error) {
|
||||||
_, rep, err := s.{{.Name | lower}}.ServeGRPC(ctx, req)
|
_, rep, err := s.{{.Name | lower}}.ServeGRPC(ctx, req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@@ -6,7 +6,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
context "golang.org/x/net/context"
|
"context"
|
||||||
|
|
||||||
pb "{{cat .GoPWD "/" .DestinationDir | nospace | clean}}/pb"
|
pb "{{cat .GoPWD "/" .DestinationDir | nospace | clean}}/pb"
|
||||||
gokit_endpoint "github.com/go-kit/kit/endpoint"
|
gokit_endpoint "github.com/go-kit/kit/endpoint"
|
||||||
@@ -21,9 +21,8 @@ var _ = httptransport.NewClient
|
|||||||
|
|
||||||
{{range .Service.Method}}
|
{{range .Service.Method}}
|
||||||
{{if and (not .ServerStreaming) (not .ClientStreaming)}}
|
{{if and (not .ServerStreaming) (not .ClientStreaming)}}
|
||||||
func Make{{.Name}}Handler(ctx context.Context, svc pb.{{$file.Package | title}}ServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
func Make{{.Name}}Handler(svc pb.{{$file.Package | title}}ServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
||||||
return httptransport.NewServer(
|
return httptransport.NewServer(
|
||||||
ctx,
|
|
||||||
endpoint,
|
endpoint,
|
||||||
decode{{.Name}}Request,
|
decode{{.Name}}Request,
|
||||||
encodeResponse,
|
encodeResponse,
|
||||||
@@ -45,11 +44,11 @@ func encodeResponse(ctx context.Context, w http.ResponseWriter, response interfa
|
|||||||
return json.NewEncoder(w).Encode(response)
|
return json.NewEncoder(w).Encode(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterHandlers(ctx context.Context, svc pb.{{$file.Package | title}}ServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error {
|
func RegisterHandlers(svc pb.{{$file.Package | title}}ServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error {
|
||||||
{{range .Service.Method}}
|
{{range .Service.Method}}
|
||||||
{{if and (not .ServerStreaming) (not .ClientStreaming)}}
|
{{if and (not .ServerStreaming) (not .ClientStreaming)}}
|
||||||
log.Println("new HTTP endpoint: \"/{{.Name}}\" (service={{$file.Package | title}})")
|
log.Println("new HTTP endpoint: \"/{{.Name}}\" (service={{$file.Package | title}})")
|
||||||
mux.Handle("/{{.Name}}", Make{{.Name}}Handler(ctx, svc, endpoints.{{.Name}}Endpoint))
|
mux.Handle("/{{.Name}}", Make{{.Name}}Handler(svc, endpoints.{{.Name}}Endpoint))
|
||||||
{{end}}
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
return nil
|
return nil
|
||||||
|
18
examples/import/Makefile
Normal file
18
examples/import/Makefile
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
.PHONY: build
|
||||||
|
build:
|
||||||
|
mkdir -p output
|
||||||
|
|
||||||
|
# generate pb.go inluding imported proto
|
||||||
|
protoc --go_out=Mproto/common.proto=github.com/moul/protoc-gen-gotemplate/examples/import/output/models/common:./output proto/article.proto
|
||||||
|
protoc --go_out=,plugins=grpc:./output proto/common.proto
|
||||||
|
|
||||||
|
# build our go file based on our template
|
||||||
|
protoc -I. --gotemplate_out=template_dir=templates,debug=true:output proto/article.proto
|
||||||
|
|
||||||
|
.PHONY: re
|
||||||
|
re: clean build
|
||||||
|
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
rm -rf output
|
113
examples/import/output/models/article/article.pb.go
Normal file
113
examples/import/output/models/article/article.pb.go
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
// Code generated by protoc-gen-go.
|
||||||
|
// source: proto/article.proto
|
||||||
|
// DO NOT EDIT!
|
||||||
|
|
||||||
|
/*
|
||||||
|
Package article is a generated protocol buffer package.
|
||||||
|
|
||||||
|
It is generated from these files:
|
||||||
|
proto/article.proto
|
||||||
|
|
||||||
|
It has these top-level messages:
|
||||||
|
GetArticleRequest
|
||||||
|
GetArticleResponse
|
||||||
|
Article
|
||||||
|
*/
|
||||||
|
package article
|
||||||
|
|
||||||
|
import proto "github.com/golang/protobuf/proto"
|
||||||
|
import fmt "fmt"
|
||||||
|
import math "math"
|
||||||
|
import common "github.com/moul/protoc-gen-gotemplate/examples/import/output/models/common"
|
||||||
|
|
||||||
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
|
var _ = proto.Marshal
|
||||||
|
var _ = fmt.Errorf
|
||||||
|
var _ = math.Inf
|
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the proto package it is being compiled against.
|
||||||
|
// A compilation error at this line likely means your copy of the
|
||||||
|
// proto package needs to be updated.
|
||||||
|
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||||
|
|
||||||
|
type GetArticleRequest struct {
|
||||||
|
Getarticle *common.GetArticle `protobuf:"bytes,1,opt,name=getarticle" json:"getarticle,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *GetArticleRequest) Reset() { *m = GetArticleRequest{} }
|
||||||
|
func (m *GetArticleRequest) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*GetArticleRequest) ProtoMessage() {}
|
||||||
|
func (*GetArticleRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
|
||||||
|
|
||||||
|
func (m *GetArticleRequest) GetGetarticle() *common.GetArticle {
|
||||||
|
if m != nil {
|
||||||
|
return m.Getarticle
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetArticleResponse struct {
|
||||||
|
Article *Article `protobuf:"bytes,1,opt,name=article" json:"article,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *GetArticleResponse) Reset() { *m = GetArticleResponse{} }
|
||||||
|
func (m *GetArticleResponse) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*GetArticleResponse) ProtoMessage() {}
|
||||||
|
func (*GetArticleResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
|
||||||
|
|
||||||
|
func (m *GetArticleResponse) GetArticle() *Article {
|
||||||
|
if m != nil {
|
||||||
|
return m.Article
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type Article struct {
|
||||||
|
Id string `protobuf:"bytes,1,opt,name=id" json:"id,omitempty"`
|
||||||
|
Name string `protobuf:"bytes,2,opt,name=name" json:"name,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Article) Reset() { *m = Article{} }
|
||||||
|
func (m *Article) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*Article) ProtoMessage() {}
|
||||||
|
func (*Article) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} }
|
||||||
|
|
||||||
|
func (m *Article) GetId() string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Id
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Article) GetName() string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Name
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
proto.RegisterType((*GetArticleRequest)(nil), "article.GetArticleRequest")
|
||||||
|
proto.RegisterType((*GetArticleResponse)(nil), "article.GetArticleResponse")
|
||||||
|
proto.RegisterType((*Article)(nil), "article.Article")
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() { proto.RegisterFile("proto/article.proto", fileDescriptor0) }
|
||||||
|
|
||||||
|
var fileDescriptor0 = []byte{
|
||||||
|
// 208 bytes of a gzipped FileDescriptorProto
|
||||||
|
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2e, 0x28, 0xca, 0x2f,
|
||||||
|
0xc9, 0xd7, 0x4f, 0x2c, 0x2a, 0xc9, 0x4c, 0xce, 0x49, 0xd5, 0x03, 0xf3, 0x84, 0xd8, 0xa1, 0x5c,
|
||||||
|
0x29, 0x21, 0x88, 0x6c, 0x72, 0x7e, 0x6e, 0x6e, 0x7e, 0x1e, 0x44, 0x52, 0xc9, 0x9d, 0x4b, 0xd0,
|
||||||
|
0x3d, 0xb5, 0xc4, 0x11, 0xa2, 0x22, 0x28, 0xb5, 0xb0, 0x34, 0xb5, 0xb8, 0x44, 0xc8, 0x88, 0x8b,
|
||||||
|
0x2b, 0x3d, 0xb5, 0x04, 0xaa, 0x4d, 0x82, 0x51, 0x81, 0x51, 0x83, 0xdb, 0x48, 0x48, 0x0f, 0xaa,
|
||||||
|
0x0f, 0x49, 0x39, 0x92, 0x2a, 0x25, 0x07, 0x2e, 0x21, 0x64, 0x83, 0x8a, 0x0b, 0xf2, 0xf3, 0x8a,
|
||||||
|
0x53, 0x85, 0xb4, 0xb8, 0xd8, 0x51, 0x8d, 0x11, 0xd0, 0x83, 0x39, 0x0e, 0xa6, 0x14, 0xa6, 0x40,
|
||||||
|
0x49, 0x97, 0x8b, 0x1d, 0x2a, 0x26, 0xc4, 0xc7, 0xc5, 0x94, 0x99, 0x02, 0xd6, 0xc1, 0x19, 0xc4,
|
||||||
|
0x94, 0x99, 0x22, 0x24, 0xc4, 0xc5, 0x92, 0x97, 0x98, 0x9b, 0x2a, 0xc1, 0x04, 0x16, 0x01, 0xb3,
|
||||||
|
0x8d, 0x42, 0xb9, 0xb8, 0xa0, 0x3a, 0x8b, 0xcb, 0x92, 0x85, 0xdc, 0xb9, 0xb8, 0x10, 0xd6, 0x0b,
|
||||||
|
0x49, 0xc1, 0x6d, 0xc1, 0xf0, 0x9c, 0x94, 0x34, 0x56, 0x39, 0x88, 0x7b, 0x95, 0x18, 0x9c, 0x24,
|
||||||
|
0xa2, 0xc4, 0x72, 0xf3, 0x53, 0x52, 0x73, 0x8a, 0x61, 0xa1, 0x68, 0x0d, 0xa5, 0x93, 0xd8, 0xc0,
|
||||||
|
0x21, 0x66, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0x16, 0x18, 0x87, 0xc4, 0x65, 0x01, 0x00, 0x00,
|
||||||
|
}
|
70
examples/import/output/models/common/common.pb.go
Normal file
70
examples/import/output/models/common/common.pb.go
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
// Code generated by protoc-gen-go.
|
||||||
|
// source: proto/common.proto
|
||||||
|
// DO NOT EDIT!
|
||||||
|
|
||||||
|
/*
|
||||||
|
Package common is a generated protocol buffer package.
|
||||||
|
|
||||||
|
It is generated from these files:
|
||||||
|
proto/common.proto
|
||||||
|
|
||||||
|
It has these top-level messages:
|
||||||
|
GetArticle
|
||||||
|
*/
|
||||||
|
package common
|
||||||
|
|
||||||
|
import proto "github.com/golang/protobuf/proto"
|
||||||
|
import fmt "fmt"
|
||||||
|
import math "math"
|
||||||
|
|
||||||
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
|
var _ = proto.Marshal
|
||||||
|
var _ = fmt.Errorf
|
||||||
|
var _ = math.Inf
|
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the proto package it is being compiled against.
|
||||||
|
// A compilation error at this line likely means your copy of the
|
||||||
|
// proto package needs to be updated.
|
||||||
|
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||||
|
|
||||||
|
type GetArticle struct {
|
||||||
|
Id string `protobuf:"bytes,1,opt,name=id" json:"id,omitempty"`
|
||||||
|
Tenant string `protobuf:"bytes,2,opt,name=tenant" json:"tenant,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *GetArticle) Reset() { *m = GetArticle{} }
|
||||||
|
func (m *GetArticle) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*GetArticle) ProtoMessage() {}
|
||||||
|
func (*GetArticle) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
|
||||||
|
|
||||||
|
func (m *GetArticle) GetId() string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Id
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *GetArticle) GetTenant() string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Tenant
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
proto.RegisterType((*GetArticle)(nil), "common.GetArticle")
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() { proto.RegisterFile("proto/common.proto", fileDescriptor0) }
|
||||||
|
|
||||||
|
var fileDescriptor0 = []byte{
|
||||||
|
// 110 bytes of a gzipped FileDescriptorProto
|
||||||
|
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2a, 0x28, 0xca, 0x2f,
|
||||||
|
0xc9, 0xd7, 0x4f, 0xce, 0xcf, 0xcd, 0xcd, 0xcf, 0xd3, 0x03, 0x73, 0x84, 0xd8, 0x20, 0x3c, 0x25,
|
||||||
|
0x13, 0x2e, 0x2e, 0xf7, 0xd4, 0x12, 0xc7, 0xa2, 0x92, 0xcc, 0xe4, 0x9c, 0x54, 0x21, 0x3e, 0x2e,
|
||||||
|
0xa6, 0xcc, 0x14, 0x09, 0x46, 0x05, 0x46, 0x0d, 0xce, 0x20, 0xa6, 0xcc, 0x14, 0x21, 0x31, 0x2e,
|
||||||
|
0xb6, 0x92, 0xd4, 0xbc, 0xc4, 0xbc, 0x12, 0x09, 0x26, 0xb0, 0x18, 0x94, 0xe7, 0x24, 0x16, 0x25,
|
||||||
|
0x92, 0x9b, 0x9f, 0x92, 0x9a, 0x53, 0x0c, 0x35, 0xd4, 0x1a, 0x42, 0x25, 0xb1, 0x81, 0x0d, 0x37,
|
||||||
|
0x06, 0x04, 0x00, 0x00, 0xff, 0xff, 0x89, 0x5a, 0x1d, 0x73, 0x72, 0x00, 0x00, 0x00,
|
||||||
|
}
|
11
examples/import/output/output.go
Normal file
11
examples/import/output/output.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
// Code generated by protoc-gen-gotemplate
|
||||||
|
package article
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/moul/protoc-gen-gotemplate/examples/import/output/models/article"
|
||||||
|
"github.com/moul/protoc-gen-gotemplate/examples/import/output/models/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Repository interface {
|
||||||
|
GetArticle(getarticle *common.GetArticle ) (*article.Article, error)
|
||||||
|
}
|
19
examples/import/proto/article.proto
Normal file
19
examples/import/proto/article.proto
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package article;
|
||||||
|
|
||||||
|
option go_package = "models/article;article";
|
||||||
|
|
||||||
|
import "proto/common.proto";
|
||||||
|
|
||||||
|
message GetArticleRequest { common.GetArticle getarticle = 1;}
|
||||||
|
message GetArticleResponse { Article article = 1;}
|
||||||
|
|
||||||
|
message Article{
|
||||||
|
string id = 1;
|
||||||
|
string name = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
service articlesvc {
|
||||||
|
rpc GetArticle (GetArticleRequest) returns (GetArticleResponse){}
|
||||||
|
}
|
10
examples/import/proto/common.proto
Normal file
10
examples/import/proto/common.proto
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package common;
|
||||||
|
|
||||||
|
option go_package = "models/common;common";
|
||||||
|
|
||||||
|
message GetArticle{
|
||||||
|
string id = 1;
|
||||||
|
string tenant = 2;
|
||||||
|
}
|
11
examples/import/templates/output.go.tmpl
Normal file
11
examples/import/templates/output.go.tmpl
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
// Code generated by protoc-gen-gotemplate
|
||||||
|
package {{.File.Package}}
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/moul/protoc-gen-gotemplate/examples/import/output/models/article"
|
||||||
|
"github.com/moul/protoc-gen-gotemplate/examples/import/output/models/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Repository interface {
|
||||||
|
{{range $m := .Service.Method}}{{with $t := $m.InputType | getMessageType $.File}} {{$m.Name}}({{range $f := $t.Field}}{{$f.Name|lowerCamelCase}} {{$f| goTypeWithPackage }} {{end}}) ({{with $out := $m.OutputType | getMessageType $.File}}{{range $f := $out.Field}}{{$f | goTypeWithPackage}}, {{end}}{{end}} error){{end}}{{end}}
|
||||||
|
}
|
@@ -1,146 +0,0 @@
|
|||||||
// GENERATED CODE -- DO NOT EDIT!
|
|
||||||
|
|
||||||
'use strict';
|
|
||||||
var grpc = require('grpc');
|
|
||||||
var test_pb = require('./test_pb.js');
|
|
||||||
|
|
||||||
|
|
||||||
function serialize_test_TestNoStreamRequest(arg) {
|
|
||||||
if (!(arg instanceof test_pb.TestNoStreamRequest)) {
|
|
||||||
throw new Error('Expected argument of type test.TestNoStreamRequest');
|
|
||||||
}
|
|
||||||
return new Buffer(arg.serializeBinary());
|
|
||||||
}
|
|
||||||
|
|
||||||
function deserialize_test_TestNoStreamRequest(buffer_arg) {
|
|
||||||
return test_pb.TestNoStreamRequest.deserializeBinary(new Uint8Array(buffer_arg));
|
|
||||||
}
|
|
||||||
|
|
||||||
function serialize_test_TestNoStreamReply(arg) {
|
|
||||||
if (!(arg instanceof test_pb.TestNoStreamReply)) {
|
|
||||||
throw new Error('Expected argument of type test.TestNoStreamReply');
|
|
||||||
}
|
|
||||||
return new Buffer(arg.serializeBinary());
|
|
||||||
}
|
|
||||||
|
|
||||||
function deserialize_test_TestNoStreamReply(buffer_arg) {
|
|
||||||
return test_pb.TestNoStreamReply.deserializeBinary(new Uint8Array(buffer_arg));
|
|
||||||
}
|
|
||||||
|
|
||||||
function serialize_test_TestStreamRequestRequest(arg) {
|
|
||||||
if (!(arg instanceof test_pb.TestStreamRequestRequest)) {
|
|
||||||
throw new Error('Expected argument of type test.TestStreamRequestRequest');
|
|
||||||
}
|
|
||||||
return new Buffer(arg.serializeBinary());
|
|
||||||
}
|
|
||||||
|
|
||||||
function deserialize_test_TestStreamRequestRequest(buffer_arg) {
|
|
||||||
return test_pb.TestStreamRequestRequest.deserializeBinary(new Uint8Array(buffer_arg));
|
|
||||||
}
|
|
||||||
|
|
||||||
function serialize_test_TestStreamRequestReply(arg) {
|
|
||||||
if (!(arg instanceof test_pb.TestStreamRequestReply)) {
|
|
||||||
throw new Error('Expected argument of type test.TestStreamRequestReply');
|
|
||||||
}
|
|
||||||
return new Buffer(arg.serializeBinary());
|
|
||||||
}
|
|
||||||
|
|
||||||
function deserialize_test_TestStreamRequestReply(buffer_arg) {
|
|
||||||
return test_pb.TestStreamRequestReply.deserializeBinary(new Uint8Array(buffer_arg));
|
|
||||||
}
|
|
||||||
|
|
||||||
function serialize_test_TestStreamReplyRequest(arg) {
|
|
||||||
if (!(arg instanceof test_pb.TestStreamReplyRequest)) {
|
|
||||||
throw new Error('Expected argument of type test.TestStreamReplyRequest');
|
|
||||||
}
|
|
||||||
return new Buffer(arg.serializeBinary());
|
|
||||||
}
|
|
||||||
|
|
||||||
function deserialize_test_TestStreamReplyRequest(buffer_arg) {
|
|
||||||
return test_pb.TestStreamReplyRequest.deserializeBinary(new Uint8Array(buffer_arg));
|
|
||||||
}
|
|
||||||
|
|
||||||
function serialize_test_TestStreamReplyReply(arg) {
|
|
||||||
if (!(arg instanceof test_pb.TestStreamReplyReply)) {
|
|
||||||
throw new Error('Expected argument of type test.TestStreamReplyReply');
|
|
||||||
}
|
|
||||||
return new Buffer(arg.serializeBinary());
|
|
||||||
}
|
|
||||||
|
|
||||||
function deserialize_test_TestStreamReplyReply(buffer_arg) {
|
|
||||||
return test_pb.TestStreamReplyReply.deserializeBinary(new Uint8Array(buffer_arg));
|
|
||||||
}
|
|
||||||
|
|
||||||
function serialize_test_TestStreamBothRequest(arg) {
|
|
||||||
if (!(arg instanceof test_pb.TestStreamBothRequest)) {
|
|
||||||
throw new Error('Expected argument of type test.TestStreamBothRequest');
|
|
||||||
}
|
|
||||||
return new Buffer(arg.serializeBinary());
|
|
||||||
}
|
|
||||||
|
|
||||||
function deserialize_test_TestStreamBothRequest(buffer_arg) {
|
|
||||||
return test_pb.TestStreamBothRequest.deserializeBinary(new Uint8Array(buffer_arg));
|
|
||||||
}
|
|
||||||
|
|
||||||
function serialize_test_TestStreamBothReply(arg) {
|
|
||||||
if (!(arg instanceof test_pb.TestStreamBothReply)) {
|
|
||||||
throw new Error('Expected argument of type test.TestStreamBothReply');
|
|
||||||
}
|
|
||||||
return new Buffer(arg.serializeBinary());
|
|
||||||
}
|
|
||||||
|
|
||||||
function deserialize_test_TestStreamBothReply(buffer_arg) {
|
|
||||||
return test_pb.TestStreamBothReply.deserializeBinary(new Uint8Array(buffer_arg));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
var TestServiceService = exports.TestServiceService = {
|
|
||||||
|
|
||||||
testNoStream: {
|
|
||||||
path: '/test.TestService/TestNoStream',
|
|
||||||
requestStream: false,
|
|
||||||
responseStream: false,
|
|
||||||
requestType: test_pb.TestNoStreamRequest,
|
|
||||||
responseType: test_pb.TestNoStreamReply,
|
|
||||||
requestSerialize: serialize_test_TestNoStreamRequest,
|
|
||||||
requestDeserialize: deserialize_test_TestNoStreamRequest,
|
|
||||||
responseSerialize: serialize_test_TestNoStreamReply,
|
|
||||||
responseDeserialize: deserialize_test_TestNoStreamReply,
|
|
||||||
},
|
|
||||||
testStreamRequest: {
|
|
||||||
path: '/test.TestService/TestStreamRequest',
|
|
||||||
requestStream: true,
|
|
||||||
responseStream: false,
|
|
||||||
requestType: test_pb.TestStreamRequestRequest,
|
|
||||||
responseType: test_pb.TestStreamRequestReply,
|
|
||||||
requestSerialize: serialize_test_TestStreamRequestRequest,
|
|
||||||
requestDeserialize: deserialize_test_TestStreamRequestRequest,
|
|
||||||
responseSerialize: serialize_test_TestStreamRequestReply,
|
|
||||||
responseDeserialize: deserialize_test_TestStreamRequestReply,
|
|
||||||
},
|
|
||||||
testStreamReply: {
|
|
||||||
path: '/test.TestService/TestStreamReply',
|
|
||||||
requestStream: false,
|
|
||||||
responseStream: true,
|
|
||||||
requestType: test_pb.TestStreamReplyRequest,
|
|
||||||
responseType: test_pb.TestStreamReplyReply,
|
|
||||||
requestSerialize: serialize_test_TestStreamReplyRequest,
|
|
||||||
requestDeserialize: deserialize_test_TestStreamReplyRequest,
|
|
||||||
responseSerialize: serialize_test_TestStreamReplyReply,
|
|
||||||
responseDeserialize: deserialize_test_TestStreamReplyReply,
|
|
||||||
},
|
|
||||||
testStreamBoth: {
|
|
||||||
path: '/test.TestService/TestStreamBoth',
|
|
||||||
requestStream: true,
|
|
||||||
responseStream: true,
|
|
||||||
requestType: test_pb.TestStreamBothRequest,
|
|
||||||
responseType: test_pb.TestStreamBothReply,
|
|
||||||
requestSerialize: serialize_test_TestStreamBothRequest,
|
|
||||||
requestDeserialize: deserialize_test_TestStreamBothRequest,
|
|
||||||
responseSerialize: serialize_test_TestStreamBothReply,
|
|
||||||
responseDeserialize: deserialize_test_TestStreamBothReply,
|
|
||||||
},
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.TestServiceClient = grpc.makeGenericClientConstructor(TestServiceService);
|
|
@@ -1,47 +0,0 @@
|
|||||||
// GENERATED CODE -- DO NOT EDIT!
|
|
||||||
{{$Package:=.File.Package}}
|
|
||||||
'use strict';
|
|
||||||
var grpc = require('grpc');
|
|
||||||
var {{$Package}}_pb = require('./{{$Package}}_pb.js');
|
|
||||||
|
|
||||||
{{range .File.Service}}{{range .Method}}
|
|
||||||
function serialize_{{$Package}}_{{.InputType | splitArray "." | last}}(arg) {
|
|
||||||
if (!(arg instanceof {{$Package}}_pb.{{.InputType | splitArray "." | last}})) {
|
|
||||||
throw new Error('Expected argument of type {{$Package}}.{{.InputType | splitArray "." | last}}');
|
|
||||||
}
|
|
||||||
return new Buffer(arg.serializeBinary());
|
|
||||||
}
|
|
||||||
|
|
||||||
function deserialize_{{$Package}}_{{.InputType | splitArray "." | last}}(buffer_arg) {
|
|
||||||
return {{$Package}}_pb.{{.InputType | splitArray "." | last}}.deserializeBinary(new Uint8Array(buffer_arg));
|
|
||||||
}
|
|
||||||
|
|
||||||
function serialize_{{$Package}}_{{.OutputType | splitArray "." | last}}(arg) {
|
|
||||||
if (!(arg instanceof {{$Package}}_pb.{{.OutputType | splitArray "." | last}})) {
|
|
||||||
throw new Error('Expected argument of type {{$Package}}.{{.OutputType | splitArray "." | last}}');
|
|
||||||
}
|
|
||||||
return new Buffer(arg.serializeBinary());
|
|
||||||
}
|
|
||||||
|
|
||||||
function deserialize_{{$Package}}_{{.OutputType | splitArray "." | last}}(buffer_arg) {
|
|
||||||
return {{$Package}}_pb.{{.OutputType | splitArray "." | last}}.deserializeBinary(new Uint8Array(buffer_arg));
|
|
||||||
}
|
|
||||||
{{end}}{{end}}
|
|
||||||
{{range .File.Service}}
|
|
||||||
var {{.Name}}Service = exports.{{.Name}}Service = {
|
|
||||||
{{$serviceName:=.Name}}
|
|
||||||
{{range .Method}}{{.Name | lowerCamelCase}}: {
|
|
||||||
path: '/{{$Package}}.{{$serviceName}}/{{.Name}}',
|
|
||||||
requestStream: {{.ClientStreaming | default "false"}},
|
|
||||||
responseStream: {{.ServerStreaming | default "false"}},
|
|
||||||
requestType: {{$Package}}_pb.{{.InputType | splitArray "." | last}},
|
|
||||||
responseType: {{$Package}}_pb.{{.OutputType | splitArray "." | last}},
|
|
||||||
requestSerialize: serialize_{{$Package}}_{{.InputType | splitArray "." | last}},
|
|
||||||
requestDeserialize: deserialize_{{$Package}}_{{.InputType | splitArray "." | last}},
|
|
||||||
responseSerialize: serialize_{{$Package}}_{{.OutputType | splitArray "." | last}},
|
|
||||||
responseDeserialize: deserialize_{{$Package}}_{{.OutputType | splitArray "." | last}},
|
|
||||||
},
|
|
||||||
{{end}}
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.{{.Name}}Client = grpc.makeGenericClientConstructor({{.Name}}Service);{{end}}
|
|
@@ -1,44 +0,0 @@
|
|||||||
// GENERATED CODE -- DO NOT EDIT!
|
|
||||||
{{$Package:=.File.Package}}
|
|
||||||
'use strict';
|
|
||||||
var grpc = require('grpc');
|
|
||||||
var {{$Package}}_pb = require('./{{$Package}}_pb.js');
|
|
||||||
{{range .File.Service}}{{range .Method}}
|
|
||||||
function serialize_{{$Package}}_{{.InputType | splitArray "." | last}}(arg) {
|
|
||||||
if (!(arg instanceof {{$Package}}_pb.{{.InputType | splitArray "." | last}})) {
|
|
||||||
throw new Error('Expected argument of type {{$Package}}.{{.InputType | splitArray "." | last}}');
|
|
||||||
}
|
|
||||||
return new Buffer(arg.serializeBinary());
|
|
||||||
}
|
|
||||||
|
|
||||||
function deserialize_{{$Package}}_{{.InputType | splitArray "." | last}}(buffer_arg) {
|
|
||||||
return {{$Package}}_pb.{{.InputType | splitArray "." | last}}.deserializeBinary(new Uint8Array(buffer_arg));
|
|
||||||
}
|
|
||||||
|
|
||||||
function serialize_{{$Package}}_{{.OutputType | splitArray "." | last}}(arg) {
|
|
||||||
if (!(arg instanceof {{$Package}}_pb.{{.OutputType | splitArray "." | last}})) {
|
|
||||||
throw new Error('Expected argument of type {{$Package}}.{{.OutputType | splitArray "." | last}}');
|
|
||||||
}
|
|
||||||
return new Buffer(arg.serializeBinary());
|
|
||||||
}
|
|
||||||
|
|
||||||
function deserialize_{{$Package}}_{{.OutputType | splitArray "." | last}}(buffer_arg) {
|
|
||||||
return {{$Package}}_pb.{{.OutputType | splitArray "." | last}}.deserializeBinary(new Uint8Array(buffer_arg));
|
|
||||||
}
|
|
||||||
{{end}}{{end}}
|
|
||||||
{{range .File.Service}}
|
|
||||||
var {{.Name}}Service = exports.{{.Name}}Service = {
|
|
||||||
{{$serviceName:=.Name}}{{range .Method}}{{.Name | lowerCamelCase}}: {
|
|
||||||
path: '/{{$Package}}.{{$serviceName}}/{{.Name}}',
|
|
||||||
requestStream: {{.ClientStreaming | default "false"}},
|
|
||||||
responseStream: {{.ServerStreaming | default "false"}},
|
|
||||||
requestType: {{$Package}}_pb.{{.InputType | splitArray "." | last}},
|
|
||||||
responseType: {{$Package}}_pb.{{.OutputType | splitArray "." | last}},
|
|
||||||
requestSerialize: serialize_{{$Package}}_{{.InputType | splitArray "." | last}},
|
|
||||||
requestDeserialize: deserialize_{{$Package}}_{{.InputType | splitArray "." | last}},
|
|
||||||
responseSerialize: serialize_{{$Package}}_{{.OutputType | splitArray "." | last}},
|
|
||||||
responseDeserialize: deserialize_{{$Package}}_{{.OutputType | splitArray "." | last}},
|
|
||||||
},{{end}}
|
|
||||||
}
|
|
||||||
|
|
||||||
exports.{{.Name}}Client = grpc.makeGenericClientConstructor({{.Name}}Service);{{end}}
|
|
26
examples/single-package-mode/Makefile
Normal file
26
examples/single-package-mode/Makefile
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
.PHONY: re
|
||||||
|
re: clean build test
|
||||||
|
|
||||||
|
.PHONY: build
|
||||||
|
build:
|
||||||
|
@mkdir -p output
|
||||||
|
|
||||||
|
@# proto-gen-go
|
||||||
|
protoc -I./proto --go_out=plugins=grpc:output proto/aaa/aaa.proto
|
||||||
|
protoc -I./proto --go_out=plugins=grpc:output proto/bbb/bbb.proto
|
||||||
|
@rm -rf output/aaa output/bbb
|
||||||
|
@mv output/github.com/moul/protoc-gen-gotemplate/examples/single-package-mode/output/* output/
|
||||||
|
@rm -rf output/github.com
|
||||||
|
|
||||||
|
@# protoc-gen-gotemplate
|
||||||
|
protoc -I./proto --gotemplate_out=template_dir=templates,single-package-mode=true:output proto/bbb/bbb.proto
|
||||||
|
gofmt -w .
|
||||||
|
|
||||||
|
.PHONY: test
|
||||||
|
test:
|
||||||
|
go test -i ./output/...
|
||||||
|
go test -v ./output/...
|
||||||
|
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
rm -rf output
|
84
examples/single-package-mode/output/aaa/aaa.pb.go
Normal file
84
examples/single-package-mode/output/aaa/aaa.pb.go
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
// Code generated by protoc-gen-go.
|
||||||
|
// source: aaa/aaa.proto
|
||||||
|
// DO NOT EDIT!
|
||||||
|
|
||||||
|
/*
|
||||||
|
Package aaa is a generated protocol buffer package.
|
||||||
|
|
||||||
|
It is generated from these files:
|
||||||
|
aaa/aaa.proto
|
||||||
|
|
||||||
|
It has these top-level messages:
|
||||||
|
AaaRequest
|
||||||
|
AaaReply
|
||||||
|
*/
|
||||||
|
package aaa
|
||||||
|
|
||||||
|
import proto "github.com/golang/protobuf/proto"
|
||||||
|
import fmt "fmt"
|
||||||
|
import math "math"
|
||||||
|
|
||||||
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
|
var _ = proto.Marshal
|
||||||
|
var _ = fmt.Errorf
|
||||||
|
var _ = math.Inf
|
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the proto package it is being compiled against.
|
||||||
|
// A compilation error at this line likely means your copy of the
|
||||||
|
// proto package needs to be updated.
|
||||||
|
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||||
|
|
||||||
|
type AaaRequest struct {
|
||||||
|
Blah string `protobuf:"bytes,1,opt,name=blah" json:"blah,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *AaaRequest) Reset() { *m = AaaRequest{} }
|
||||||
|
func (m *AaaRequest) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*AaaRequest) ProtoMessage() {}
|
||||||
|
func (*AaaRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
|
||||||
|
|
||||||
|
func (m *AaaRequest) GetBlah() string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Blah
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
type AaaReply struct {
|
||||||
|
Error string `protobuf:"bytes,1,opt,name=error" json:"error,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *AaaReply) Reset() { *m = AaaReply{} }
|
||||||
|
func (m *AaaReply) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*AaaReply) ProtoMessage() {}
|
||||||
|
func (*AaaReply) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
|
||||||
|
|
||||||
|
func (m *AaaReply) GetError() string {
|
||||||
|
if m != nil {
|
||||||
|
return m.Error
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
proto.RegisterType((*AaaRequest)(nil), "the.aaa.package.AaaRequest")
|
||||||
|
proto.RegisterType((*AaaReply)(nil), "the.aaa.package.AaaReply")
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() { proto.RegisterFile("aaa/aaa.proto", fileDescriptor0) }
|
||||||
|
|
||||||
|
var fileDescriptor0 = []byte{
|
||||||
|
// 172 bytes of a gzipped FileDescriptorProto
|
||||||
|
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x2c, 0x8e, 0x31, 0x0b, 0xc2, 0x30,
|
||||||
|
0x10, 0x46, 0x29, 0xa8, 0x68, 0x40, 0x84, 0xe0, 0xe0, 0x58, 0x3a, 0xb9, 0xa4, 0x19, 0xfc, 0x05,
|
||||||
|
0xba, 0x8b, 0xd0, 0xd1, 0xed, 0x5a, 0x8f, 0xb4, 0x78, 0xe9, 0xc5, 0xf4, 0x02, 0xfa, 0xef, 0xc5,
|
||||||
|
0xd8, 0xed, 0x1e, 0xbc, 0xe3, 0x7b, 0x6a, 0x0b, 0x00, 0x16, 0x00, 0xea, 0x10, 0x59, 0x58, 0xef,
|
||||||
|
0xa4, 0xc7, 0x3a, 0x23, 0x74, 0x4f, 0x70, 0x58, 0x95, 0x4a, 0x9d, 0x01, 0x1a, 0x7c, 0x25, 0x9c,
|
||||||
|
0x44, 0x6b, 0xb5, 0x68, 0x09, 0xfa, 0x43, 0x51, 0x16, 0xc7, 0x4d, 0x93, 0xef, 0xaa, 0x54, 0xeb,
|
||||||
|
0x6c, 0x04, 0xfa, 0xe8, 0xbd, 0x5a, 0x62, 0x8c, 0x1c, 0x67, 0xe1, 0x0f, 0x97, 0xdb, 0xfd, 0xea,
|
||||||
|
0x06, 0xe9, 0x53, 0x5b, 0x77, 0xec, 0xad, 0xe7, 0x44, 0x36, 0xaf, 0x75, 0xc6, 0xe1, 0x68, 0x1c,
|
||||||
|
0x0b, 0xfa, 0x40, 0x20, 0x68, 0xf1, 0x0d, 0x3e, 0x10, 0x4e, 0x76, 0x1a, 0x46, 0x47, 0x68, 0xe6,
|
||||||
|
0x08, 0xe3, 0xf9, 0x81, 0x96, 0x93, 0x84, 0x24, 0xbf, 0xd6, 0x76, 0x95, 0xdf, 0x4f, 0xdf, 0x00,
|
||||||
|
0x00, 0x00, 0xff, 0xff, 0x93, 0x24, 0x48, 0x9c, 0xbd, 0x00, 0x00, 0x00,
|
||||||
|
}
|
200
examples/single-package-mode/output/bbb/bbb.pb.go
Normal file
200
examples/single-package-mode/output/bbb/bbb.pb.go
Normal file
@@ -0,0 +1,200 @@
|
|||||||
|
// Code generated by protoc-gen-go.
|
||||||
|
// source: bbb/bbb.proto
|
||||||
|
// DO NOT EDIT!
|
||||||
|
|
||||||
|
/*
|
||||||
|
Package bbb is a generated protocol buffer package.
|
||||||
|
|
||||||
|
It is generated from these files:
|
||||||
|
bbb/bbb.proto
|
||||||
|
|
||||||
|
It has these top-level messages:
|
||||||
|
BbbRequest
|
||||||
|
BbbReply
|
||||||
|
*/
|
||||||
|
package bbb
|
||||||
|
|
||||||
|
import proto "github.com/golang/protobuf/proto"
|
||||||
|
import fmt "fmt"
|
||||||
|
import math "math"
|
||||||
|
import the_aaa_package "github.com/moul/protoc-gen-gotemplate/examples/single-package-mode/output/aaa"
|
||||||
|
|
||||||
|
import (
|
||||||
|
context "golang.org/x/net/context"
|
||||||
|
grpc "google.golang.org/grpc"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
|
var _ = proto.Marshal
|
||||||
|
var _ = fmt.Errorf
|
||||||
|
var _ = math.Inf
|
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the proto package it is being compiled against.
|
||||||
|
// A compilation error at this line likely means your copy of the
|
||||||
|
// proto package needs to be updated.
|
||||||
|
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||||
|
|
||||||
|
type BbbRequest struct {
|
||||||
|
Enable bool `protobuf:"varint,1,opt,name=enable" json:"enable,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *BbbRequest) Reset() { *m = BbbRequest{} }
|
||||||
|
func (m *BbbRequest) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*BbbRequest) ProtoMessage() {}
|
||||||
|
func (*BbbRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
|
||||||
|
|
||||||
|
func (m *BbbRequest) GetEnable() bool {
|
||||||
|
if m != nil {
|
||||||
|
return m.Enable
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
type BbbReply struct {
|
||||||
|
Done bool `protobuf:"varint,1,opt,name=done" json:"done,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *BbbReply) Reset() { *m = BbbReply{} }
|
||||||
|
func (m *BbbReply) String() string { return proto.CompactTextString(m) }
|
||||||
|
func (*BbbReply) ProtoMessage() {}
|
||||||
|
func (*BbbReply) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
|
||||||
|
|
||||||
|
func (m *BbbReply) GetDone() bool {
|
||||||
|
if m != nil {
|
||||||
|
return m.Done
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
proto.RegisterType((*BbbRequest)(nil), "the.bbb.package.BbbRequest")
|
||||||
|
proto.RegisterType((*BbbReply)(nil), "the.bbb.package.BbbReply")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
|
var _ context.Context
|
||||||
|
var _ grpc.ClientConn
|
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the grpc package it is being compiled against.
|
||||||
|
const _ = grpc.SupportPackageIsVersion4
|
||||||
|
|
||||||
|
// Client API for BbbService service
|
||||||
|
|
||||||
|
type BbbServiceClient interface {
|
||||||
|
Aaa(ctx context.Context, in *the_aaa_package.AaaRequest, opts ...grpc.CallOption) (*the_aaa_package.AaaReply, error)
|
||||||
|
Bbb(ctx context.Context, in *BbbRequest, opts ...grpc.CallOption) (*BbbReply, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type bbbServiceClient struct {
|
||||||
|
cc *grpc.ClientConn
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewBbbServiceClient(cc *grpc.ClientConn) BbbServiceClient {
|
||||||
|
return &bbbServiceClient{cc}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *bbbServiceClient) Aaa(ctx context.Context, in *the_aaa_package.AaaRequest, opts ...grpc.CallOption) (*the_aaa_package.AaaReply, error) {
|
||||||
|
out := new(the_aaa_package.AaaReply)
|
||||||
|
err := grpc.Invoke(ctx, "/the.bbb.package.BbbService/Aaa", in, out, c.cc, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *bbbServiceClient) Bbb(ctx context.Context, in *BbbRequest, opts ...grpc.CallOption) (*BbbReply, error) {
|
||||||
|
out := new(BbbReply)
|
||||||
|
err := grpc.Invoke(ctx, "/the.bbb.package.BbbService/Bbb", in, out, c.cc, opts...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Server API for BbbService service
|
||||||
|
|
||||||
|
type BbbServiceServer interface {
|
||||||
|
Aaa(context.Context, *the_aaa_package.AaaRequest) (*the_aaa_package.AaaReply, error)
|
||||||
|
Bbb(context.Context, *BbbRequest) (*BbbReply, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterBbbServiceServer(s *grpc.Server, srv BbbServiceServer) {
|
||||||
|
s.RegisterService(&_BbbService_serviceDesc, srv)
|
||||||
|
}
|
||||||
|
|
||||||
|
func _BbbService_Aaa_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||||
|
in := new(the_aaa_package.AaaRequest)
|
||||||
|
if err := dec(in); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if interceptor == nil {
|
||||||
|
return srv.(BbbServiceServer).Aaa(ctx, in)
|
||||||
|
}
|
||||||
|
info := &grpc.UnaryServerInfo{
|
||||||
|
Server: srv,
|
||||||
|
FullMethod: "/the.bbb.package.BbbService/Aaa",
|
||||||
|
}
|
||||||
|
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||||
|
return srv.(BbbServiceServer).Aaa(ctx, req.(*the_aaa_package.AaaRequest))
|
||||||
|
}
|
||||||
|
return interceptor(ctx, in, info, handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
func _BbbService_Bbb_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||||
|
in := new(BbbRequest)
|
||||||
|
if err := dec(in); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if interceptor == nil {
|
||||||
|
return srv.(BbbServiceServer).Bbb(ctx, in)
|
||||||
|
}
|
||||||
|
info := &grpc.UnaryServerInfo{
|
||||||
|
Server: srv,
|
||||||
|
FullMethod: "/the.bbb.package.BbbService/Bbb",
|
||||||
|
}
|
||||||
|
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||||
|
return srv.(BbbServiceServer).Bbb(ctx, req.(*BbbRequest))
|
||||||
|
}
|
||||||
|
return interceptor(ctx, in, info, handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
var _BbbService_serviceDesc = grpc.ServiceDesc{
|
||||||
|
ServiceName: "the.bbb.package.BbbService",
|
||||||
|
HandlerType: (*BbbServiceServer)(nil),
|
||||||
|
Methods: []grpc.MethodDesc{
|
||||||
|
{
|
||||||
|
MethodName: "Aaa",
|
||||||
|
Handler: _BbbService_Aaa_Handler,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
MethodName: "Bbb",
|
||||||
|
Handler: _BbbService_Bbb_Handler,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Streams: []grpc.StreamDesc{},
|
||||||
|
Metadata: "bbb/bbb.proto",
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() { proto.RegisterFile("bbb/bbb.proto", fileDescriptor0) }
|
||||||
|
|
||||||
|
var fileDescriptor0 = []byte{
|
||||||
|
// 242 bytes of a gzipped FileDescriptorProto
|
||||||
|
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x90, 0x41, 0x4b, 0x03, 0x31,
|
||||||
|
0x10, 0x85, 0x59, 0x2a, 0xa5, 0x04, 0x8a, 0x90, 0x83, 0x68, 0x05, 0x91, 0xe2, 0xc1, 0xcb, 0x26,
|
||||||
|
0xa0, 0xe7, 0x1e, 0xba, 0x77, 0x11, 0xea, 0xcd, 0xdb, 0xcc, 0x76, 0x48, 0x17, 0x93, 0x9d, 0xd8,
|
||||||
|
0x9d, 0x88, 0xfd, 0x0b, 0xfe, 0x6a, 0xd9, 0x34, 0xa2, 0x60, 0x6f, 0x19, 0xde, 0xbc, 0xf7, 0xbd,
|
||||||
|
0x8c, 0x9a, 0x23, 0xa2, 0x45, 0x44, 0x13, 0xf7, 0x2c, 0xac, 0xcf, 0x65, 0x47, 0x26, 0x8f, 0xd0,
|
||||||
|
0xbe, 0x81, 0xa3, 0xc5, 0x1c, 0x00, 0x2c, 0x00, 0x1c, 0xf5, 0xe5, 0x9d, 0x52, 0x0d, 0xe2, 0x86,
|
||||||
|
0xde, 0x13, 0x0d, 0xa2, 0x2f, 0xd4, 0x94, 0x7a, 0x40, 0x4f, 0x97, 0xd5, 0x6d, 0x75, 0x3f, 0xdb,
|
||||||
|
0x94, 0x69, 0x79, 0xa3, 0x66, 0x79, 0x2b, 0xfa, 0x83, 0xd6, 0xea, 0x6c, 0xcb, 0xfd, 0xcf, 0x46,
|
||||||
|
0x7e, 0x3f, 0x7c, 0x55, 0x39, 0xe6, 0x85, 0xf6, 0x1f, 0x5d, 0x4b, 0x7a, 0xa5, 0x26, 0x6b, 0x00,
|
||||||
|
0x7d, 0x6d, 0x46, 0x78, 0x66, 0x1d, 0xe1, 0x66, 0x0d, 0x50, 0x50, 0x8b, 0xab, 0xd3, 0xe2, 0x48,
|
||||||
|
0x58, 0xa9, 0x49, 0x83, 0x58, 0xec, 0x7f, 0xba, 0x9b, 0xdf, 0xa6, 0xc5, 0xfe, 0x4f, 0x8c, 0xfe,
|
||||||
|
0xd0, 0x3c, 0xbf, 0x3e, 0xb9, 0x4e, 0x76, 0x09, 0x4d, 0xcb, 0xc1, 0x06, 0x4e, 0xde, 0xe6, 0xbf,
|
||||||
|
0xb6, 0xb5, 0xa3, 0xbe, 0x76, 0x2c, 0x14, 0xa2, 0x07, 0x21, 0x4b, 0x9f, 0x10, 0xa2, 0xa7, 0xc1,
|
||||||
|
0x0e, 0x5d, 0xef, 0x3c, 0xd5, 0x25, 0xa9, 0x0e, 0xbc, 0x25, 0xcb, 0x49, 0x62, 0x92, 0xf1, 0x92,
|
||||||
|
0x38, 0xcd, 0xf6, 0xc7, 0xef, 0x00, 0x00, 0x00, 0xff, 0xff, 0xe0, 0xe1, 0x26, 0xcb, 0x5b, 0x01,
|
||||||
|
0x00, 0x00,
|
||||||
|
}
|
19
examples/single-package-mode/output/bbb/service.go
Normal file
19
examples/single-package-mode/output/bbb/service.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
// file generated with protoc-gen-gotemplate
|
||||||
|
package bbb
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/moul/protoc-gen-gotemplate/examples/single-package-mode/output/aaa"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Service struct{}
|
||||||
|
|
||||||
|
func (service Service) Aaa(ctx context.Context, input *aaa.AaaRequest) (*aaa.AaaReply, error) {
|
||||||
|
return nil, fmt.Errorf("method not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (service Service) Bbb(ctx context.Context, input *BbbRequest) (*BbbReply, error) {
|
||||||
|
return nil, fmt.Errorf("method not implemented")
|
||||||
|
}
|
12
examples/single-package-mode/proto/aaa/aaa.proto
Normal file
12
examples/single-package-mode/proto/aaa/aaa.proto
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
option go_package = "github.com/moul/protoc-gen-gotemplate/examples/single-package-mode/output/aaa";
|
||||||
|
|
||||||
|
package the.aaa.package;
|
||||||
|
|
||||||
|
message AaaRequest {
|
||||||
|
string blah = 1;
|
||||||
|
}
|
||||||
|
message AaaReply {
|
||||||
|
string error = 1;
|
||||||
|
}
|
19
examples/single-package-mode/proto/bbb/bbb.proto
Normal file
19
examples/single-package-mode/proto/bbb/bbb.proto
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package the.bbb.package;
|
||||||
|
|
||||||
|
option go_package = "github.com/moul/protoc-gen-gotemplate/examples/single-package-mode/output/bbb";
|
||||||
|
|
||||||
|
import "aaa/aaa.proto";
|
||||||
|
|
||||||
|
service BbbService {
|
||||||
|
rpc Aaa(the.aaa.package.AaaRequest) returns (the.aaa.package.AaaReply);
|
||||||
|
rpc Bbb(BbbRequest) returns (BbbReply);
|
||||||
|
}
|
||||||
|
|
||||||
|
message BbbRequest {
|
||||||
|
bool enable = 1;
|
||||||
|
}
|
||||||
|
message BbbReply {
|
||||||
|
bool done = 1;
|
||||||
|
}
|
@@ -0,0 +1,26 @@
|
|||||||
|
// file generated with protoc-gen-gotemplate
|
||||||
|
package {{.File.Name | dir}}
|
||||||
|
|
||||||
|
{{- $file := .File}}
|
||||||
|
{{- $currentFile := $file.Name | getProtoFile}}
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
|
||||||
|
{{- range .File.Dependency}}
|
||||||
|
{{- $dependency := . | getProtoFile}}
|
||||||
|
{{$dependency.GoPkg}}
|
||||||
|
{{end}}
|
||||||
|
)
|
||||||
|
|
||||||
|
type Service struct {}
|
||||||
|
|
||||||
|
{{- range .Service.Method}}
|
||||||
|
{{- $in := .InputType | getMessageType $file}}
|
||||||
|
{{- $out := .OutputType | getMessageType $file}}
|
||||||
|
func (service Service) {{.Name}}(ctx context.Context, input *{{$in.GoType $currentFile.GoPkg.Path}}) (*{{$out.GoType $currentFile.GoPkg.Path}}, error) {
|
||||||
|
return nil, fmt.Errorf("method not implemented")
|
||||||
|
}
|
||||||
|
{{end}}
|
3
examples/sitemap/Makefile
Normal file
3
examples/sitemap/Makefile
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
.PHONY: build
|
||||||
|
build:
|
||||||
|
protoc -I. --gotemplate_out=template_dir=.:. sitemap.proto
|
12
examples/sitemap/sitemap.proto
Normal file
12
examples/sitemap/sitemap.proto
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
syntax = "proto3";
|
||||||
|
|
||||||
|
package sitemap;
|
||||||
|
|
||||||
|
service DummyService {
|
||||||
|
rpc Posts(Request) returns (Response) {}
|
||||||
|
rpc Authors(Request) returns (Response) {}
|
||||||
|
rpc Comments(Request) returns (Response) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
message Request {}
|
||||||
|
message Response {}
|
21
examples/sitemap/sitemap.xml
Normal file
21
examples/sitemap/sitemap.xml
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||||
|
<url>
|
||||||
|
<loc>/posts</loc>
|
||||||
|
<priority>0.5</priority>
|
||||||
|
<changefreq>monthly</changefreq>
|
||||||
|
<lastmod>2017-03-31</lastmod>
|
||||||
|
</url>
|
||||||
|
<url>
|
||||||
|
<loc>/authors</loc>
|
||||||
|
<priority>0.5</priority>
|
||||||
|
<changefreq>monthly</changefreq>
|
||||||
|
<lastmod>2017-03-31</lastmod>
|
||||||
|
</url>
|
||||||
|
<url>
|
||||||
|
<loc>/comments</loc>
|
||||||
|
<priority>0.5</priority>
|
||||||
|
<changefreq>monthly</changefreq>
|
||||||
|
<lastmod>2017-03-31</lastmod>
|
||||||
|
</url>
|
||||||
|
</urlset>
|
9
examples/sitemap/sitemap.xml.tmpl
Normal file
9
examples/sitemap/sitemap.xml.tmpl
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">{{range .Service.Method}}
|
||||||
|
<url>
|
||||||
|
<loc>/{{.Name | lower}}</loc>
|
||||||
|
<priority>0.5</priority>
|
||||||
|
<changefreq>monthly</changefreq>
|
||||||
|
<lastmod>{{now | date "2006-01-02"}}</lastmod>
|
||||||
|
</url>{{end}}
|
||||||
|
</urlset>
|
76
glide.lock
generated
76
glide.lock
generated
@@ -1,8 +1,24 @@
|
|||||||
hash: 488701437e53b39667ed5a84ed1500e727ea8d4902d804c3ea7eb50b1fc022a1
|
hash: 1944ae13e983e8da7b26c697fc40d79d34326b8c7f56c8939fb16f1ff8caca5b
|
||||||
updated: 2016-12-21T10:24:42.512734471+01:00
|
updated: 2017-05-18T19:20:01.855895064+02:00
|
||||||
imports:
|
imports:
|
||||||
- name: github.com/aokoli/goutils
|
- name: github.com/aokoli/goutils
|
||||||
version: 9c37978a95bd5c709a15883b6242714ea6709e64
|
version: e57d01ace047c1a43e6a49ecf3ecc50ed2be81d1
|
||||||
|
- name: github.com/dgrijalva/jwt-go
|
||||||
|
version: c9eaceb2896dbb515dae7ec352b377a226a52721
|
||||||
|
- name: github.com/go-kit/kit
|
||||||
|
version: 9f5c614cd1e70102f80b644edbc760805ebf16d5
|
||||||
|
subpackages:
|
||||||
|
- auth/jwt
|
||||||
|
- endpoint
|
||||||
|
- log
|
||||||
|
- transport/grpc
|
||||||
|
- transport/http
|
||||||
|
- name: github.com/go-logfmt/logfmt
|
||||||
|
version: 390ab7935ee28ec6b286364bba9b4dd6410cb3d5
|
||||||
|
- name: github.com/go-stack/stack
|
||||||
|
version: 7a2f19628aabfe68f0766b59e74d6315f8347d22
|
||||||
|
- name: github.com/golang/glog
|
||||||
|
version: 23def4e6c14b4da8ac2ed8007337bc5eb5007998
|
||||||
- name: github.com/golang/protobuf
|
- name: github.com/golang/protobuf
|
||||||
version: 8ee79997227bf9b34611aee7946ae64735e6fd93
|
version: 8ee79997227bf9b34611aee7946ae64735e6fd93
|
||||||
subpackages:
|
subpackages:
|
||||||
@@ -10,12 +26,64 @@ imports:
|
|||||||
- protoc-gen-go/descriptor
|
- protoc-gen-go/descriptor
|
||||||
- protoc-gen-go/generator
|
- protoc-gen-go/generator
|
||||||
- protoc-gen-go/plugin
|
- protoc-gen-go/plugin
|
||||||
|
- name: github.com/gorilla/handlers
|
||||||
|
version: e1b2144f2167de0e1042d1d35e5cba5119d4fb5d
|
||||||
|
- name: github.com/grpc-ecosystem/grpc-gateway
|
||||||
|
version: 589b126116b5fc961939b3e156c29e4d9d58222f
|
||||||
|
subpackages:
|
||||||
|
- protoc-gen-grpc-gateway/descriptor
|
||||||
|
- protoc-gen-grpc-gateway/httprule
|
||||||
|
- utilities
|
||||||
- name: github.com/huandu/xstrings
|
- name: github.com/huandu/xstrings
|
||||||
version: 3959339b333561bf62a38b424fd41517c2c90f40
|
version: 3959339b333561bf62a38b424fd41517c2c90f40
|
||||||
|
- name: github.com/imdario/mergo
|
||||||
|
version: 3e95a51e0639b4cf372f2ccf74c86749d747fbdc
|
||||||
- name: github.com/kr/fs
|
- name: github.com/kr/fs
|
||||||
version: 2788f0dbd16903de03cb8186e5c7d97b69ad387b
|
version: 2788f0dbd16903de03cb8186e5c7d97b69ad387b
|
||||||
|
- name: github.com/kr/logfmt
|
||||||
|
version: b84e30acd515aadc4b783ad4ff83aff3299bdfe0
|
||||||
|
- name: github.com/Masterminds/semver
|
||||||
|
version: 59c29afe1a994eacb71c833025ca7acf874bb1da
|
||||||
- name: github.com/Masterminds/sprig
|
- name: github.com/Masterminds/sprig
|
||||||
version: 69011c0cd9b4d2e0733c4d9e2c8e2a5a0d0a2f2f
|
version: 2f4371ac162f912989f01cc2b6af4ba6660e6a30
|
||||||
- name: github.com/satori/go.uuid
|
- name: github.com/satori/go.uuid
|
||||||
version: 879c5887cd475cd7864858769793b2ceb0d44feb
|
version: 879c5887cd475cd7864858769793b2ceb0d44feb
|
||||||
|
- name: golang.org/x/crypto
|
||||||
|
version: 0fe963104e9d1877082f8fb38f816fcd97eb1d10
|
||||||
|
subpackages:
|
||||||
|
- pbkdf2
|
||||||
|
- scrypt
|
||||||
|
- name: golang.org/x/net
|
||||||
|
version: da2b4fa28524a3baf148c1b94df4440267063c88
|
||||||
|
subpackages:
|
||||||
|
- context
|
||||||
|
- context/ctxhttp
|
||||||
|
- http2
|
||||||
|
- http2/hpack
|
||||||
|
- idna
|
||||||
|
- internal/timeseries
|
||||||
|
- lex/httplex
|
||||||
|
- trace
|
||||||
|
- name: golang.org/x/text
|
||||||
|
version: a49bea13b776691cb1b49873e5d8df96ec74831a
|
||||||
|
subpackages:
|
||||||
|
- secure/bidirule
|
||||||
|
- transform
|
||||||
|
- unicode/bidi
|
||||||
|
- unicode/norm
|
||||||
|
- name: google.golang.org/genproto
|
||||||
|
version: bb3573be0c484136831138976d444b8754777aff
|
||||||
|
subpackages:
|
||||||
|
- googleapis/api/annotations
|
||||||
|
- name: google.golang.org/grpc
|
||||||
|
version: 777daa17ff9b5daef1cfdf915088a2ada3332bf0
|
||||||
|
subpackages:
|
||||||
|
- codes
|
||||||
|
- credentials
|
||||||
|
- grpclog
|
||||||
|
- internal
|
||||||
|
- metadata
|
||||||
|
- naming
|
||||||
|
- peer
|
||||||
|
- transport
|
||||||
testImports: []
|
testImports: []
|
||||||
|
@@ -9,3 +9,8 @@ import:
|
|||||||
- package: github.com/kr/fs
|
- package: github.com/kr/fs
|
||||||
- package: github.com/Masterminds/sprig
|
- package: github.com/Masterminds/sprig
|
||||||
- package: github.com/huandu/xstrings
|
- package: github.com/huandu/xstrings
|
||||||
|
- package: google.golang.org/genproto
|
||||||
|
subpackages:
|
||||||
|
- googleapis/api/annotations
|
||||||
|
- package: github.com/grpc-ecosystem/grpc-gateway
|
||||||
|
version: 1.2.2
|
197
helpers.go
197
helpers.go
@@ -3,17 +3,20 @@ package main
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/Masterminds/sprig"
|
"github.com/Masterminds/sprig"
|
||||||
"github.com/huandu/xstrings"
|
|
||||||
|
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
"github.com/golang/protobuf/protoc-gen-go/descriptor"
|
"github.com/golang/protobuf/protoc-gen-go/descriptor"
|
||||||
options "github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api"
|
ggdescriptor "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
|
||||||
|
"github.com/huandu/xstrings"
|
||||||
|
options "google.golang.org/genproto/googleapis/api/annotations"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var jsReservedRe *regexp.Regexp = regexp.MustCompile(`(^|[^A-Za-z])(do|if|in|for|let|new|try|var|case|else|enum|eval|false|null|this|true|void|with|break|catch|class|const|super|throw|while|yield|delete|export|import|public|return|static|switch|typeof|default|extends|finally|package|private|continue|debugger|function|arguments|interface|protected|implements|instanceof)($|[^A-Za-z])`)
|
||||||
|
|
||||||
var ProtoHelpersFuncMap = template.FuncMap{
|
var ProtoHelpersFuncMap = template.FuncMap{
|
||||||
"string": func(i interface {
|
"string": func(i interface {
|
||||||
String() string
|
String() string
|
||||||
@@ -44,24 +47,37 @@ var ProtoHelpersFuncMap = template.FuncMap{
|
|||||||
return strings.ToLower(s[:1]) + s[1:]
|
return strings.ToLower(s[:1]) + s[1:]
|
||||||
},
|
},
|
||||||
"camelCase": func(s string) string {
|
"camelCase": func(s string) string {
|
||||||
return xstrings.ToCamelCase(s)
|
if len(s) > 1 {
|
||||||
|
return xstrings.ToCamelCase(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
return strings.ToUpper(s[:1])
|
||||||
},
|
},
|
||||||
"lowerCamelCase": func(s string) string {
|
"lowerCamelCase": func(s string) string {
|
||||||
cc := xstrings.ToCamelCase(s)
|
if len(s) > 1 {
|
||||||
return strings.ToLower(cc[:1]) + cc[1:]
|
s = xstrings.ToCamelCase(s)
|
||||||
},
|
}
|
||||||
"snakeCase": func(s string) string {
|
|
||||||
return xstrings.ToSnakeCase(s)
|
return strings.ToLower(s[:1]) + s[1:]
|
||||||
},
|
},
|
||||||
"kebabCase": func(s string) string {
|
"kebabCase": func(s string) string {
|
||||||
return strings.Replace(xstrings.ToSnakeCase(s), "_", "-", -1)
|
return strings.Replace(xstrings.ToSnakeCase(s), "_", "-", -1)
|
||||||
},
|
},
|
||||||
"getMessageType": getMessageType,
|
"snakeCase": xstrings.ToSnakeCase,
|
||||||
"isFieldMessage": isFieldMessage,
|
"getProtoFile": getProtoFile,
|
||||||
"isFieldRepeated": isFieldRepeated,
|
"getMessageType": getMessageType,
|
||||||
"goType": goType,
|
"getEnumValue": getEnumValue,
|
||||||
"httpVerb": httpVerb,
|
"isFieldMessage": isFieldMessage,
|
||||||
"httpPath": httpPath,
|
"isFieldRepeated": isFieldRepeated,
|
||||||
|
"goType": goType,
|
||||||
|
"goTypeWithPackage": goTypeWithPackage,
|
||||||
|
"jsType": jsType,
|
||||||
|
"jsSuffixReserved": jsSuffixReservedKeyword,
|
||||||
|
"namespacedFlowType": namespacedFlowType,
|
||||||
|
"httpVerb": httpVerb,
|
||||||
|
"httpPath": httpPath,
|
||||||
|
"shortType": shortType,
|
||||||
|
"urlHasVarsFromMessage": urlHasVarsFromMessage,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -70,11 +86,44 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getMessageType(f *descriptor.FileDescriptorProto, name string) *descriptor.DescriptorProto {
|
func getProtoFile(name string) *ggdescriptor.File {
|
||||||
|
if registry == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
file, err := registry.LookupFile(name)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return file
|
||||||
|
}
|
||||||
|
|
||||||
|
func getMessageType(f *descriptor.FileDescriptorProto, name string) *ggdescriptor.Message {
|
||||||
|
if registry != nil {
|
||||||
|
msg, err := registry.LookupMsg(".", name)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return msg
|
||||||
|
}
|
||||||
|
|
||||||
|
// name is in the form .packageName.MessageTypeName.InnerMessageTypeName...
|
||||||
|
// e.g. .article.ProductTag
|
||||||
|
splits := strings.Split(name, ".")
|
||||||
|
target := splits[len(splits)-1]
|
||||||
for _, m := range f.MessageType {
|
for _, m := range f.MessageType {
|
||||||
// name usually contains the package name
|
if target == *m.Name {
|
||||||
if strings.HasSuffix(name, *m.Name) {
|
return &ggdescriptor.Message{
|
||||||
return m
|
DescriptorProto: m,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getEnumValue(f []*descriptor.EnumDescriptorProto, name string) []*descriptor.EnumValueDescriptorProto {
|
||||||
|
for _, item := range f {
|
||||||
|
if strings.EqualFold(*item.Name, name) {
|
||||||
|
return item.GetValue()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,8 +131,7 @@ func getMessageType(f *descriptor.FileDescriptorProto, name string) *descriptor.
|
|||||||
}
|
}
|
||||||
|
|
||||||
func isFieldMessage(f *descriptor.FieldDescriptorProto) bool {
|
func isFieldMessage(f *descriptor.FieldDescriptorProto) bool {
|
||||||
if f.Type != nil && *f.Type == descriptor.FieldDescriptorProto_TYPE_MESSAGE &&
|
if f.Type != nil && *f.Type == descriptor.FieldDescriptorProto_TYPE_MESSAGE {
|
||||||
f.Label != nil && *f.Label != descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,30 +146,68 @@ func isFieldRepeated(f *descriptor.FieldDescriptorProto) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func goTypeWithPackage(f *descriptor.FieldDescriptorProto) string {
|
||||||
|
pkg := ""
|
||||||
|
if *f.Type == descriptor.FieldDescriptorProto_TYPE_MESSAGE {
|
||||||
|
pkg = getPackageTypeName(*f.TypeName)
|
||||||
|
}
|
||||||
|
return goType(pkg, f)
|
||||||
|
}
|
||||||
|
|
||||||
func goType(pkg string, f *descriptor.FieldDescriptorProto) string {
|
func goType(pkg string, f *descriptor.FieldDescriptorProto) string {
|
||||||
switch *f.Type {
|
switch *f.Type {
|
||||||
case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
|
case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
|
||||||
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
||||||
|
return "[]float64"
|
||||||
|
}
|
||||||
return "float64"
|
return "float64"
|
||||||
case descriptor.FieldDescriptorProto_TYPE_FLOAT:
|
case descriptor.FieldDescriptorProto_TYPE_FLOAT:
|
||||||
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
||||||
|
return "[]float32"
|
||||||
|
}
|
||||||
return "float32"
|
return "float32"
|
||||||
case descriptor.FieldDescriptorProto_TYPE_INT64:
|
case descriptor.FieldDescriptorProto_TYPE_INT64:
|
||||||
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
||||||
|
return "[]int64"
|
||||||
|
}
|
||||||
return "int64"
|
return "int64"
|
||||||
case descriptor.FieldDescriptorProto_TYPE_UINT64:
|
case descriptor.FieldDescriptorProto_TYPE_UINT64:
|
||||||
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
||||||
|
return "[]uint64"
|
||||||
|
}
|
||||||
return "uint64"
|
return "uint64"
|
||||||
case descriptor.FieldDescriptorProto_TYPE_INT32:
|
case descriptor.FieldDescriptorProto_TYPE_INT32:
|
||||||
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
||||||
|
return "[]uint32"
|
||||||
|
}
|
||||||
return "uint32"
|
return "uint32"
|
||||||
case descriptor.FieldDescriptorProto_TYPE_BOOL:
|
case descriptor.FieldDescriptorProto_TYPE_BOOL:
|
||||||
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
||||||
|
return "[]bool"
|
||||||
|
}
|
||||||
return "bool"
|
return "bool"
|
||||||
case descriptor.FieldDescriptorProto_TYPE_STRING:
|
case descriptor.FieldDescriptorProto_TYPE_STRING:
|
||||||
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
||||||
|
return "[]string"
|
||||||
|
}
|
||||||
return "string"
|
return "string"
|
||||||
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
|
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
|
||||||
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
if pkg != "" {
|
||||||
return fmt.Sprintf("[]*%s.%s", pkg, shortType(*f.TypeName))
|
pkg = pkg + "."
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("*%s.%s", pkg, shortType(*f.TypeName))
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
||||||
|
return fmt.Sprintf("[]*%s%s", pkg, shortType(*f.TypeName))
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("*%s%s", pkg, shortType(*f.TypeName))
|
||||||
case descriptor.FieldDescriptorProto_TYPE_BYTES:
|
case descriptor.FieldDescriptorProto_TYPE_BYTES:
|
||||||
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
||||||
|
return "[]byte"
|
||||||
|
}
|
||||||
return "byte"
|
return "byte"
|
||||||
case descriptor.FieldDescriptorProto_TYPE_UINT32:
|
case descriptor.FieldDescriptorProto_TYPE_UINT32:
|
||||||
|
if *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
||||||
|
return "[]uint32"
|
||||||
|
}
|
||||||
return "uint32"
|
return "uint32"
|
||||||
case descriptor.FieldDescriptorProto_TYPE_ENUM:
|
case descriptor.FieldDescriptorProto_TYPE_ENUM:
|
||||||
return fmt.Sprintf("*%s.%s", pkg, shortType(*f.TypeName))
|
return fmt.Sprintf("*%s.%s", pkg, shortType(*f.TypeName))
|
||||||
@@ -130,11 +216,62 @@ func goType(pkg string, f *descriptor.FieldDescriptorProto) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func jsType(f *descriptor.FieldDescriptorProto) string {
|
||||||
|
template := "%s"
|
||||||
|
if isFieldRepeated(f) == true {
|
||||||
|
template = "Array<%s>"
|
||||||
|
}
|
||||||
|
|
||||||
|
switch *f.Type {
|
||||||
|
case descriptor.FieldDescriptorProto_TYPE_MESSAGE,
|
||||||
|
descriptor.FieldDescriptorProto_TYPE_ENUM:
|
||||||
|
return fmt.Sprintf(template, namespacedFlowType(*f.TypeName))
|
||||||
|
case descriptor.FieldDescriptorProto_TYPE_DOUBLE,
|
||||||
|
descriptor.FieldDescriptorProto_TYPE_FLOAT,
|
||||||
|
descriptor.FieldDescriptorProto_TYPE_INT64,
|
||||||
|
descriptor.FieldDescriptorProto_TYPE_UINT64,
|
||||||
|
descriptor.FieldDescriptorProto_TYPE_INT32,
|
||||||
|
descriptor.FieldDescriptorProto_TYPE_FIXED64,
|
||||||
|
descriptor.FieldDescriptorProto_TYPE_FIXED32,
|
||||||
|
descriptor.FieldDescriptorProto_TYPE_UINT32,
|
||||||
|
descriptor.FieldDescriptorProto_TYPE_SFIXED32,
|
||||||
|
descriptor.FieldDescriptorProto_TYPE_SFIXED64,
|
||||||
|
descriptor.FieldDescriptorProto_TYPE_SINT32,
|
||||||
|
descriptor.FieldDescriptorProto_TYPE_SINT64:
|
||||||
|
return fmt.Sprintf(template, "number")
|
||||||
|
case descriptor.FieldDescriptorProto_TYPE_BOOL:
|
||||||
|
return fmt.Sprintf(template, "boolean")
|
||||||
|
case descriptor.FieldDescriptorProto_TYPE_BYTES:
|
||||||
|
return fmt.Sprintf(template, "Uint8Array")
|
||||||
|
case descriptor.FieldDescriptorProto_TYPE_STRING:
|
||||||
|
return fmt.Sprintf(template, "string")
|
||||||
|
default:
|
||||||
|
return fmt.Sprintf(template, "any")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func jsSuffixReservedKeyword(s string) string {
|
||||||
|
return jsReservedRe.ReplaceAllString(s, "${1}${2}_${3}")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getPackageTypeName(s string) string {
|
||||||
|
if strings.Contains(s, ".") {
|
||||||
|
return strings.Split(s, ".")[1]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func shortType(s string) string {
|
func shortType(s string) string {
|
||||||
t := strings.Split(s, ".")
|
t := strings.Split(s, ".")
|
||||||
return t[len(t)-1]
|
return t[len(t)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func namespacedFlowType(s string) string {
|
||||||
|
trimmed := strings.TrimLeft(s, ".")
|
||||||
|
splitted := strings.Split(trimmed, ".")
|
||||||
|
return strings.Join(splitted, "$")
|
||||||
|
}
|
||||||
|
|
||||||
func httpPath(m *descriptor.MethodDescriptorProto) string {
|
func httpPath(m *descriptor.MethodDescriptorProto) string {
|
||||||
|
|
||||||
ext, err := proto.GetExtension(m.Options, options.E_Http)
|
ext, err := proto.GetExtension(m.Options, options.E_Http)
|
||||||
@@ -192,3 +329,15 @@ func httpVerb(m *descriptor.MethodDescriptorProto) string {
|
|||||||
return t.Custom.Kind
|
return t.Custom.Kind
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func urlHasVarsFromMessage(path string, d *descriptor.DescriptorProto) bool {
|
||||||
|
for _, field := range d.Field {
|
||||||
|
if !isFieldMessage(field) {
|
||||||
|
if strings.Contains(path, fmt.Sprintf("{%s}", *field.Name)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
71
main.go
71
main.go
@@ -8,6 +8,12 @@ import (
|
|||||||
|
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
"github.com/golang/protobuf/protoc-gen-go/generator"
|
"github.com/golang/protobuf/protoc-gen-go/generator"
|
||||||
|
"github.com/golang/protobuf/protoc-gen-go/plugin"
|
||||||
|
ggdescriptor "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
registry *ggdescriptor.Registry // some helpers need access to registry
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -29,9 +35,13 @@ func main() {
|
|||||||
g.CommandLineParameters(g.Request.GetParameter())
|
g.CommandLineParameters(g.Request.GetParameter())
|
||||||
|
|
||||||
// Parse parameters
|
// Parse parameters
|
||||||
templateDir := "./templates"
|
var (
|
||||||
destinationDir := "."
|
templateDir = "./templates"
|
||||||
debug := false
|
destinationDir = "."
|
||||||
|
debug = false
|
||||||
|
all = false
|
||||||
|
singlePackageMode = false
|
||||||
|
)
|
||||||
if parameter := g.Request.GetParameter(); parameter != "" {
|
if parameter := g.Request.GetParameter(); parameter != "" {
|
||||||
for _, param := range strings.Split(parameter, ",") {
|
for _, param := range strings.Split(parameter, ",") {
|
||||||
parts := strings.Split(param, "=")
|
parts := strings.Split(param, "=")
|
||||||
@@ -46,6 +56,15 @@ func main() {
|
|||||||
case "destination_dir":
|
case "destination_dir":
|
||||||
destinationDir = parts[1]
|
destinationDir = parts[1]
|
||||||
break
|
break
|
||||||
|
case "single-package-mode":
|
||||||
|
switch strings.ToLower(parts[1]) {
|
||||||
|
case "true", "t":
|
||||||
|
singlePackageMode = true
|
||||||
|
case "false", "f":
|
||||||
|
default:
|
||||||
|
log.Printf("Err: invalid value for single-package-mode: %q", parts[1])
|
||||||
|
}
|
||||||
|
break
|
||||||
case "debug":
|
case "debug":
|
||||||
switch strings.ToLower(parts[1]) {
|
switch strings.ToLower(parts[1]) {
|
||||||
case "true", "t":
|
case "true", "t":
|
||||||
@@ -55,17 +74,59 @@ func main() {
|
|||||||
log.Printf("Err: invalid value for debug: %q", parts[1])
|
log.Printf("Err: invalid value for debug: %q", parts[1])
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
case "all":
|
||||||
|
switch strings.ToLower(parts[1]) {
|
||||||
|
case "true", "t":
|
||||||
|
all = true
|
||||||
|
case "false", "f":
|
||||||
|
default:
|
||||||
|
log.Printf("Err: invalid value for debug: %q", parts[1])
|
||||||
|
}
|
||||||
|
break
|
||||||
default:
|
default:
|
||||||
log.Printf("Err: unknown parameter: %q", param)
|
log.Printf("Err: unknown parameter: %q", param)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tmplMap := make(map[string]*plugin_go.CodeGeneratorResponse_File)
|
||||||
|
concatOrAppend := func(file *plugin_go.CodeGeneratorResponse_File) {
|
||||||
|
if val, ok := tmplMap[file.GetName()]; ok {
|
||||||
|
*val.Content += file.GetContent()
|
||||||
|
} else {
|
||||||
|
tmplMap[file.GetName()] = file
|
||||||
|
g.Response.File = append(g.Response.File, file)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if singlePackageMode {
|
||||||
|
registry = ggdescriptor.NewRegistry()
|
||||||
|
if err := registry.Load(g.Request); err != nil {
|
||||||
|
g.Error(err, "registry: failed to load the request")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Generate the encoders
|
// Generate the encoders
|
||||||
for _, file := range g.Request.GetProtoFile() {
|
for _, file := range g.Request.GetProtoFile() {
|
||||||
|
if all {
|
||||||
|
if singlePackageMode {
|
||||||
|
if _, err := registry.LookupFile(file.GetName()); err != nil {
|
||||||
|
g.Error(err, "registry: failed to lookup file %q", file.GetName())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
encoder := NewGenericTemplateBasedEncoder(templateDir, file, debug, destinationDir)
|
||||||
|
for _, tmpl := range encoder.Files() {
|
||||||
|
concatOrAppend(tmpl)
|
||||||
|
}
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
for _, service := range file.GetService() {
|
for _, service := range file.GetService() {
|
||||||
encoder := NewGenericTemplateBasedEncoder(templateDir, service, file, debug, destinationDir)
|
encoder := NewGenericServiceTemplateBasedEncoder(templateDir, service, file, debug, destinationDir)
|
||||||
g.Response.File = append(g.Response.File, encoder.Files()...)
|
for _, tmpl := range encoder.Files() {
|
||||||
|
concatOrAppend(tmpl)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
25
vendor/github.com/Masterminds/semver/.travis.yml
generated
vendored
Normal file
25
vendor/github.com/Masterminds/semver/.travis.yml
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
language: go
|
||||||
|
|
||||||
|
go:
|
||||||
|
- 1.5
|
||||||
|
- 1.6
|
||||||
|
- 1.7
|
||||||
|
- tip
|
||||||
|
|
||||||
|
# Setting sudo access to false will let Travis CI use containers rather than
|
||||||
|
# VMs to run the tests. For more details see:
|
||||||
|
# - http://docs.travis-ci.com/user/workers/container-based-infrastructure/
|
||||||
|
# - http://docs.travis-ci.com/user/workers/standard-infrastructure/
|
||||||
|
sudo: false
|
||||||
|
|
||||||
|
script:
|
||||||
|
- GO15VENDOREXPERIMENT=1 make setup
|
||||||
|
- GO15VENDOREXPERIMENT=1 make test
|
||||||
|
|
||||||
|
notifications:
|
||||||
|
webhooks:
|
||||||
|
urls:
|
||||||
|
- https://webhooks.gitter.im/e/06e3328629952dabe3e0
|
||||||
|
on_success: change # options: [always|never|change] default: always
|
||||||
|
on_failure: always # options: [always|never|change] default: always
|
||||||
|
on_start: never # options: [always|never|change] default: always
|
45
vendor/github.com/Masterminds/semver/CHANGELOG.md
generated
vendored
Normal file
45
vendor/github.com/Masterminds/semver/CHANGELOG.md
generated
vendored
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
# Release 1.2.2 (2016-12-13)
|
||||||
|
|
||||||
|
## Fixed
|
||||||
|
- #34: Fixed issue where hyphen range was not working with pre-release parsing.
|
||||||
|
|
||||||
|
# Release 1.2.1 (2016-11-28)
|
||||||
|
|
||||||
|
## Fixed
|
||||||
|
- #24: Fixed edge case issue where constraint "> 0" does not handle "0.0.1-alpha"
|
||||||
|
properly.
|
||||||
|
|
||||||
|
# Release 1.2.0 (2016-11-04)
|
||||||
|
|
||||||
|
## Added
|
||||||
|
- #20: Added MustParse function for versions (thanks @adamreese)
|
||||||
|
- #15: Added increment methods on versions (thanks @mh-cbon)
|
||||||
|
|
||||||
|
## Fixed
|
||||||
|
- Issue #21: Per the SemVer spec (section 9) a pre-release is unstable and
|
||||||
|
might not satisfy the intended compatibility. The change here ignores pre-releases
|
||||||
|
on constraint checks (e.g., ~ or ^) when a pre-release is not part of the
|
||||||
|
constraint. For example, `^1.2.3` will ignore pre-releases while
|
||||||
|
`^1.2.3-alpha` will include them.
|
||||||
|
|
||||||
|
# Release 1.1.1 (2016-06-30)
|
||||||
|
|
||||||
|
## Changed
|
||||||
|
- Issue #9: Speed up version comparison performance (thanks @sdboyer)
|
||||||
|
- Issue #8: Added benchmarks (thanks @sdboyer)
|
||||||
|
- Updated Go Report Card URL to new location
|
||||||
|
- Updated Readme to add code snippet formatting (thanks @mh-cbon)
|
||||||
|
- Updating tagging to v[SemVer] structure for compatibility with other tools.
|
||||||
|
|
||||||
|
# Release 1.1.0 (2016-03-11)
|
||||||
|
|
||||||
|
- Issue #2: Implemented validation to provide reasons a versions failed a
|
||||||
|
constraint.
|
||||||
|
|
||||||
|
# Release 1.0.1 (2015-12-31)
|
||||||
|
|
||||||
|
- Fixed #1: * constraint failing on valid versions.
|
||||||
|
|
||||||
|
# Release 1.0.0 (2015-10-20)
|
||||||
|
|
||||||
|
- Initial release
|
20
vendor/github.com/Masterminds/semver/LICENSE.txt
generated
vendored
Normal file
20
vendor/github.com/Masterminds/semver/LICENSE.txt
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
The Masterminds
|
||||||
|
Copyright (C) 2014-2015, Matt Butcher and Matt Farina
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
36
vendor/github.com/Masterminds/semver/Makefile
generated
vendored
Normal file
36
vendor/github.com/Masterminds/semver/Makefile
generated
vendored
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
.PHONY: setup
|
||||||
|
setup:
|
||||||
|
go get -u gopkg.in/alecthomas/gometalinter.v1
|
||||||
|
gometalinter.v1 --install
|
||||||
|
|
||||||
|
.PHONY: test
|
||||||
|
test: validate lint
|
||||||
|
@echo "==> Running tests"
|
||||||
|
go test -v
|
||||||
|
|
||||||
|
.PHONY: validate
|
||||||
|
validate:
|
||||||
|
@echo "==> Running static validations"
|
||||||
|
@gometalinter.v1 \
|
||||||
|
--disable-all \
|
||||||
|
--enable deadcode \
|
||||||
|
--severity deadcode:error \
|
||||||
|
--enable gofmt \
|
||||||
|
--enable gosimple \
|
||||||
|
--enable ineffassign \
|
||||||
|
--enable misspell \
|
||||||
|
--enable vet \
|
||||||
|
--tests \
|
||||||
|
--vendor \
|
||||||
|
--deadline 60s \
|
||||||
|
./... || exit_code=1
|
||||||
|
|
||||||
|
.PHONY: lint
|
||||||
|
lint:
|
||||||
|
@echo "==> Running linters"
|
||||||
|
@gometalinter.v1 \
|
||||||
|
--disable-all \
|
||||||
|
--enable golint \
|
||||||
|
--vendor \
|
||||||
|
--deadline 60s \
|
||||||
|
./... || :
|
163
vendor/github.com/Masterminds/semver/README.md
generated
vendored
Normal file
163
vendor/github.com/Masterminds/semver/README.md
generated
vendored
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
# SemVer
|
||||||
|
|
||||||
|
The `semver` package provides the ability to work with [Semantic Versions](http://semver.org) in Go. Specifically it provides the ability to:
|
||||||
|
|
||||||
|
* Parse semantic versions
|
||||||
|
* Sort semantic versions
|
||||||
|
* Check if a semantic version fits within a set of constraints
|
||||||
|
* Optionally work with a `v` prefix
|
||||||
|
|
||||||
|
[](https://travis-ci.org/Masterminds/semver) [](https://ci.appveyor.com/project/mattfarina/semver/branch/master) [](https://godoc.org/github.com/Masterminds/semver) [](https://goreportcard.com/report/github.com/Masterminds/semver)
|
||||||
|
|
||||||
|
## Parsing Semantic Versions
|
||||||
|
|
||||||
|
To parse a semantic version use the `NewVersion` function. For example,
|
||||||
|
|
||||||
|
```go
|
||||||
|
v, err := semver.NewVersion("1.2.3-beta.1+build345")
|
||||||
|
```
|
||||||
|
|
||||||
|
If there is an error the version wasn't parseable. The version object has methods
|
||||||
|
to get the parts of the version, compare it to other versions, convert the
|
||||||
|
version back into a string, and get the original string. For more details
|
||||||
|
please see the [documentation](https://godoc.org/github.com/Masterminds/semver).
|
||||||
|
|
||||||
|
## Sorting Semantic Versions
|
||||||
|
|
||||||
|
A set of versions can be sorted using the [`sort`](https://golang.org/pkg/sort/)
|
||||||
|
package from the standard library. For example,
|
||||||
|
|
||||||
|
```go
|
||||||
|
raw := []string{"1.2.3", "1.0", "1.3", "2", "0.4.2",}
|
||||||
|
vs := make([]*semver.Version, len(raw))
|
||||||
|
for i, r := range raw {
|
||||||
|
v, err := semver.NewVersion(r)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error parsing version: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
vs[i] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Sort(semver.Collection(vs))
|
||||||
|
```
|
||||||
|
|
||||||
|
## Checking Version Constraints
|
||||||
|
|
||||||
|
Checking a version against version constraints is one of the most featureful
|
||||||
|
parts of the package.
|
||||||
|
|
||||||
|
```go
|
||||||
|
c, err := semver.NewConstraint(">= 1.2.3")
|
||||||
|
if err != nil {
|
||||||
|
// Handle constraint not being parseable.
|
||||||
|
}
|
||||||
|
|
||||||
|
v, _ := semver.NewVersion("1.3")
|
||||||
|
if err != nil {
|
||||||
|
// Handle version not being parseable.
|
||||||
|
}
|
||||||
|
// Check if the version meets the constraints. The a variable will be true.
|
||||||
|
a := c.Check(v)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Basic Comparisons
|
||||||
|
|
||||||
|
There are two elements to the comparisons. First, a comparison string is a list
|
||||||
|
of comma separated and comparisons. These are then separated by || separated or
|
||||||
|
comparisons. For example, `">= 1.2, < 3.0.0 || >= 4.2.3"` is looking for a
|
||||||
|
comparison that's greater than or equal to 1.2 and less than 3.0.0 or is
|
||||||
|
greater than or equal to 4.2.3.
|
||||||
|
|
||||||
|
The basic comparisons are:
|
||||||
|
|
||||||
|
* `=`: equal (aliased to no operator)
|
||||||
|
* `!=`: not equal
|
||||||
|
* `>`: greater than
|
||||||
|
* `<`: less than
|
||||||
|
* `>=`: greater than or equal to
|
||||||
|
* `<=`: less than or equal to
|
||||||
|
|
||||||
|
_Note, according to the Semantic Version specification pre-releases may not be
|
||||||
|
API compliant with their release counterpart. It says,_
|
||||||
|
|
||||||
|
> _A pre-release version indicates that the version is unstable and might not satisfy the intended compatibility requirements as denoted by its associated normal version._
|
||||||
|
|
||||||
|
_SemVer comparisons without a pre-release value will skip pre-release versions.
|
||||||
|
For example, `>1.2.3` will skip pre-releases when looking at a list of values
|
||||||
|
while `>1.2.3-alpha.1` will evaluate pre-releases._
|
||||||
|
|
||||||
|
## Hyphen Range Comparisons
|
||||||
|
|
||||||
|
There are multiple methods to handle ranges and the first is hyphens ranges.
|
||||||
|
These look like:
|
||||||
|
|
||||||
|
* `1.2 - 1.4.5` which is equivalent to `>= 1.2, <= 1.4.5`
|
||||||
|
* `2.3.4 - 4.5` which is equivalent to `>= 2.3.4, <= 4.5`
|
||||||
|
|
||||||
|
## Wildcards In Comparisons
|
||||||
|
|
||||||
|
The `x`, `X`, and `*` characters can be used as a wildcard character. This works
|
||||||
|
for all comparison operators. When used on the `=` operator it falls
|
||||||
|
back to the pack level comparison (see tilde below). For example,
|
||||||
|
|
||||||
|
* `1.2.x` is equivalent to `>= 1.2.0, < 1.3.0`
|
||||||
|
* `>= 1.2.x` is equivalent to `>= 1.2.0`
|
||||||
|
* `<= 2.x` is equivalent to `<= 3`
|
||||||
|
* `*` is equivalent to `>= 0.0.0`
|
||||||
|
|
||||||
|
## Tilde Range Comparisons (Patch)
|
||||||
|
|
||||||
|
The tilde (`~`) comparison operator is for patch level ranges when a minor
|
||||||
|
version is specified and major level changes when the minor number is missing.
|
||||||
|
For example,
|
||||||
|
|
||||||
|
* `~1.2.3` is equivalent to `>= 1.2.3, < 1.3.0`
|
||||||
|
* `~1` is equivalent to `>= 1, < 2`
|
||||||
|
* `~2.3` is equivalent to `>= 2.3, < 2.4`
|
||||||
|
* `~1.2.x` is equivalent to `>= 1.2.0, < 1.3.0`
|
||||||
|
* `~1.x` is equivalent to `>= 1, < 2`
|
||||||
|
|
||||||
|
## Caret Range Comparisons (Major)
|
||||||
|
|
||||||
|
The caret (`^`) comparison operator is for major level changes. This is useful
|
||||||
|
when comparisons of API versions as a major change is API breaking. For example,
|
||||||
|
|
||||||
|
* `^1.2.3` is equivalent to `>= 1.2.3, < 2.0.0`
|
||||||
|
* `^1.2.x` is equivalent to `>= 1.2.0, < 2.0.0`
|
||||||
|
* `^2.3` is equivalent to `>= 2.3, < 3`
|
||||||
|
* `^2.x` is equivalent to `>= 2.0.0, < 3`
|
||||||
|
|
||||||
|
# Validation
|
||||||
|
|
||||||
|
In addition to testing a version against a constraint, a version can be validated
|
||||||
|
against a constraint. When validation fails a slice of errors containing why a
|
||||||
|
version didn't meet the constraint is returned. For example,
|
||||||
|
|
||||||
|
```go
|
||||||
|
c, err := semver.NewConstraint("<= 1.2.3, >= 1.4")
|
||||||
|
if err != nil {
|
||||||
|
// Handle constraint not being parseable.
|
||||||
|
}
|
||||||
|
|
||||||
|
v, _ := semver.NewVersion("1.3")
|
||||||
|
if err != nil {
|
||||||
|
// Handle version not being parseable.
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate a version against a constraint.
|
||||||
|
a, msgs := c.Validate(v)
|
||||||
|
// a is false
|
||||||
|
for _, m := range msgs {
|
||||||
|
fmt.Println(m)
|
||||||
|
|
||||||
|
// Loops over the errors which would read
|
||||||
|
// "1.3 is greater than 1.2.3"
|
||||||
|
// "1.3 is less than 1.4"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
# Contribute
|
||||||
|
|
||||||
|
If you find an issue or want to contribute please file an [issue](https://github.com/Masterminds/semver/issues)
|
||||||
|
or [create a pull request](https://github.com/Masterminds/semver/pulls).
|
44
vendor/github.com/Masterminds/semver/appveyor.yml
generated
vendored
Normal file
44
vendor/github.com/Masterminds/semver/appveyor.yml
generated
vendored
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
version: build-{build}.{branch}
|
||||||
|
|
||||||
|
clone_folder: C:\gopath\src\github.com\Masterminds\semver
|
||||||
|
shallow_clone: true
|
||||||
|
|
||||||
|
environment:
|
||||||
|
GOPATH: C:\gopath
|
||||||
|
|
||||||
|
platform:
|
||||||
|
- x64
|
||||||
|
|
||||||
|
install:
|
||||||
|
- go version
|
||||||
|
- go env
|
||||||
|
- go get -u gopkg.in/alecthomas/gometalinter.v1
|
||||||
|
- set PATH=%PATH%;%GOPATH%\bin
|
||||||
|
- gometalinter.v1.exe --install
|
||||||
|
|
||||||
|
build_script:
|
||||||
|
- go install -v ./...
|
||||||
|
|
||||||
|
test_script:
|
||||||
|
- "gometalinter.v1 \
|
||||||
|
--disable-all \
|
||||||
|
--enable deadcode \
|
||||||
|
--severity deadcode:error \
|
||||||
|
--enable gofmt \
|
||||||
|
--enable gosimple \
|
||||||
|
--enable ineffassign \
|
||||||
|
--enable misspell \
|
||||||
|
--enable vet \
|
||||||
|
--tests \
|
||||||
|
--vendor \
|
||||||
|
--deadline 60s \
|
||||||
|
./... || exit_code=1"
|
||||||
|
- "gometalinter.v1 \
|
||||||
|
--disable-all \
|
||||||
|
--enable golint \
|
||||||
|
--vendor \
|
||||||
|
--deadline 60s \
|
||||||
|
./... || :"
|
||||||
|
- go test -v
|
||||||
|
|
||||||
|
deploy: off
|
157
vendor/github.com/Masterminds/semver/benchmark_test.go
generated
vendored
Normal file
157
vendor/github.com/Masterminds/semver/benchmark_test.go
generated
vendored
Normal file
@@ -0,0 +1,157 @@
|
|||||||
|
package semver_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/Masterminds/semver"
|
||||||
|
)
|
||||||
|
|
||||||
|
/* Constraint creation benchmarks */
|
||||||
|
|
||||||
|
func benchNewConstraint(c string, b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
semver.NewConstraint(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkNewConstraintUnary(b *testing.B) {
|
||||||
|
benchNewConstraint("=2.0", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkNewConstraintTilde(b *testing.B) {
|
||||||
|
benchNewConstraint("~2.0.0", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkNewConstraintCaret(b *testing.B) {
|
||||||
|
benchNewConstraint("^2.0.0", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkNewConstraintWildcard(b *testing.B) {
|
||||||
|
benchNewConstraint("1.x", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkNewConstraintRange(b *testing.B) {
|
||||||
|
benchNewConstraint(">=2.1.x, <3.1.0", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkNewConstraintUnion(b *testing.B) {
|
||||||
|
benchNewConstraint("~2.0.0 || =3.1.0", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check benchmarks */
|
||||||
|
|
||||||
|
func benchCheckVersion(c, v string, b *testing.B) {
|
||||||
|
version, _ := semver.NewVersion(v)
|
||||||
|
constraint, _ := semver.NewConstraint(c)
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
constraint.Check(version)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkCheckVersionUnary(b *testing.B) {
|
||||||
|
benchCheckVersion("=2.0", "2.0.0", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkCheckVersionTilde(b *testing.B) {
|
||||||
|
benchCheckVersion("~2.0.0", "2.0.5", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkCheckVersionCaret(b *testing.B) {
|
||||||
|
benchCheckVersion("^2.0.0", "2.1.0", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkCheckVersionWildcard(b *testing.B) {
|
||||||
|
benchCheckVersion("1.x", "1.4.0", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkCheckVersionRange(b *testing.B) {
|
||||||
|
benchCheckVersion(">=2.1.x, <3.1.0", "2.4.5", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkCheckVersionUnion(b *testing.B) {
|
||||||
|
benchCheckVersion("~2.0.0 || =3.1.0", "3.1.0", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func benchValidateVersion(c, v string, b *testing.B) {
|
||||||
|
version, _ := semver.NewVersion(v)
|
||||||
|
constraint, _ := semver.NewConstraint(c)
|
||||||
|
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
constraint.Validate(version)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Validate benchmarks, including fails */
|
||||||
|
|
||||||
|
func BenchmarkValidateVersionUnary(b *testing.B) {
|
||||||
|
benchValidateVersion("=2.0", "2.0.0", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkValidateVersionUnaryFail(b *testing.B) {
|
||||||
|
benchValidateVersion("=2.0", "2.0.1", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkValidateVersionTilde(b *testing.B) {
|
||||||
|
benchValidateVersion("~2.0.0", "2.0.5", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkValidateVersionTildeFail(b *testing.B) {
|
||||||
|
benchValidateVersion("~2.0.0", "1.0.5", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkValidateVersionCaret(b *testing.B) {
|
||||||
|
benchValidateVersion("^2.0.0", "2.1.0", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkValidateVersionCaretFail(b *testing.B) {
|
||||||
|
benchValidateVersion("^2.0.0", "4.1.0", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkValidateVersionWildcard(b *testing.B) {
|
||||||
|
benchValidateVersion("1.x", "1.4.0", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkValidateVersionWildcardFail(b *testing.B) {
|
||||||
|
benchValidateVersion("1.x", "2.4.0", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkValidateVersionRange(b *testing.B) {
|
||||||
|
benchValidateVersion(">=2.1.x, <3.1.0", "2.4.5", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkValidateVersionRangeFail(b *testing.B) {
|
||||||
|
benchValidateVersion(">=2.1.x, <3.1.0", "1.4.5", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkValidateVersionUnion(b *testing.B) {
|
||||||
|
benchValidateVersion("~2.0.0 || =3.1.0", "3.1.0", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkValidateVersionUnionFail(b *testing.B) {
|
||||||
|
benchValidateVersion("~2.0.0 || =3.1.0", "3.1.1", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Version creation benchmarks */
|
||||||
|
|
||||||
|
func benchNewVersion(v string, b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
semver.NewVersion(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkNewVersionSimple(b *testing.B) {
|
||||||
|
benchNewVersion("1.0.0", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkNewVersionPre(b *testing.B) {
|
||||||
|
benchNewVersion("1.0.0-alpha", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkNewVersionMeta(b *testing.B) {
|
||||||
|
benchNewVersion("1.0.0+metadata", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkNewVersionMetaDash(b *testing.B) {
|
||||||
|
benchNewVersion("1.0.0+metadata-dash", b)
|
||||||
|
}
|
24
vendor/github.com/Masterminds/semver/collection.go
generated
vendored
Normal file
24
vendor/github.com/Masterminds/semver/collection.go
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package semver
|
||||||
|
|
||||||
|
// Collection is a collection of Version instances and implements the sort
|
||||||
|
// interface. See the sort package for more details.
|
||||||
|
// https://golang.org/pkg/sort/
|
||||||
|
type Collection []*Version
|
||||||
|
|
||||||
|
// Len returns the length of a collection. The number of Version instances
|
||||||
|
// on the slice.
|
||||||
|
func (c Collection) Len() int {
|
||||||
|
return len(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Less is needed for the sort interface to compare two Version objects on the
|
||||||
|
// slice. If checks if one is less than the other.
|
||||||
|
func (c Collection) Less(i, j int) bool {
|
||||||
|
return c[i].LessThan(c[j])
|
||||||
|
}
|
||||||
|
|
||||||
|
// Swap is needed for the sort interface to replace the Version objects
|
||||||
|
// at two different positions in the slice.
|
||||||
|
func (c Collection) Swap(i, j int) {
|
||||||
|
c[i], c[j] = c[j], c[i]
|
||||||
|
}
|
46
vendor/github.com/Masterminds/semver/collection_test.go
generated
vendored
Normal file
46
vendor/github.com/Masterminds/semver/collection_test.go
generated
vendored
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package semver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"sort"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCollection(t *testing.T) {
|
||||||
|
raw := []string{
|
||||||
|
"1.2.3",
|
||||||
|
"1.0",
|
||||||
|
"1.3",
|
||||||
|
"2",
|
||||||
|
"0.4.2",
|
||||||
|
}
|
||||||
|
|
||||||
|
vs := make([]*Version, len(raw))
|
||||||
|
for i, r := range raw {
|
||||||
|
v, err := NewVersion(r)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error parsing version: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
vs[i] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Sort(Collection(vs))
|
||||||
|
|
||||||
|
e := []string{
|
||||||
|
"0.4.2",
|
||||||
|
"1.0.0",
|
||||||
|
"1.2.3",
|
||||||
|
"1.3.0",
|
||||||
|
"2.0.0",
|
||||||
|
}
|
||||||
|
|
||||||
|
a := make([]string, len(vs))
|
||||||
|
for i, v := range vs {
|
||||||
|
a[i] = v.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(a, e) {
|
||||||
|
t.Error("Sorting Collection failed")
|
||||||
|
}
|
||||||
|
}
|
421
vendor/github.com/Masterminds/semver/constraints.go
generated
vendored
Normal file
421
vendor/github.com/Masterminds/semver/constraints.go
generated
vendored
Normal file
@@ -0,0 +1,421 @@
|
|||||||
|
package semver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Constraints is one or more constraint that a semantic version can be
|
||||||
|
// checked against.
|
||||||
|
type Constraints struct {
|
||||||
|
constraints [][]*constraint
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewConstraint returns a Constraints instance that a Version instance can
|
||||||
|
// be checked against. If there is a parse error it will be returned.
|
||||||
|
func NewConstraint(c string) (*Constraints, error) {
|
||||||
|
|
||||||
|
// Rewrite - ranges into a comparison operation.
|
||||||
|
c = rewriteRange(c)
|
||||||
|
|
||||||
|
ors := strings.Split(c, "||")
|
||||||
|
or := make([][]*constraint, len(ors))
|
||||||
|
for k, v := range ors {
|
||||||
|
cs := strings.Split(v, ",")
|
||||||
|
result := make([]*constraint, len(cs))
|
||||||
|
for i, s := range cs {
|
||||||
|
pc, err := parseConstraint(s)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
result[i] = pc
|
||||||
|
}
|
||||||
|
or[k] = result
|
||||||
|
}
|
||||||
|
|
||||||
|
o := &Constraints{constraints: or}
|
||||||
|
return o, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check tests if a version satisfies the constraints.
|
||||||
|
func (cs Constraints) Check(v *Version) bool {
|
||||||
|
// loop over the ORs and check the inner ANDs
|
||||||
|
for _, o := range cs.constraints {
|
||||||
|
joy := true
|
||||||
|
for _, c := range o {
|
||||||
|
if !c.check(v) {
|
||||||
|
joy = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if joy {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate checks if a version satisfies a constraint. If not a slice of
|
||||||
|
// reasons for the failure are returned in addition to a bool.
|
||||||
|
func (cs Constraints) Validate(v *Version) (bool, []error) {
|
||||||
|
// loop over the ORs and check the inner ANDs
|
||||||
|
var e []error
|
||||||
|
for _, o := range cs.constraints {
|
||||||
|
joy := true
|
||||||
|
for _, c := range o {
|
||||||
|
if !c.check(v) {
|
||||||
|
em := fmt.Errorf(c.msg, v, c.orig)
|
||||||
|
e = append(e, em)
|
||||||
|
joy = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if joy {
|
||||||
|
return true, []error{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false, e
|
||||||
|
}
|
||||||
|
|
||||||
|
var constraintOps map[string]cfunc
|
||||||
|
var constraintMsg map[string]string
|
||||||
|
var constraintRegex *regexp.Regexp
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
constraintOps = map[string]cfunc{
|
||||||
|
"": constraintTildeOrEqual,
|
||||||
|
"=": constraintTildeOrEqual,
|
||||||
|
"!=": constraintNotEqual,
|
||||||
|
">": constraintGreaterThan,
|
||||||
|
"<": constraintLessThan,
|
||||||
|
">=": constraintGreaterThanEqual,
|
||||||
|
"=>": constraintGreaterThanEqual,
|
||||||
|
"<=": constraintLessThanEqual,
|
||||||
|
"=<": constraintLessThanEqual,
|
||||||
|
"~": constraintTilde,
|
||||||
|
"~>": constraintTilde,
|
||||||
|
"^": constraintCaret,
|
||||||
|
}
|
||||||
|
|
||||||
|
constraintMsg = map[string]string{
|
||||||
|
"": "%s is not equal to %s",
|
||||||
|
"=": "%s is not equal to %s",
|
||||||
|
"!=": "%s is equal to %s",
|
||||||
|
">": "%s is less than or equal to %s",
|
||||||
|
"<": "%s is greater than or equal to %s",
|
||||||
|
">=": "%s is less than %s",
|
||||||
|
"=>": "%s is less than %s",
|
||||||
|
"<=": "%s is greater than %s",
|
||||||
|
"=<": "%s is greater than %s",
|
||||||
|
"~": "%s does not have same major and minor version as %s",
|
||||||
|
"~>": "%s does not have same major and minor version as %s",
|
||||||
|
"^": "%s does not have same major version as %s",
|
||||||
|
}
|
||||||
|
|
||||||
|
ops := make([]string, 0, len(constraintOps))
|
||||||
|
for k := range constraintOps {
|
||||||
|
ops = append(ops, regexp.QuoteMeta(k))
|
||||||
|
}
|
||||||
|
|
||||||
|
constraintRegex = regexp.MustCompile(fmt.Sprintf(
|
||||||
|
`^\s*(%s)\s*(%s)\s*$`,
|
||||||
|
strings.Join(ops, "|"),
|
||||||
|
cvRegex))
|
||||||
|
|
||||||
|
constraintRangeRegex = regexp.MustCompile(fmt.Sprintf(
|
||||||
|
`\s*(%s)\s+-\s+(%s)\s*`,
|
||||||
|
cvRegex, cvRegex))
|
||||||
|
}
|
||||||
|
|
||||||
|
// An individual constraint
|
||||||
|
type constraint struct {
|
||||||
|
// The callback function for the restraint. It performs the logic for
|
||||||
|
// the constraint.
|
||||||
|
function cfunc
|
||||||
|
|
||||||
|
msg string
|
||||||
|
|
||||||
|
// The version used in the constraint check. For example, if a constraint
|
||||||
|
// is '<= 2.0.0' the con a version instance representing 2.0.0.
|
||||||
|
con *Version
|
||||||
|
|
||||||
|
// The original parsed version (e.g., 4.x from != 4.x)
|
||||||
|
orig string
|
||||||
|
|
||||||
|
// When an x is used as part of the version (e.g., 1.x)
|
||||||
|
minorDirty bool
|
||||||
|
dirty bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if a version meets the constraint
|
||||||
|
func (c *constraint) check(v *Version) bool {
|
||||||
|
return c.function(v, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
type cfunc func(v *Version, c *constraint) bool
|
||||||
|
|
||||||
|
func parseConstraint(c string) (*constraint, error) {
|
||||||
|
m := constraintRegex.FindStringSubmatch(c)
|
||||||
|
if m == nil {
|
||||||
|
return nil, fmt.Errorf("improper constraint: %s", c)
|
||||||
|
}
|
||||||
|
|
||||||
|
ver := m[2]
|
||||||
|
orig := ver
|
||||||
|
minorDirty := false
|
||||||
|
dirty := false
|
||||||
|
if isX(m[3]) {
|
||||||
|
ver = "0.0.0"
|
||||||
|
dirty = true
|
||||||
|
} else if isX(strings.TrimPrefix(m[4], ".")) {
|
||||||
|
minorDirty = true
|
||||||
|
dirty = true
|
||||||
|
ver = fmt.Sprintf("%s.0.0%s", m[3], m[6])
|
||||||
|
} else if isX(strings.TrimPrefix(m[5], ".")) {
|
||||||
|
dirty = true
|
||||||
|
ver = fmt.Sprintf("%s%s.0%s", m[3], m[4], m[6])
|
||||||
|
}
|
||||||
|
|
||||||
|
con, err := NewVersion(ver)
|
||||||
|
if err != nil {
|
||||||
|
|
||||||
|
// The constraintRegex should catch any regex parsing errors. So,
|
||||||
|
// we should never get here.
|
||||||
|
return nil, errors.New("constraint Parser Error")
|
||||||
|
}
|
||||||
|
|
||||||
|
cs := &constraint{
|
||||||
|
function: constraintOps[m[1]],
|
||||||
|
msg: constraintMsg[m[1]],
|
||||||
|
con: con,
|
||||||
|
orig: orig,
|
||||||
|
minorDirty: minorDirty,
|
||||||
|
dirty: dirty,
|
||||||
|
}
|
||||||
|
return cs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Constraint functions
|
||||||
|
func constraintNotEqual(v *Version, c *constraint) bool {
|
||||||
|
if c.dirty {
|
||||||
|
|
||||||
|
// If there is a pre-release on the version but the constraint isn't looking
|
||||||
|
// for them assume that pre-releases are not compatible. See issue 21 for
|
||||||
|
// more details.
|
||||||
|
if v.Prerelease() != "" && c.con.Prerelease() == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.con.Major() != v.Major() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if c.con.Minor() != v.Minor() && !c.minorDirty {
|
||||||
|
return true
|
||||||
|
} else if c.minorDirty {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return !v.Equal(c.con)
|
||||||
|
}
|
||||||
|
|
||||||
|
func constraintGreaterThan(v *Version, c *constraint) bool {
|
||||||
|
|
||||||
|
// An edge case the constraint is 0.0.0 and the version is 0.0.0-someprerelease
|
||||||
|
// exists. This that case.
|
||||||
|
if !isNonZero(c.con) && isNonZero(v) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there is a pre-release on the version but the constraint isn't looking
|
||||||
|
// for them assume that pre-releases are not compatible. See issue 21 for
|
||||||
|
// more details.
|
||||||
|
if v.Prerelease() != "" && c.con.Prerelease() == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return v.Compare(c.con) == 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func constraintLessThan(v *Version, c *constraint) bool {
|
||||||
|
// If there is a pre-release on the version but the constraint isn't looking
|
||||||
|
// for them assume that pre-releases are not compatible. See issue 21 for
|
||||||
|
// more details.
|
||||||
|
if v.Prerelease() != "" && c.con.Prerelease() == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if !c.dirty {
|
||||||
|
return v.Compare(c.con) < 0
|
||||||
|
}
|
||||||
|
|
||||||
|
if v.Major() > c.con.Major() {
|
||||||
|
return false
|
||||||
|
} else if v.Minor() > c.con.Minor() && !c.minorDirty {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func constraintGreaterThanEqual(v *Version, c *constraint) bool {
|
||||||
|
// An edge case the constraint is 0.0.0 and the version is 0.0.0-someprerelease
|
||||||
|
// exists. This that case.
|
||||||
|
if !isNonZero(c.con) && isNonZero(v) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there is a pre-release on the version but the constraint isn't looking
|
||||||
|
// for them assume that pre-releases are not compatible. See issue 21 for
|
||||||
|
// more details.
|
||||||
|
if v.Prerelease() != "" && c.con.Prerelease() == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return v.Compare(c.con) >= 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func constraintLessThanEqual(v *Version, c *constraint) bool {
|
||||||
|
// If there is a pre-release on the version but the constraint isn't looking
|
||||||
|
// for them assume that pre-releases are not compatible. See issue 21 for
|
||||||
|
// more details.
|
||||||
|
if v.Prerelease() != "" && c.con.Prerelease() == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if !c.dirty {
|
||||||
|
return v.Compare(c.con) <= 0
|
||||||
|
}
|
||||||
|
|
||||||
|
if v.Major() > c.con.Major() {
|
||||||
|
return false
|
||||||
|
} else if v.Minor() > c.con.Minor() && !c.minorDirty {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~*, ~>* --> >= 0.0.0 (any)
|
||||||
|
// ~2, ~2.x, ~2.x.x, ~>2, ~>2.x ~>2.x.x --> >=2.0.0, <3.0.0
|
||||||
|
// ~2.0, ~2.0.x, ~>2.0, ~>2.0.x --> >=2.0.0, <2.1.0
|
||||||
|
// ~1.2, ~1.2.x, ~>1.2, ~>1.2.x --> >=1.2.0, <1.3.0
|
||||||
|
// ~1.2.3, ~>1.2.3 --> >=1.2.3, <1.3.0
|
||||||
|
// ~1.2.0, ~>1.2.0 --> >=1.2.0, <1.3.0
|
||||||
|
func constraintTilde(v *Version, c *constraint) bool {
|
||||||
|
// If there is a pre-release on the version but the constraint isn't looking
|
||||||
|
// for them assume that pre-releases are not compatible. See issue 21 for
|
||||||
|
// more details.
|
||||||
|
if v.Prerelease() != "" && c.con.Prerelease() == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if v.LessThan(c.con) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// ~0.0.0 is a special case where all constraints are accepted. It's
|
||||||
|
// equivalent to >= 0.0.0.
|
||||||
|
if c.con.Major() == 0 && c.con.Minor() == 0 && c.con.Patch() == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if v.Major() != c.con.Major() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if v.Minor() != c.con.Minor() && !c.minorDirty {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// When there is a .x (dirty) status it automatically opts in to ~. Otherwise
|
||||||
|
// it's a straight =
|
||||||
|
func constraintTildeOrEqual(v *Version, c *constraint) bool {
|
||||||
|
// If there is a pre-release on the version but the constraint isn't looking
|
||||||
|
// for them assume that pre-releases are not compatible. See issue 21 for
|
||||||
|
// more details.
|
||||||
|
if v.Prerelease() != "" && c.con.Prerelease() == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.dirty {
|
||||||
|
c.msg = constraintMsg["~"]
|
||||||
|
return constraintTilde(v, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
return v.Equal(c.con)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ^* --> (any)
|
||||||
|
// ^2, ^2.x, ^2.x.x --> >=2.0.0, <3.0.0
|
||||||
|
// ^2.0, ^2.0.x --> >=2.0.0, <3.0.0
|
||||||
|
// ^1.2, ^1.2.x --> >=1.2.0, <2.0.0
|
||||||
|
// ^1.2.3 --> >=1.2.3, <2.0.0
|
||||||
|
// ^1.2.0 --> >=1.2.0, <2.0.0
|
||||||
|
func constraintCaret(v *Version, c *constraint) bool {
|
||||||
|
// If there is a pre-release on the version but the constraint isn't looking
|
||||||
|
// for them assume that pre-releases are not compatible. See issue 21 for
|
||||||
|
// more details.
|
||||||
|
if v.Prerelease() != "" && c.con.Prerelease() == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if v.LessThan(c.con) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if v.Major() != c.con.Major() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
var constraintRangeRegex *regexp.Regexp
|
||||||
|
|
||||||
|
const cvRegex string = `v?([0-9|x|X|\*]+)(\.[0-9|x|X|\*]+)?(\.[0-9|x|X|\*]+)?` +
|
||||||
|
`(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?` +
|
||||||
|
`(\+([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?`
|
||||||
|
|
||||||
|
func isX(x string) bool {
|
||||||
|
switch x {
|
||||||
|
case "x", "*", "X":
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func rewriteRange(i string) string {
|
||||||
|
m := constraintRangeRegex.FindAllStringSubmatch(i, -1)
|
||||||
|
if m == nil {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
o := i
|
||||||
|
for _, v := range m {
|
||||||
|
t := fmt.Sprintf(">= %s, <= %s", v[1], v[11])
|
||||||
|
o = strings.Replace(o, v[0], t, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detect if a version is not zero (0.0.0)
|
||||||
|
func isNonZero(v *Version) bool {
|
||||||
|
if v.Major() != 0 || v.Minor() != 0 || v.Patch() != 0 || v.Prerelease() != "" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
452
vendor/github.com/Masterminds/semver/constraints_test.go
generated
vendored
Normal file
452
vendor/github.com/Masterminds/semver/constraints_test.go
generated
vendored
Normal file
@@ -0,0 +1,452 @@
|
|||||||
|
package semver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestParseConstraint(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
in string
|
||||||
|
f cfunc
|
||||||
|
v string
|
||||||
|
err bool
|
||||||
|
}{
|
||||||
|
{">= 1.2", constraintGreaterThanEqual, "1.2.0", false},
|
||||||
|
{"1.0", constraintTildeOrEqual, "1.0.0", false},
|
||||||
|
{"foo", nil, "", true},
|
||||||
|
{"<= 1.2", constraintLessThanEqual, "1.2.0", false},
|
||||||
|
{"=< 1.2", constraintLessThanEqual, "1.2.0", false},
|
||||||
|
{"=> 1.2", constraintGreaterThanEqual, "1.2.0", false},
|
||||||
|
{"v1.2", constraintTildeOrEqual, "1.2.0", false},
|
||||||
|
{"=1.5", constraintTildeOrEqual, "1.5.0", false},
|
||||||
|
{"> 1.3", constraintGreaterThan, "1.3.0", false},
|
||||||
|
{"< 1.4.1", constraintLessThan, "1.4.1", false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
c, err := parseConstraint(tc.in)
|
||||||
|
if tc.err && err == nil {
|
||||||
|
t.Errorf("Expected error for %s didn't occur", tc.in)
|
||||||
|
} else if !tc.err && err != nil {
|
||||||
|
t.Errorf("Unexpected error for %s", tc.in)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If an error was expected continue the loop and don't try the other
|
||||||
|
// tests as they will cause errors.
|
||||||
|
if tc.err {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if tc.v != c.con.String() {
|
||||||
|
t.Errorf("Incorrect version found on %s", tc.in)
|
||||||
|
}
|
||||||
|
|
||||||
|
f1 := reflect.ValueOf(tc.f)
|
||||||
|
f2 := reflect.ValueOf(c.function)
|
||||||
|
if f1 != f2 {
|
||||||
|
t.Errorf("Wrong constraint found for %s", tc.in)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConstraintCheck(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
constraint string
|
||||||
|
version string
|
||||||
|
check bool
|
||||||
|
}{
|
||||||
|
{"= 2.0", "1.2.3", false},
|
||||||
|
{"= 2.0", "2.0.0", true},
|
||||||
|
{"4.1", "4.1.0", true},
|
||||||
|
{"!=4.1", "4.1.0", false},
|
||||||
|
{"!=4.1", "5.1.0", true},
|
||||||
|
{">1.1", "4.1.0", true},
|
||||||
|
{">1.1", "1.1.0", false},
|
||||||
|
{"<1.1", "0.1.0", true},
|
||||||
|
{"<1.1", "1.1.0", false},
|
||||||
|
{"<1.1", "1.1.1", false},
|
||||||
|
{">=1.1", "4.1.0", true},
|
||||||
|
{">=1.1", "1.1.0", true},
|
||||||
|
{">=1.1", "0.0.9", false},
|
||||||
|
{"<=1.1", "0.1.0", true},
|
||||||
|
{"<=1.1", "1.1.0", true},
|
||||||
|
{"<=1.1", "1.1.1", false},
|
||||||
|
{">0", "0.0.1-alpha", true},
|
||||||
|
{">=0", "0.0.1-alpha", true},
|
||||||
|
{">0", "0", false},
|
||||||
|
{">=0", "0", true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
c, err := parseConstraint(tc.constraint)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("err: %s", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
v, err := NewVersion(tc.version)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("err: %s", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
a := c.check(v)
|
||||||
|
if a != tc.check {
|
||||||
|
t.Errorf("Constraint %q failing with %q", tc.constraint, tc.version)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNewConstraint(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
input string
|
||||||
|
ors int
|
||||||
|
count int
|
||||||
|
err bool
|
||||||
|
}{
|
||||||
|
{">= 1.1", 1, 1, false},
|
||||||
|
{"2.0", 1, 1, false},
|
||||||
|
{"v2.3.5-20161202202307-sha.e8fc5e5", 1, 1, false},
|
||||||
|
{">= bar", 0, 0, true},
|
||||||
|
{">= 1.2.3, < 2.0", 1, 2, false},
|
||||||
|
{">= 1.2.3, < 2.0 || => 3.0, < 4", 2, 2, false},
|
||||||
|
|
||||||
|
// The 3 - 4 should be broken into 2 by the range rewriting
|
||||||
|
{"3 - 4 || => 3.0, < 4", 2, 2, false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
v, err := NewConstraint(tc.input)
|
||||||
|
if tc.err && err == nil {
|
||||||
|
t.Errorf("expected but did not get error for: %s", tc.input)
|
||||||
|
continue
|
||||||
|
} else if !tc.err && err != nil {
|
||||||
|
t.Errorf("unexpectederror for input %s: %s", tc.input, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if tc.err {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
l := len(v.constraints)
|
||||||
|
if tc.ors != l {
|
||||||
|
t.Errorf("Expected %s to have %d ORs but got %d",
|
||||||
|
tc.input, tc.ors, l)
|
||||||
|
}
|
||||||
|
|
||||||
|
l = len(v.constraints[0])
|
||||||
|
if tc.count != l {
|
||||||
|
t.Errorf("Expected %s to have %d constraints but got %d",
|
||||||
|
tc.input, tc.count, l)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConstraintsCheck(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
constraint string
|
||||||
|
version string
|
||||||
|
check bool
|
||||||
|
}{
|
||||||
|
{"*", "1.2.3", true},
|
||||||
|
{"~0.0.0", "1.2.3", true},
|
||||||
|
{"= 2.0", "1.2.3", false},
|
||||||
|
{"= 2.0", "2.0.0", true},
|
||||||
|
{"4.1", "4.1.0", true},
|
||||||
|
{"4.1.x", "4.1.3", true},
|
||||||
|
{"1.x", "1.4", true},
|
||||||
|
{"!=4.1", "4.1.0", false},
|
||||||
|
{"!=4.1-alpha", "4.1.0-alpha", false},
|
||||||
|
{"!=4.1-alpha", "4.1.0", true},
|
||||||
|
{"!=4.1", "5.1.0", true},
|
||||||
|
{"!=4.x", "5.1.0", true},
|
||||||
|
{"!=4.x", "4.1.0", false},
|
||||||
|
{"!=4.1.x", "4.2.0", true},
|
||||||
|
{"!=4.2.x", "4.2.3", false},
|
||||||
|
{">1.1", "4.1.0", true},
|
||||||
|
{">1.1", "1.1.0", false},
|
||||||
|
{"<1.1", "0.1.0", true},
|
||||||
|
{"<1.1", "1.1.0", false},
|
||||||
|
{"<1.1", "1.1.1", false},
|
||||||
|
{"<1.x", "1.1.1", true},
|
||||||
|
{"<1.x", "2.1.1", false},
|
||||||
|
{"<1.1.x", "1.2.1", false},
|
||||||
|
{"<1.1.x", "1.1.500", true},
|
||||||
|
{"<1.2.x", "1.1.1", true},
|
||||||
|
{">=1.1", "4.1.0", true},
|
||||||
|
{">=1.1", "4.1.0-beta", false},
|
||||||
|
{">=1.1", "1.1.0", true},
|
||||||
|
{">=1.1", "0.0.9", false},
|
||||||
|
{"<=1.1", "0.1.0", true},
|
||||||
|
{"<=1.1", "0.1.0-alpha", false},
|
||||||
|
{"<=1.1-a", "0.1.0-alpha", true},
|
||||||
|
{"<=1.1", "1.1.0", true},
|
||||||
|
{"<=1.x", "1.1.0", true},
|
||||||
|
{"<=2.x", "3.1.0", false},
|
||||||
|
{"<=1.1", "1.1.1", false},
|
||||||
|
{"<=1.1.x", "1.2.500", false},
|
||||||
|
{">1.1, <2", "1.1.1", true},
|
||||||
|
{">1.1, <3", "4.3.2", false},
|
||||||
|
{">=1.1, <2, !=1.2.3", "1.2.3", false},
|
||||||
|
{">=1.1, <2, !=1.2.3 || > 3", "3.1.2", true},
|
||||||
|
{">=1.1, <2, !=1.2.3 || >= 3", "3.0.0", true},
|
||||||
|
{">=1.1, <2, !=1.2.3 || > 3", "3.0.0", false},
|
||||||
|
{">=1.1, <2, !=1.2.3 || > 3", "1.2.3", false},
|
||||||
|
{"1.1 - 2", "1.1.1", true},
|
||||||
|
{"1.1-3", "4.3.2", false},
|
||||||
|
{"^1.1", "1.1.1", true},
|
||||||
|
{"^1.1", "4.3.2", false},
|
||||||
|
{"^1.x", "1.1.1", true},
|
||||||
|
{"^2.x", "1.1.1", false},
|
||||||
|
{"^1.x", "2.1.1", false},
|
||||||
|
{"^1.x", "1.1.1-beta1", false},
|
||||||
|
{"^1.1.2-alpha", "1.2.1-beta1", true},
|
||||||
|
{"^1.2.x-alpha", "1.1.1-beta1", false},
|
||||||
|
{"~*", "2.1.1", true},
|
||||||
|
{"~1.x", "2.1.1", false},
|
||||||
|
{"~1.x", "1.3.5", true},
|
||||||
|
{"~1.x", "1.4", true},
|
||||||
|
{"~1.1", "1.1.1", true},
|
||||||
|
{"~1.1", "1.1.1-alpha", false},
|
||||||
|
{"~1.1-alpha", "1.1.1-beta", true},
|
||||||
|
{"~1.1.1-beta", "1.1.1-alpha", false},
|
||||||
|
{"~1.1.1-beta", "1.1.1", true},
|
||||||
|
{"~1.2.3", "1.2.5", true},
|
||||||
|
{"~1.2.3", "1.2.2", false},
|
||||||
|
{"~1.2.3", "1.3.2", false},
|
||||||
|
{"~1.1", "1.2.3", false},
|
||||||
|
{"~1.3", "2.4.5", false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
c, err := NewConstraint(tc.constraint)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("err: %s", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
v, err := NewVersion(tc.version)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("err: %s", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
a := c.Check(v)
|
||||||
|
if a != tc.check {
|
||||||
|
t.Errorf("Constraint '%s' failing with '%s'", tc.constraint, tc.version)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRewriteRange(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
c string
|
||||||
|
nc string
|
||||||
|
}{
|
||||||
|
{"2 - 3", ">= 2, <= 3"},
|
||||||
|
{"2 - 3, 2 - 3", ">= 2, <= 3,>= 2, <= 3"},
|
||||||
|
{"2 - 3, 4.0.0 - 5.1", ">= 2, <= 3,>= 4.0.0, <= 5.1"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
o := rewriteRange(tc.c)
|
||||||
|
|
||||||
|
if o != tc.nc {
|
||||||
|
t.Errorf("Range %s rewritten incorrectly as '%s'", tc.c, o)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsX(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
t string
|
||||||
|
c bool
|
||||||
|
}{
|
||||||
|
{"A", false},
|
||||||
|
{"%", false},
|
||||||
|
{"X", true},
|
||||||
|
{"x", true},
|
||||||
|
{"*", true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
a := isX(tc.t)
|
||||||
|
if a != tc.c {
|
||||||
|
t.Errorf("Function isX error on %s", tc.t)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConstraintsValidate(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
constraint string
|
||||||
|
version string
|
||||||
|
check bool
|
||||||
|
}{
|
||||||
|
{"*", "1.2.3", true},
|
||||||
|
{"~0.0.0", "1.2.3", true},
|
||||||
|
{"= 2.0", "1.2.3", false},
|
||||||
|
{"= 2.0", "2.0.0", true},
|
||||||
|
{"4.1", "4.1.0", true},
|
||||||
|
{"4.1.x", "4.1.3", true},
|
||||||
|
{"1.x", "1.4", true},
|
||||||
|
{"!=4.1", "4.1.0", false},
|
||||||
|
{"!=4.1", "5.1.0", true},
|
||||||
|
{"!=4.x", "5.1.0", true},
|
||||||
|
{"!=4.x", "4.1.0", false},
|
||||||
|
{"!=4.1.x", "4.2.0", true},
|
||||||
|
{"!=4.2.x", "4.2.3", false},
|
||||||
|
{">1.1", "4.1.0", true},
|
||||||
|
{">1.1", "1.1.0", false},
|
||||||
|
{"<1.1", "0.1.0", true},
|
||||||
|
{"<1.1", "1.1.0", false},
|
||||||
|
{"<1.1", "1.1.1", false},
|
||||||
|
{"<1.x", "1.1.1", true},
|
||||||
|
{"<1.x", "2.1.1", false},
|
||||||
|
{"<1.1.x", "1.2.1", false},
|
||||||
|
{"<1.1.x", "1.1.500", true},
|
||||||
|
{"<1.2.x", "1.1.1", true},
|
||||||
|
{">=1.1", "4.1.0", true},
|
||||||
|
{">=1.1", "1.1.0", true},
|
||||||
|
{">=1.1", "0.0.9", false},
|
||||||
|
{"<=1.1", "0.1.0", true},
|
||||||
|
{"<=1.1", "1.1.0", true},
|
||||||
|
{"<=1.x", "1.1.0", true},
|
||||||
|
{"<=2.x", "3.1.0", false},
|
||||||
|
{"<=1.1", "1.1.1", false},
|
||||||
|
{"<=1.1.x", "1.2.500", false},
|
||||||
|
{">1.1, <2", "1.1.1", true},
|
||||||
|
{">1.1, <3", "4.3.2", false},
|
||||||
|
{">=1.1, <2, !=1.2.3", "1.2.3", false},
|
||||||
|
{">=1.1, <2, !=1.2.3 || > 3", "3.1.2", true},
|
||||||
|
{">=1.1, <2, !=1.2.3 || >= 3", "3.0.0", true},
|
||||||
|
{">=1.1, <2, !=1.2.3 || > 3", "3.0.0", false},
|
||||||
|
{">=1.1, <2, !=1.2.3 || > 3", "1.2.3", false},
|
||||||
|
{"1.1 - 2", "1.1.1", true},
|
||||||
|
{"1.1-3", "4.3.2", false},
|
||||||
|
{"^1.1", "1.1.1", true},
|
||||||
|
{"^1.1", "1.1.1-alpha", false},
|
||||||
|
{"^1.1.1-alpha", "1.1.1-beta", true},
|
||||||
|
{"^1.1.1-beta", "1.1.1-alpha", false},
|
||||||
|
{"^1.1", "4.3.2", false},
|
||||||
|
{"^1.x", "1.1.1", true},
|
||||||
|
{"^2.x", "1.1.1", false},
|
||||||
|
{"^1.x", "2.1.1", false},
|
||||||
|
{"~*", "2.1.1", true},
|
||||||
|
{"~1.x", "2.1.1", false},
|
||||||
|
{"~1.x", "1.3.5", true},
|
||||||
|
{"~1.x", "1.3.5-beta", false},
|
||||||
|
{"~1.3.6-alpha", "1.3.5-beta", false},
|
||||||
|
{"~1.3.5-alpha", "1.3.5-beta", true},
|
||||||
|
{"~1.3.5-beta", "1.3.5-alpha", false},
|
||||||
|
{"~1.x", "1.4", true},
|
||||||
|
{"~1.1", "1.1.1", true},
|
||||||
|
{"~1.2.3", "1.2.5", true},
|
||||||
|
{"~1.2.3", "1.2.2", false},
|
||||||
|
{"~1.2.3", "1.3.2", false},
|
||||||
|
{"~1.1", "1.2.3", false},
|
||||||
|
{"~1.3", "2.4.5", false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
c, err := NewConstraint(tc.constraint)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("err: %s", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
v, err := NewVersion(tc.version)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("err: %s", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
a, msgs := c.Validate(v)
|
||||||
|
if a != tc.check {
|
||||||
|
t.Errorf("Constraint '%s' failing with '%s'", tc.constraint, tc.version)
|
||||||
|
} else if !a && len(msgs) == 0 {
|
||||||
|
t.Errorf("%q failed with %q but no errors returned", tc.constraint, tc.version)
|
||||||
|
}
|
||||||
|
|
||||||
|
// if a == false {
|
||||||
|
// for _, m := range msgs {
|
||||||
|
// t.Errorf("%s", m)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
v, err := NewVersion("1.2.3")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
c, err := NewConstraint("!= 1.2.5, ^2, <= 1.1.x")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("err: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, msgs := c.Validate(v)
|
||||||
|
if len(msgs) != 2 {
|
||||||
|
t.Error("Invalid number of validations found")
|
||||||
|
}
|
||||||
|
e := msgs[0].Error()
|
||||||
|
if e != "1.2.3 does not have same major version as 2" {
|
||||||
|
t.Error("Did not get expected message: 1.2.3 does not have same major version as 2")
|
||||||
|
}
|
||||||
|
e = msgs[1].Error()
|
||||||
|
if e != "1.2.3 is greater than 1.1.x" {
|
||||||
|
t.Error("Did not get expected message: 1.2.3 is greater than 1.1.x")
|
||||||
|
}
|
||||||
|
|
||||||
|
tests2 := []struct {
|
||||||
|
constraint, version, msg string
|
||||||
|
}{
|
||||||
|
{"= 2.0", "1.2.3", "1.2.3 is not equal to 2.0"},
|
||||||
|
{"!=4.1", "4.1.0", "4.1.0 is equal to 4.1"},
|
||||||
|
{"!=4.x", "4.1.0", "4.1.0 is equal to 4.x"},
|
||||||
|
{"!=4.2.x", "4.2.3", "4.2.3 is equal to 4.2.x"},
|
||||||
|
{">1.1", "1.1.0", "1.1.0 is less than or equal to 1.1"},
|
||||||
|
{"<1.1", "1.1.0", "1.1.0 is greater than or equal to 1.1"},
|
||||||
|
{"<1.1", "1.1.1", "1.1.1 is greater than or equal to 1.1"},
|
||||||
|
{"<1.x", "2.1.1", "2.1.1 is greater than or equal to 1.x"},
|
||||||
|
{"<1.1.x", "1.2.1", "1.2.1 is greater than or equal to 1.1.x"},
|
||||||
|
{">=1.1", "0.0.9", "0.0.9 is less than 1.1"},
|
||||||
|
{"<=2.x", "3.1.0", "3.1.0 is greater than 2.x"},
|
||||||
|
{"<=1.1", "1.1.1", "1.1.1 is greater than 1.1"},
|
||||||
|
{"<=1.1.x", "1.2.500", "1.2.500 is greater than 1.1.x"},
|
||||||
|
{">1.1, <3", "4.3.2", "4.3.2 is greater than or equal to 3"},
|
||||||
|
{">=1.1, <2, !=1.2.3", "1.2.3", "1.2.3 is equal to 1.2.3"},
|
||||||
|
{">=1.1, <2, !=1.2.3 || > 3", "3.0.0", "3.0.0 is greater than or equal to 2"},
|
||||||
|
{">=1.1, <2, !=1.2.3 || > 3", "1.2.3", "1.2.3 is equal to 1.2.3"},
|
||||||
|
{"1.1 - 3", "4.3.2", "4.3.2 is greater than 3"},
|
||||||
|
{"^1.1", "4.3.2", "4.3.2 does not have same major version as 1.1"},
|
||||||
|
{"^2.x", "1.1.1", "1.1.1 does not have same major version as 2.x"},
|
||||||
|
{"^1.x", "2.1.1", "2.1.1 does not have same major version as 1.x"},
|
||||||
|
{"~1.x", "2.1.1", "2.1.1 does not have same major and minor version as 1.x"},
|
||||||
|
{"~1.2.3", "1.2.2", "1.2.2 does not have same major and minor version as 1.2.3"},
|
||||||
|
{"~1.2.3", "1.3.2", "1.3.2 does not have same major and minor version as 1.2.3"},
|
||||||
|
{"~1.1", "1.2.3", "1.2.3 does not have same major and minor version as 1.1"},
|
||||||
|
{"~1.3", "2.4.5", "2.4.5 does not have same major and minor version as 1.3"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests2 {
|
||||||
|
c, err := NewConstraint(tc.constraint)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("err: %s", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
v, err := NewVersion(tc.version)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("err: %s", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
_, msgs := c.Validate(v)
|
||||||
|
e := msgs[0].Error()
|
||||||
|
if e != tc.msg {
|
||||||
|
t.Errorf("Did not get expected message %q: %s", tc.msg, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
115
vendor/github.com/Masterminds/semver/doc.go
generated
vendored
Normal file
115
vendor/github.com/Masterminds/semver/doc.go
generated
vendored
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
/*
|
||||||
|
Package semver provides the ability to work with Semantic Versions (http://semver.org) in Go.
|
||||||
|
|
||||||
|
Specifically it provides the ability to:
|
||||||
|
|
||||||
|
* Parse semantic versions
|
||||||
|
* Sort semantic versions
|
||||||
|
* Check if a semantic version fits within a set of constraints
|
||||||
|
* Optionally work with a `v` prefix
|
||||||
|
|
||||||
|
Parsing Semantic Versions
|
||||||
|
|
||||||
|
To parse a semantic version use the `NewVersion` function. For example,
|
||||||
|
|
||||||
|
v, err := semver.NewVersion("1.2.3-beta.1+build345")
|
||||||
|
|
||||||
|
If there is an error the version wasn't parseable. The version object has methods
|
||||||
|
to get the parts of the version, compare it to other versions, convert the
|
||||||
|
version back into a string, and get the original string. For more details
|
||||||
|
please see the documentation at https://godoc.org/github.com/Masterminds/semver.
|
||||||
|
|
||||||
|
Sorting Semantic Versions
|
||||||
|
|
||||||
|
A set of versions can be sorted using the `sort` package from the standard library.
|
||||||
|
For example,
|
||||||
|
|
||||||
|
raw := []string{"1.2.3", "1.0", "1.3", "2", "0.4.2",}
|
||||||
|
vs := make([]*semver.Version, len(raw))
|
||||||
|
for i, r := range raw {
|
||||||
|
v, err := semver.NewVersion(r)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error parsing version: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
vs[i] = v
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Sort(semver.Collection(vs))
|
||||||
|
|
||||||
|
Checking Version Constraints
|
||||||
|
|
||||||
|
Checking a version against version constraints is one of the most featureful
|
||||||
|
parts of the package.
|
||||||
|
|
||||||
|
c, err := semver.NewConstraint(">= 1.2.3")
|
||||||
|
if err != nil {
|
||||||
|
// Handle constraint not being parseable.
|
||||||
|
}
|
||||||
|
|
||||||
|
v, _ := semver.NewVersion("1.3")
|
||||||
|
if err != nil {
|
||||||
|
// Handle version not being parseable.
|
||||||
|
}
|
||||||
|
// Check if the version meets the constraints. The a variable will be true.
|
||||||
|
a := c.Check(v)
|
||||||
|
|
||||||
|
Basic Comparisons
|
||||||
|
|
||||||
|
There are two elements to the comparisons. First, a comparison string is a list
|
||||||
|
of comma separated and comparisons. These are then separated by || separated or
|
||||||
|
comparisons. For example, `">= 1.2, < 3.0.0 || >= 4.2.3"` is looking for a
|
||||||
|
comparison that's greater than or equal to 1.2 and less than 3.0.0 or is
|
||||||
|
greater than or equal to 4.2.3.
|
||||||
|
|
||||||
|
The basic comparisons are:
|
||||||
|
|
||||||
|
* `=`: equal (aliased to no operator)
|
||||||
|
* `!=`: not equal
|
||||||
|
* `>`: greater than
|
||||||
|
* `<`: less than
|
||||||
|
* `>=`: greater than or equal to
|
||||||
|
* `<=`: less than or equal to
|
||||||
|
|
||||||
|
Hyphen Range Comparisons
|
||||||
|
|
||||||
|
There are multiple methods to handle ranges and the first is hyphens ranges.
|
||||||
|
These look like:
|
||||||
|
|
||||||
|
* `1.2 - 1.4.5` which is equivalent to `>= 1.2, <= 1.4.5`
|
||||||
|
* `2.3.4 - 4.5` which is equivalent to `>= 2.3.4, <= 4.5`
|
||||||
|
|
||||||
|
Wildcards In Comparisons
|
||||||
|
|
||||||
|
The `x`, `X`, and `*` characters can be used as a wildcard character. This works
|
||||||
|
for all comparison operators. When used on the `=` operator it falls
|
||||||
|
back to the pack level comparison (see tilde below). For example,
|
||||||
|
|
||||||
|
* `1.2.x` is equivalent to `>= 1.2.0, < 1.3.0`
|
||||||
|
* `>= 1.2.x` is equivalent to `>= 1.2.0`
|
||||||
|
* `<= 2.x` is equivalent to `<= 3`
|
||||||
|
* `*` is equivalent to `>= 0.0.0`
|
||||||
|
|
||||||
|
Tilde Range Comparisons (Patch)
|
||||||
|
|
||||||
|
The tilde (`~`) comparison operator is for patch level ranges when a minor
|
||||||
|
version is specified and major level changes when the minor number is missing.
|
||||||
|
For example,
|
||||||
|
|
||||||
|
* `~1.2.3` is equivalent to `>= 1.2.3, < 1.3.0`
|
||||||
|
* `~1` is equivalent to `>= 1, < 2`
|
||||||
|
* `~2.3` is equivalent to `>= 2.3, < 2.4`
|
||||||
|
* `~1.2.x` is equivalent to `>= 1.2.0, < 1.3.0`
|
||||||
|
* `~1.x` is equivalent to `>= 1, < 2`
|
||||||
|
|
||||||
|
Caret Range Comparisons (Major)
|
||||||
|
|
||||||
|
The caret (`^`) comparison operator is for major level changes. This is useful
|
||||||
|
when comparisons of API versions as a major change is API breaking. For example,
|
||||||
|
|
||||||
|
* `^1.2.3` is equivalent to `>= 1.2.3, < 2.0.0`
|
||||||
|
* `^1.2.x` is equivalent to `>= 1.2.0, < 2.0.0`
|
||||||
|
* `^2.3` is equivalent to `>= 2.3, < 3`
|
||||||
|
* `^2.x` is equivalent to `>= 2.0.0, < 3`
|
||||||
|
*/
|
||||||
|
package semver
|
375
vendor/github.com/Masterminds/semver/version.go
generated
vendored
Normal file
375
vendor/github.com/Masterminds/semver/version.go
generated
vendored
Normal file
@@ -0,0 +1,375 @@
|
|||||||
|
package semver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The compiled version of the regex created at init() is cached here so it
|
||||||
|
// only needs to be created once.
|
||||||
|
var versionRegex *regexp.Regexp
|
||||||
|
var validPrereleaseRegex *regexp.Regexp
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrInvalidSemVer is returned a version is found to be invalid when
|
||||||
|
// being parsed.
|
||||||
|
ErrInvalidSemVer = errors.New("Invalid Semantic Version")
|
||||||
|
|
||||||
|
// ErrInvalidMetadata is returned when the metadata is an invalid format
|
||||||
|
ErrInvalidMetadata = errors.New("Invalid Metadata string")
|
||||||
|
|
||||||
|
// ErrInvalidPrerelease is returned when the pre-release is an invalid format
|
||||||
|
ErrInvalidPrerelease = errors.New("Invalid Prerelease string")
|
||||||
|
)
|
||||||
|
|
||||||
|
// SemVerRegex is the regular expression used to parse a semantic version.
|
||||||
|
const SemVerRegex string = `v?([0-9]+)(\.[0-9]+)?(\.[0-9]+)?` +
|
||||||
|
`(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?` +
|
||||||
|
`(\+([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?`
|
||||||
|
|
||||||
|
// ValidPrerelease is the regular expression which validates
|
||||||
|
// both prerelease and metadata values.
|
||||||
|
const ValidPrerelease string = `^([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*)`
|
||||||
|
|
||||||
|
// Version represents a single semantic version.
|
||||||
|
type Version struct {
|
||||||
|
major, minor, patch int64
|
||||||
|
pre string
|
||||||
|
metadata string
|
||||||
|
original string
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
versionRegex = regexp.MustCompile("^" + SemVerRegex + "$")
|
||||||
|
validPrereleaseRegex = regexp.MustCompile(ValidPrerelease)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewVersion parses a given version and returns an instance of Version or
|
||||||
|
// an error if unable to parse the version.
|
||||||
|
func NewVersion(v string) (*Version, error) {
|
||||||
|
m := versionRegex.FindStringSubmatch(v)
|
||||||
|
if m == nil {
|
||||||
|
return nil, ErrInvalidSemVer
|
||||||
|
}
|
||||||
|
|
||||||
|
sv := &Version{
|
||||||
|
metadata: m[8],
|
||||||
|
pre: m[5],
|
||||||
|
original: v,
|
||||||
|
}
|
||||||
|
|
||||||
|
var temp int64
|
||||||
|
temp, err := strconv.ParseInt(m[1], 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Error parsing version segment: %s", err)
|
||||||
|
}
|
||||||
|
sv.major = temp
|
||||||
|
|
||||||
|
if m[2] != "" {
|
||||||
|
temp, err = strconv.ParseInt(strings.TrimPrefix(m[2], "."), 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Error parsing version segment: %s", err)
|
||||||
|
}
|
||||||
|
sv.minor = temp
|
||||||
|
} else {
|
||||||
|
sv.minor = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
if m[3] != "" {
|
||||||
|
temp, err = strconv.ParseInt(strings.TrimPrefix(m[3], "."), 10, 32)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Error parsing version segment: %s", err)
|
||||||
|
}
|
||||||
|
sv.patch = temp
|
||||||
|
} else {
|
||||||
|
sv.patch = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return sv, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustParse parses a given version and panics on error.
|
||||||
|
func MustParse(v string) *Version {
|
||||||
|
sv, err := NewVersion(v)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return sv
|
||||||
|
}
|
||||||
|
|
||||||
|
// String converts a Version object to a string.
|
||||||
|
// Note, if the original version contained a leading v this version will not.
|
||||||
|
// See the Original() method to retrieve the original value. Semantic Versions
|
||||||
|
// don't contain a leading v per the spec. Instead it's optional on
|
||||||
|
// impelementation.
|
||||||
|
func (v *Version) String() string {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
fmt.Fprintf(&buf, "%d.%d.%d", v.major, v.minor, v.patch)
|
||||||
|
if v.pre != "" {
|
||||||
|
fmt.Fprintf(&buf, "-%s", v.pre)
|
||||||
|
}
|
||||||
|
if v.metadata != "" {
|
||||||
|
fmt.Fprintf(&buf, "+%s", v.metadata)
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Original returns the original value passed in to be parsed.
|
||||||
|
func (v *Version) Original() string {
|
||||||
|
return v.original
|
||||||
|
}
|
||||||
|
|
||||||
|
// Major returns the major version.
|
||||||
|
func (v *Version) Major() int64 {
|
||||||
|
return v.major
|
||||||
|
}
|
||||||
|
|
||||||
|
// Minor returns the minor version.
|
||||||
|
func (v *Version) Minor() int64 {
|
||||||
|
return v.minor
|
||||||
|
}
|
||||||
|
|
||||||
|
// Patch returns the patch version.
|
||||||
|
func (v *Version) Patch() int64 {
|
||||||
|
return v.patch
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prerelease returns the pre-release version.
|
||||||
|
func (v *Version) Prerelease() string {
|
||||||
|
return v.pre
|
||||||
|
}
|
||||||
|
|
||||||
|
// Metadata returns the metadata on the version.
|
||||||
|
func (v *Version) Metadata() string {
|
||||||
|
return v.metadata
|
||||||
|
}
|
||||||
|
|
||||||
|
// originalVPrefix returns the original 'v' prefix if any.
|
||||||
|
func (v *Version) originalVPrefix() string {
|
||||||
|
|
||||||
|
// Note, only lowercase v is supported as a prefix by the parser.
|
||||||
|
if v.original != "" && v.original[:1] == "v" {
|
||||||
|
return v.original[:1]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// IncPatch produces the next patch version.
|
||||||
|
// If the current version does not have prerelease/metadata information,
|
||||||
|
// it unsets metadata and prerelease values, increments patch number.
|
||||||
|
// If the current version has any of prerelease or metadata information,
|
||||||
|
// it unsets both values and keeps curent patch value
|
||||||
|
func (v Version) IncPatch() Version {
|
||||||
|
vNext := v
|
||||||
|
// according to http://semver.org/#spec-item-9
|
||||||
|
// Pre-release versions have a lower precedence than the associated normal version.
|
||||||
|
// according to http://semver.org/#spec-item-10
|
||||||
|
// Build metadata SHOULD be ignored when determining version precedence.
|
||||||
|
if v.pre != "" {
|
||||||
|
vNext.metadata = ""
|
||||||
|
vNext.pre = ""
|
||||||
|
} else {
|
||||||
|
vNext.metadata = ""
|
||||||
|
vNext.pre = ""
|
||||||
|
vNext.patch = v.patch + 1
|
||||||
|
}
|
||||||
|
vNext.original = v.originalVPrefix() + "" + vNext.String()
|
||||||
|
return vNext
|
||||||
|
}
|
||||||
|
|
||||||
|
// IncMinor produces the next minor version.
|
||||||
|
// Sets patch to 0.
|
||||||
|
// Increments minor number.
|
||||||
|
// Unsets metadata.
|
||||||
|
// Unsets prerelease status.
|
||||||
|
func (v Version) IncMinor() Version {
|
||||||
|
vNext := v
|
||||||
|
vNext.metadata = ""
|
||||||
|
vNext.pre = ""
|
||||||
|
vNext.patch = 0
|
||||||
|
vNext.minor = v.minor + 1
|
||||||
|
vNext.original = v.originalVPrefix() + "" + vNext.String()
|
||||||
|
return vNext
|
||||||
|
}
|
||||||
|
|
||||||
|
// IncMajor produces the next major version.
|
||||||
|
// Sets patch to 0.
|
||||||
|
// Sets minor to 0.
|
||||||
|
// Increments major number.
|
||||||
|
// Unsets metadata.
|
||||||
|
// Unsets prerelease status.
|
||||||
|
func (v Version) IncMajor() Version {
|
||||||
|
vNext := v
|
||||||
|
vNext.metadata = ""
|
||||||
|
vNext.pre = ""
|
||||||
|
vNext.patch = 0
|
||||||
|
vNext.minor = 0
|
||||||
|
vNext.major = v.major + 1
|
||||||
|
vNext.original = v.originalVPrefix() + "" + vNext.String()
|
||||||
|
return vNext
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetPrerelease defines the prerelease value.
|
||||||
|
// Value must not include the required 'hypen' prefix.
|
||||||
|
func (v Version) SetPrerelease(prerelease string) (Version, error) {
|
||||||
|
vNext := v
|
||||||
|
if len(prerelease) > 0 && !validPrereleaseRegex.MatchString(prerelease) {
|
||||||
|
return vNext, ErrInvalidPrerelease
|
||||||
|
}
|
||||||
|
vNext.pre = prerelease
|
||||||
|
vNext.original = v.originalVPrefix() + "" + vNext.String()
|
||||||
|
return vNext, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetMetadata defines metadata value.
|
||||||
|
// Value must not include the required 'plus' prefix.
|
||||||
|
func (v Version) SetMetadata(metadata string) (Version, error) {
|
||||||
|
vNext := v
|
||||||
|
if len(metadata) > 0 && !validPrereleaseRegex.MatchString(metadata) {
|
||||||
|
return vNext, ErrInvalidMetadata
|
||||||
|
}
|
||||||
|
vNext.metadata = metadata
|
||||||
|
vNext.original = v.originalVPrefix() + "" + vNext.String()
|
||||||
|
return vNext, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// LessThan tests if one version is less than another one.
|
||||||
|
func (v *Version) LessThan(o *Version) bool {
|
||||||
|
return v.Compare(o) < 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// GreaterThan tests if one version is greater than another one.
|
||||||
|
func (v *Version) GreaterThan(o *Version) bool {
|
||||||
|
return v.Compare(o) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Equal tests if two versions are equal to each other.
|
||||||
|
// Note, versions can be equal with different metadata since metadata
|
||||||
|
// is not considered part of the comparable version.
|
||||||
|
func (v *Version) Equal(o *Version) bool {
|
||||||
|
return v.Compare(o) == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compare compares this version to another one. It returns -1, 0, or 1 if
|
||||||
|
// the version smaller, equal, or larger than the other version.
|
||||||
|
//
|
||||||
|
// Versions are compared by X.Y.Z. Build metadata is ignored. Prerelease is
|
||||||
|
// lower than the version without a prerelease.
|
||||||
|
func (v *Version) Compare(o *Version) int {
|
||||||
|
// Compare the major, minor, and patch version for differences. If a
|
||||||
|
// difference is found return the comparison.
|
||||||
|
if d := compareSegment(v.Major(), o.Major()); d != 0 {
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
if d := compareSegment(v.Minor(), o.Minor()); d != 0 {
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
if d := compareSegment(v.Patch(), o.Patch()); d != 0 {
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
|
||||||
|
// At this point the major, minor, and patch versions are the same.
|
||||||
|
ps := v.pre
|
||||||
|
po := o.Prerelease()
|
||||||
|
|
||||||
|
if ps == "" && po == "" {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
if ps == "" {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
if po == "" {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
return comparePrerelease(ps, po)
|
||||||
|
}
|
||||||
|
|
||||||
|
func compareSegment(v, o int64) int {
|
||||||
|
if v < o {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
if v > o {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func comparePrerelease(v, o string) int {
|
||||||
|
|
||||||
|
// split the prelease versions by their part. The separator, per the spec,
|
||||||
|
// is a .
|
||||||
|
sparts := strings.Split(v, ".")
|
||||||
|
oparts := strings.Split(o, ".")
|
||||||
|
|
||||||
|
// Find the longer length of the parts to know how many loop iterations to
|
||||||
|
// go through.
|
||||||
|
slen := len(sparts)
|
||||||
|
olen := len(oparts)
|
||||||
|
|
||||||
|
l := slen
|
||||||
|
if olen > slen {
|
||||||
|
l = olen
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iterate over each part of the prereleases to compare the differences.
|
||||||
|
for i := 0; i < l; i++ {
|
||||||
|
// Since the lentgh of the parts can be different we need to create
|
||||||
|
// a placeholder. This is to avoid out of bounds issues.
|
||||||
|
stemp := ""
|
||||||
|
if i < slen {
|
||||||
|
stemp = sparts[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
otemp := ""
|
||||||
|
if i < olen {
|
||||||
|
otemp = oparts[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
d := comparePrePart(stemp, otemp)
|
||||||
|
if d != 0 {
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reaching here means two versions are of equal value but have different
|
||||||
|
// metadata (the part following a +). They are not identical in string form
|
||||||
|
// but the version comparison finds them to be equal.
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func comparePrePart(s, o string) int {
|
||||||
|
// Fastpath if they are equal
|
||||||
|
if s == o {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// When s or o are empty we can use the other in an attempt to determine
|
||||||
|
// the response.
|
||||||
|
if o == "" {
|
||||||
|
_, n := strconv.ParseInt(s, 10, 64)
|
||||||
|
if n != nil {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
if s == "" {
|
||||||
|
_, n := strconv.ParseInt(o, 10, 64)
|
||||||
|
if n != nil {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
if s > o {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
450
vendor/github.com/Masterminds/semver/version_test.go
generated
vendored
Normal file
450
vendor/github.com/Masterminds/semver/version_test.go
generated
vendored
Normal file
@@ -0,0 +1,450 @@
|
|||||||
|
package semver
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNewVersion(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
version string
|
||||||
|
err bool
|
||||||
|
}{
|
||||||
|
{"1.2.3", false},
|
||||||
|
{"v1.2.3", false},
|
||||||
|
{"1.0", false},
|
||||||
|
{"v1.0", false},
|
||||||
|
{"1", false},
|
||||||
|
{"v1", false},
|
||||||
|
{"1.2.beta", true},
|
||||||
|
{"v1.2.beta", true},
|
||||||
|
{"foo", true},
|
||||||
|
{"1.2-5", false},
|
||||||
|
{"v1.2-5", false},
|
||||||
|
{"1.2-beta.5", false},
|
||||||
|
{"v1.2-beta.5", false},
|
||||||
|
{"\n1.2", true},
|
||||||
|
{"\nv1.2", true},
|
||||||
|
{"1.2.0-x.Y.0+metadata", false},
|
||||||
|
{"v1.2.0-x.Y.0+metadata", false},
|
||||||
|
{"1.2.0-x.Y.0+metadata-width-hypen", false},
|
||||||
|
{"v1.2.0-x.Y.0+metadata-width-hypen", false},
|
||||||
|
{"1.2.3-rc1-with-hypen", false},
|
||||||
|
{"v1.2.3-rc1-with-hypen", false},
|
||||||
|
{"1.2.3.4", true},
|
||||||
|
{"v1.2.3.4", true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
_, err := NewVersion(tc.version)
|
||||||
|
if tc.err && err == nil {
|
||||||
|
t.Fatalf("expected error for version: %s", tc.version)
|
||||||
|
} else if !tc.err && err != nil {
|
||||||
|
t.Fatalf("error for version %s: %s", tc.version, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOriginal(t *testing.T) {
|
||||||
|
tests := []string{
|
||||||
|
"1.2.3",
|
||||||
|
"v1.2.3",
|
||||||
|
"1.0",
|
||||||
|
"v1.0",
|
||||||
|
"1",
|
||||||
|
"v1",
|
||||||
|
"1.2-5",
|
||||||
|
"v1.2-5",
|
||||||
|
"1.2-beta.5",
|
||||||
|
"v1.2-beta.5",
|
||||||
|
"1.2.0-x.Y.0+metadata",
|
||||||
|
"v1.2.0-x.Y.0+metadata",
|
||||||
|
"1.2.0-x.Y.0+metadata-width-hypen",
|
||||||
|
"v1.2.0-x.Y.0+metadata-width-hypen",
|
||||||
|
"1.2.3-rc1-with-hypen",
|
||||||
|
"v1.2.3-rc1-with-hypen",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
v, err := NewVersion(tc)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error parsing version %s", tc)
|
||||||
|
}
|
||||||
|
|
||||||
|
o := v.Original()
|
||||||
|
if o != tc {
|
||||||
|
t.Errorf("Error retrieving originl. Expected '%s' but got '%s'", tc, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParts(t *testing.T) {
|
||||||
|
v, err := NewVersion("1.2.3-beta.1+build.123")
|
||||||
|
if err != nil {
|
||||||
|
t.Error("Error parsing version 1.2.3-beta.1+build.123")
|
||||||
|
}
|
||||||
|
|
||||||
|
if v.Major() != 1 {
|
||||||
|
t.Error("Major() returning wrong value")
|
||||||
|
}
|
||||||
|
if v.Minor() != 2 {
|
||||||
|
t.Error("Minor() returning wrong value")
|
||||||
|
}
|
||||||
|
if v.Patch() != 3 {
|
||||||
|
t.Error("Patch() returning wrong value")
|
||||||
|
}
|
||||||
|
if v.Prerelease() != "beta.1" {
|
||||||
|
t.Error("Prerelease() returning wrong value")
|
||||||
|
}
|
||||||
|
if v.Metadata() != "build.123" {
|
||||||
|
t.Error("Metadata() returning wrong value")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestString(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
version string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{"1.2.3", "1.2.3"},
|
||||||
|
{"v1.2.3", "1.2.3"},
|
||||||
|
{"1.0", "1.0.0"},
|
||||||
|
{"v1.0", "1.0.0"},
|
||||||
|
{"1", "1.0.0"},
|
||||||
|
{"v1", "1.0.0"},
|
||||||
|
{"1.2-5", "1.2.0-5"},
|
||||||
|
{"v1.2-5", "1.2.0-5"},
|
||||||
|
{"1.2-beta.5", "1.2.0-beta.5"},
|
||||||
|
{"v1.2-beta.5", "1.2.0-beta.5"},
|
||||||
|
{"1.2.0-x.Y.0+metadata", "1.2.0-x.Y.0+metadata"},
|
||||||
|
{"v1.2.0-x.Y.0+metadata", "1.2.0-x.Y.0+metadata"},
|
||||||
|
{"1.2.0-x.Y.0+metadata-width-hypen", "1.2.0-x.Y.0+metadata-width-hypen"},
|
||||||
|
{"v1.2.0-x.Y.0+metadata-width-hypen", "1.2.0-x.Y.0+metadata-width-hypen"},
|
||||||
|
{"1.2.3-rc1-with-hypen", "1.2.3-rc1-with-hypen"},
|
||||||
|
{"v1.2.3-rc1-with-hypen", "1.2.3-rc1-with-hypen"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
v, err := NewVersion(tc.version)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error parsing version %s", tc)
|
||||||
|
}
|
||||||
|
|
||||||
|
s := v.String()
|
||||||
|
if s != tc.expected {
|
||||||
|
t.Errorf("Error generating string. Expected '%s' but got '%s'", tc.expected, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCompare(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
v1 string
|
||||||
|
v2 string
|
||||||
|
expected int
|
||||||
|
}{
|
||||||
|
{"1.2.3", "1.5.1", -1},
|
||||||
|
{"2.2.3", "1.5.1", 1},
|
||||||
|
{"2.2.3", "2.2.2", 1},
|
||||||
|
{"3.2-beta", "3.2-beta", 0},
|
||||||
|
{"1.3", "1.1.4", 1},
|
||||||
|
{"4.2", "4.2-beta", 1},
|
||||||
|
{"4.2-beta", "4.2", -1},
|
||||||
|
{"4.2-alpha", "4.2-beta", -1},
|
||||||
|
{"4.2-alpha", "4.2-alpha", 0},
|
||||||
|
{"4.2-beta.2", "4.2-beta.1", 1},
|
||||||
|
{"4.2-beta2", "4.2-beta1", 1},
|
||||||
|
{"4.2-beta", "4.2-beta.2", -1},
|
||||||
|
{"4.2-beta", "4.2-beta.foo", 1},
|
||||||
|
{"4.2-beta.2", "4.2-beta", 1},
|
||||||
|
{"4.2-beta.foo", "4.2-beta", -1},
|
||||||
|
{"1.2+bar", "1.2+baz", 0},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
v1, err := NewVersion(tc.v1)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error parsing version: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
v2, err := NewVersion(tc.v2)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error parsing version: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
a := v1.Compare(v2)
|
||||||
|
e := tc.expected
|
||||||
|
if a != e {
|
||||||
|
t.Errorf(
|
||||||
|
"Comparison of '%s' and '%s' failed. Expected '%d', got '%d'",
|
||||||
|
tc.v1, tc.v2, e, a,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLessThan(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
v1 string
|
||||||
|
v2 string
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{"1.2.3", "1.5.1", true},
|
||||||
|
{"2.2.3", "1.5.1", false},
|
||||||
|
{"3.2-beta", "3.2-beta", false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
v1, err := NewVersion(tc.v1)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error parsing version: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
v2, err := NewVersion(tc.v2)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error parsing version: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
a := v1.LessThan(v2)
|
||||||
|
e := tc.expected
|
||||||
|
if a != e {
|
||||||
|
t.Errorf(
|
||||||
|
"Comparison of '%s' and '%s' failed. Expected '%t', got '%t'",
|
||||||
|
tc.v1, tc.v2, e, a,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGreaterThan(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
v1 string
|
||||||
|
v2 string
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{"1.2.3", "1.5.1", false},
|
||||||
|
{"2.2.3", "1.5.1", true},
|
||||||
|
{"3.2-beta", "3.2-beta", false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
v1, err := NewVersion(tc.v1)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error parsing version: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
v2, err := NewVersion(tc.v2)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error parsing version: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
a := v1.GreaterThan(v2)
|
||||||
|
e := tc.expected
|
||||||
|
if a != e {
|
||||||
|
t.Errorf(
|
||||||
|
"Comparison of '%s' and '%s' failed. Expected '%t', got '%t'",
|
||||||
|
tc.v1, tc.v2, e, a,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEqual(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
v1 string
|
||||||
|
v2 string
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{"1.2.3", "1.5.1", false},
|
||||||
|
{"2.2.3", "1.5.1", false},
|
||||||
|
{"3.2-beta", "3.2-beta", true},
|
||||||
|
{"3.2-beta+foo", "3.2-beta+bar", true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
v1, err := NewVersion(tc.v1)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error parsing version: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
v2, err := NewVersion(tc.v2)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error parsing version: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
a := v1.Equal(v2)
|
||||||
|
e := tc.expected
|
||||||
|
if a != e {
|
||||||
|
t.Errorf(
|
||||||
|
"Comparison of '%s' and '%s' failed. Expected '%t', got '%t'",
|
||||||
|
tc.v1, tc.v2, e, a,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInc(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
v1 string
|
||||||
|
expected string
|
||||||
|
how string
|
||||||
|
expectedOriginal string
|
||||||
|
}{
|
||||||
|
{"1.2.3", "1.2.4", "patch", "1.2.4"},
|
||||||
|
{"v1.2.4", "1.2.5", "patch", "v1.2.5"},
|
||||||
|
{"1.2.3", "1.3.0", "minor", "1.3.0"},
|
||||||
|
{"v1.2.4", "1.3.0", "minor", "v1.3.0"},
|
||||||
|
{"1.2.3", "2.0.0", "major", "2.0.0"},
|
||||||
|
{"v1.2.4", "2.0.0", "major", "v2.0.0"},
|
||||||
|
{"1.2.3+meta", "1.2.4", "patch", "1.2.4"},
|
||||||
|
{"1.2.3-beta+meta", "1.2.3", "patch", "1.2.3"},
|
||||||
|
{"v1.2.4-beta+meta", "1.2.4", "patch", "v1.2.4"},
|
||||||
|
{"1.2.3-beta+meta", "1.3.0", "minor", "1.3.0"},
|
||||||
|
{"v1.2.4-beta+meta", "1.3.0", "minor", "v1.3.0"},
|
||||||
|
{"1.2.3-beta+meta", "2.0.0", "major", "2.0.0"},
|
||||||
|
{"v1.2.4-beta+meta", "2.0.0", "major", "v2.0.0"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
v1, err := NewVersion(tc.v1)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error parsing version: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var v2 Version
|
||||||
|
switch tc.how {
|
||||||
|
case "patch":
|
||||||
|
v2 = v1.IncPatch()
|
||||||
|
case "minor":
|
||||||
|
v2 = v1.IncMinor()
|
||||||
|
case "major":
|
||||||
|
v2 = v1.IncMajor()
|
||||||
|
}
|
||||||
|
|
||||||
|
a := v2.String()
|
||||||
|
e := tc.expected
|
||||||
|
if a != e {
|
||||||
|
t.Errorf(
|
||||||
|
"Inc %q failed. Expected %q got %q",
|
||||||
|
tc.how, e, a,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
a = v2.Original()
|
||||||
|
e = tc.expectedOriginal
|
||||||
|
if a != e {
|
||||||
|
t.Errorf(
|
||||||
|
"Inc %q failed. Expected original %q got %q",
|
||||||
|
tc.how, e, a,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSetPrerelease(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
v1 string
|
||||||
|
prerelease string
|
||||||
|
expectedVersion string
|
||||||
|
expectedPrerelease string
|
||||||
|
expectedOriginal string
|
||||||
|
expectedErr error
|
||||||
|
}{
|
||||||
|
{"1.2.3", "**", "1.2.3", "", "1.2.3", ErrInvalidPrerelease},
|
||||||
|
{"1.2.3", "beta", "1.2.3-beta", "beta", "1.2.3-beta", nil},
|
||||||
|
{"v1.2.4", "beta", "1.2.4-beta", "beta", "v1.2.4-beta", nil},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
v1, err := NewVersion(tc.v1)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error parsing version: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
v2, err := v1.SetPrerelease(tc.prerelease)
|
||||||
|
if err != tc.expectedErr {
|
||||||
|
t.Errorf("Expected to get err=%s, but got err=%s", tc.expectedErr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
a := v2.Prerelease()
|
||||||
|
e := tc.expectedPrerelease
|
||||||
|
if a != e {
|
||||||
|
t.Errorf("Expected prerelease value=%q, but got %q", e, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
a = v2.String()
|
||||||
|
e = tc.expectedVersion
|
||||||
|
if a != e {
|
||||||
|
t.Errorf("Expected version string=%q, but got %q", e, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
a = v2.Original()
|
||||||
|
e = tc.expectedOriginal
|
||||||
|
if a != e {
|
||||||
|
t.Errorf("Expected version original=%q, but got %q", e, a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSetMetadata(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
v1 string
|
||||||
|
metadata string
|
||||||
|
expectedVersion string
|
||||||
|
expectedMetadata string
|
||||||
|
expectedOriginal string
|
||||||
|
expectedErr error
|
||||||
|
}{
|
||||||
|
{"1.2.3", "**", "1.2.3", "", "1.2.3", ErrInvalidMetadata},
|
||||||
|
{"1.2.3", "meta", "1.2.3+meta", "meta", "1.2.3+meta", nil},
|
||||||
|
{"v1.2.4", "meta", "1.2.4+meta", "meta", "v1.2.4+meta", nil},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
v1, err := NewVersion(tc.v1)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Error parsing version: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
v2, err := v1.SetMetadata(tc.metadata)
|
||||||
|
if err != tc.expectedErr {
|
||||||
|
t.Errorf("Expected to get err=%s, but got err=%s", tc.expectedErr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
a := v2.Metadata()
|
||||||
|
e := tc.expectedMetadata
|
||||||
|
if a != e {
|
||||||
|
t.Errorf("Expected metadata value=%q, but got %q", e, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
a = v2.String()
|
||||||
|
e = tc.expectedVersion
|
||||||
|
if e != a {
|
||||||
|
t.Errorf("Expected version string=%q, but got %q", e, a)
|
||||||
|
}
|
||||||
|
|
||||||
|
a = v2.Original()
|
||||||
|
e = tc.expectedOriginal
|
||||||
|
if a != e {
|
||||||
|
t.Errorf("Expected version original=%q, but got %q", e, a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOriginalVPrefix(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
version string
|
||||||
|
vprefix string
|
||||||
|
}{
|
||||||
|
{"1.2.3", ""},
|
||||||
|
{"v1.2.4", "v"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
v1, _ := NewVersion(tc.version)
|
||||||
|
a := v1.originalVPrefix()
|
||||||
|
e := tc.vprefix
|
||||||
|
if a != e {
|
||||||
|
t.Errorf("Expected vprefix=%q, but got %q", e, a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1
vendor/github.com/Masterminds/sprig/.gitignore
generated
vendored
1
vendor/github.com/Masterminds/sprig/.gitignore
generated
vendored
@@ -1 +1,2 @@
|
|||||||
vendor/
|
vendor/
|
||||||
|
/.glide
|
||||||
|
9
vendor/github.com/Masterminds/sprig/.travis.yml
generated
vendored
9
vendor/github.com/Masterminds/sprig/.travis.yml
generated
vendored
@@ -1,9 +1,9 @@
|
|||||||
language: go
|
language: go
|
||||||
|
|
||||||
go:
|
go:
|
||||||
- 1.3
|
- 1.6
|
||||||
- 1.4
|
- 1.7
|
||||||
- 1.5
|
- 1.8
|
||||||
- tip
|
- tip
|
||||||
|
|
||||||
# Setting sudo access to false will let Travis CI use containers rather than
|
# Setting sudo access to false will let Travis CI use containers rather than
|
||||||
@@ -12,6 +12,9 @@ go:
|
|||||||
# - http://docs.travis-ci.com/user/workers/standard-infrastructure/
|
# - http://docs.travis-ci.com/user/workers/standard-infrastructure/
|
||||||
sudo: false
|
sudo: false
|
||||||
|
|
||||||
|
script:
|
||||||
|
- make setup test
|
||||||
|
|
||||||
notifications:
|
notifications:
|
||||||
webhooks:
|
webhooks:
|
||||||
urls:
|
urls:
|
||||||
|
13
vendor/github.com/Masterminds/sprig/Makefile
generated
vendored
Normal file
13
vendor/github.com/Masterminds/sprig/Makefile
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
|
||||||
|
HAS_GLIDE := $(shell command -v glide;)
|
||||||
|
|
||||||
|
.PHONY: test
|
||||||
|
test:
|
||||||
|
go test -v .
|
||||||
|
|
||||||
|
.PHONY: setup
|
||||||
|
setup:
|
||||||
|
ifndef HAS_GLIDE
|
||||||
|
go get -u github.com/Masterminds/glide
|
||||||
|
endif
|
||||||
|
glide install
|
173
vendor/github.com/Masterminds/sprig/README.md
generated
vendored
173
vendor/github.com/Masterminds/sprig/README.md
generated
vendored
@@ -1,4 +1,6 @@
|
|||||||
# Sprig: Template functions for Go templates
|
# Sprig: Template functions for Go templates
|
||||||
|
[](https://masterminds.github.io/stability/sustained.html)
|
||||||
|
[](https://travis-ci.org/Masterminds/sprig)
|
||||||
|
|
||||||
The Go language comes with a [built-in template
|
The Go language comes with a [built-in template
|
||||||
language](http://golang.org/pkg/text/template/), but not
|
language](http://golang.org/pkg/text/template/), but not
|
||||||
@@ -6,12 +8,15 @@ very many template functions. This library provides a group of commonly
|
|||||||
used template functions.
|
used template functions.
|
||||||
|
|
||||||
It is inspired by the template functions found in
|
It is inspired by the template functions found in
|
||||||
[Twig](http://twig.sensiolabs.org/documentation).
|
[Twig](http://twig.sensiolabs.org/documentation) and also in various
|
||||||
|
JavaScript libraries, such as [underscore.js](http://underscorejs.org/).
|
||||||
[](https://travis-ci.org/Masterminds/sprig)
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
Template developers can read the [Sprig function documentation](http://masterminds.github.io/sprig/) to
|
||||||
|
learn about the >100 template functions available.
|
||||||
|
|
||||||
|
For Go developers wishing to include Sprig as a library in their programs,
|
||||||
API documentation is available [at GoDoc.org](http://godoc.org/github.com/Masterminds/sprig), but
|
API documentation is available [at GoDoc.org](http://godoc.org/github.com/Masterminds/sprig), but
|
||||||
read on for standard usage.
|
read on for standard usage.
|
||||||
|
|
||||||
@@ -54,168 +59,6 @@ Produces:
|
|||||||
HELLO!HELLO!HELLO!HELLO!HELLO!
|
HELLO!HELLO!HELLO!HELLO!HELLO!
|
||||||
```
|
```
|
||||||
|
|
||||||
## Functions
|
|
||||||
|
|
||||||
### Date Functions
|
|
||||||
|
|
||||||
- date: Format a date, where a date is an integer type or a time.Time type, and
|
|
||||||
format is a time.Format formatting string.
|
|
||||||
- dateModify: Given a date, modify it with a duration: `date_modify "-1.5h" now`. If the duration doesn't
|
|
||||||
parse, it returns the time unaltered. See `time.ParseDuration` for info on duration strings.
|
|
||||||
- now: Current time.Time, for feeding into date-related functions.
|
|
||||||
- htmlDate: Format a date for use in the value field of an HTML "date" form element.
|
|
||||||
- dateInZone: Like date, but takes three arguments: format, timestamp,
|
|
||||||
timezone.
|
|
||||||
- htmlDateInZone: Like htmlDate, but takes two arguments: timestamp,
|
|
||||||
timezone.
|
|
||||||
|
|
||||||
### String Functions
|
|
||||||
|
|
||||||
- trim: strings.TrimSpace
|
|
||||||
- trimAll: strings.Trim, but with the argument order reversed `trimAll "$" "$5.00"` or `"$5.00 | trimAll "$"`
|
|
||||||
- trimSuffix: strings.TrimSuffix, but with the argument order reversed `trimSuffix "-" "5-"`
|
|
||||||
- trimPrefix: strings.TrimPrefix, but with the argument order reversed `trimPrefix "$" "$5"`
|
|
||||||
- upper: strings.ToUpper
|
|
||||||
- lower: strings.ToLower
|
|
||||||
- title: strings.Title
|
|
||||||
- repeat: strings.Repeat, but with the arguments switched: `repeat count str`. (This simplifies common pipelines)
|
|
||||||
- substr: Given string, start, and length, return a substr.
|
|
||||||
- nospace: Remove all spaces from a string. `h e l l o` becomes
|
|
||||||
`hello`.
|
|
||||||
- abbrev: Truncate a string with ellipses
|
|
||||||
- trunc: Truncate a string (no suffix). `trunc 5 "Hello World"` yields "hello".
|
|
||||||
- abbrevboth: Truncate both sides of a string with ellipses
|
|
||||||
- untitle: Remove title case
|
|
||||||
- intials: Given multiple words, return the first letter of each
|
|
||||||
word
|
|
||||||
- randAlphaNum: Generate a random alpha-numeric string
|
|
||||||
- randAlpha: Generate a random alphabetic string
|
|
||||||
- randAscii: Generate a random ASCII string, including symbols
|
|
||||||
- randNumeric: Generate a random numeric string
|
|
||||||
- wrap: Wrap text at the given column count
|
|
||||||
- wrapWith: Wrap text at the given column count, and with the given
|
|
||||||
string for a line terminator: `wrap 50 "\n\t" $string`
|
|
||||||
- contains: strings.Contains, but with the arguments switched: `contains "cat" "uncatch"`. (This simplifies common pipelines)
|
|
||||||
- hasPrefix: strings.hasPrefix, but with the arguments switched: `hasPrefix "cat" "catch"`.
|
|
||||||
- hasSuffix: strings.hasSuffix, but with the arguments switched: `hasSuffix "cat" "ducat"`.
|
|
||||||
- quote: Wrap strings in double quotes. `quote "a" "b"` returns `"a"
|
|
||||||
"b"`
|
|
||||||
- squote: Wrap strings in single quotes.
|
|
||||||
- cat: Concatenate strings, separating them by spaces. `cat $a $b $c`.
|
|
||||||
- indent: Indent a string using space characters. `indent 4 "foo\nbar"` produces " foo\n bar"
|
|
||||||
- replace: Replace an old with a new in a string: `$name | replace " " "-"`
|
|
||||||
- plural: Choose singular or plural based on length: `len $fish | plural
|
|
||||||
"one anchovy" "many anchovies"`
|
|
||||||
- uuidv4: Generate a UUID v4 string
|
|
||||||
- sha256sum: Generate a hex encoded sha256 hash of the input
|
|
||||||
|
|
||||||
### String Slice Functions:
|
|
||||||
|
|
||||||
- join: strings.Join, but as `join SEP SLICE`
|
|
||||||
- split: strings.Split, but as `split SEP STRING`. The results are returned
|
|
||||||
as a map with the indexes set to _N, where N is an integer starting from 0.
|
|
||||||
Use it like this: `{{$v := "foo/bar/baz" | split "/"}}{{$v._0}}` (Prints `foo`)
|
|
||||||
|
|
||||||
### Integer Slice Functions:
|
|
||||||
|
|
||||||
- until: Given an integer, returns a slice of counting integers from 0 to one
|
|
||||||
less than the given integer: `range $i, $e := until 5`
|
|
||||||
- untilStep: Given start, stop, and step, return an integer slice starting at
|
|
||||||
'start', stopping at `stop`, and incrementing by 'step'. This is the same
|
|
||||||
as Python's long-form of 'range'.
|
|
||||||
|
|
||||||
### Conversions:
|
|
||||||
|
|
||||||
- atoi: Convert a string to an integer. 0 if the integer could not be parsed.
|
|
||||||
- int: Convert a string or numeric to an int
|
|
||||||
- int64: Convert a string or numeric to an int64
|
|
||||||
- float64: Convert a string or numeric to a float64
|
|
||||||
|
|
||||||
### Defaults:
|
|
||||||
|
|
||||||
- default: Give a default value. Used like this: {{trim " "| default "empty"}}.
|
|
||||||
Since trim produces an empty string, the default value is returned. For
|
|
||||||
things with a length (strings, slices, maps), len(0) will trigger the default.
|
|
||||||
For numbers, the value 0 will trigger the default. For booleans, false will
|
|
||||||
trigger the default. For structs, the default is never returned (there is
|
|
||||||
no clear empty condition). For everything else, nil value triggers a default.
|
|
||||||
- empty: Returns true if the given value is the zero value for that
|
|
||||||
type. Structs are always non-empty.
|
|
||||||
|
|
||||||
### OS:
|
|
||||||
|
|
||||||
- env: Read an environment variable.
|
|
||||||
- expandenv: Expand all environment variables in a string.
|
|
||||||
|
|
||||||
### Encoding:
|
|
||||||
|
|
||||||
- b32enc: Encode a string into a Base32 string
|
|
||||||
- b32dec: Decode a string from a Base32 string
|
|
||||||
- b64enc: Encode a string into a Base64 string
|
|
||||||
- b64dec: Decode a string from a Base64 string
|
|
||||||
|
|
||||||
### Data Structures:
|
|
||||||
|
|
||||||
- tuple: A sequence of related objects. It is implemented as a
|
|
||||||
`[]interface{}`, where each item can be accessed using `index`.
|
|
||||||
- dict: Takes a list of name/values and returns a map[string]interface{}.
|
|
||||||
The first parameter is converted to a string and stored as a key, the
|
|
||||||
second parameter is treated as the value. And so on, with odds as keys and
|
|
||||||
evens as values. If the function call ends with an odd, the last key will
|
|
||||||
be assigned the empty string. Non-string keys are converted to strings as
|
|
||||||
follows: []byte are converted, fmt.Stringers will have String() called.
|
|
||||||
errors will have Error() called. All others will be passed through
|
|
||||||
fmt.Sprtinf("%v").
|
|
||||||
- set: Takes a dict, a key, and a value, and sets that key/value pair in
|
|
||||||
the dict. `set $dict $key $value`. For convenience, it returns the dict,
|
|
||||||
even though the dict was modified in place.
|
|
||||||
- unset: Takes a dict and a key, and deletes that key/value pair from the
|
|
||||||
dict. `unset $dict $key`. This returns the dict for convenience.
|
|
||||||
- hasKey: Takes a dict and a key, and returns boolean true if the key is in
|
|
||||||
the dict.
|
|
||||||
|
|
||||||
```
|
|
||||||
{{$t := tuple 1 "a" "foo"}}
|
|
||||||
{{index $t 2}}{{index $t 0 }}{{index $t 1}}
|
|
||||||
{{/* Prints foo1a *}}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Reflection:
|
|
||||||
|
|
||||||
- typeOf: Takes an interface and returns a string representation of the type.
|
|
||||||
For pointers, this will return a type prefixed with an asterisk(`*`). So
|
|
||||||
a pointer to type `Foo` will be `*Foo`.
|
|
||||||
- typeIs: Compares an interface with a string name, and returns true if they match.
|
|
||||||
Note that a pointer will not match a reference. For example `*Foo` will not
|
|
||||||
match `Foo`.
|
|
||||||
- typeIsLike: returns true if the interface is of the given type, or
|
|
||||||
is a pointer to the given type.
|
|
||||||
- kindOf: Takes an interface and returns a string representation of its kind.
|
|
||||||
- kindIs: Returns true if the given string matches the kind of the given interface.
|
|
||||||
|
|
||||||
Note: None of these can test whether or not something implements a given
|
|
||||||
interface, since doing so would require compiling the interface in ahead of
|
|
||||||
time.
|
|
||||||
|
|
||||||
|
|
||||||
### Math Functions:
|
|
||||||
|
|
||||||
Integer functions will convert integers of any width to `int64`. If a
|
|
||||||
string is passed in, functions will attempt to conver with
|
|
||||||
`strconv.ParseInt(s, 1064)`. If this fails, the value will be treated as 0.
|
|
||||||
|
|
||||||
- add1: Increment an integer by 1
|
|
||||||
- add: Sum integers. `add 1 2 3` renders `6`
|
|
||||||
- sub: Subtract the second integer from the first
|
|
||||||
- div: Divide the first integer by the second
|
|
||||||
- mod: Module of first integer divided by second
|
|
||||||
- mul: Multiply integers integers
|
|
||||||
- max (biggest): Return the biggest of a series of integers. `max 1 2 3`
|
|
||||||
returns `3`.
|
|
||||||
- min: Return the smallest of a series of integers. `min 1 2 3` returns
|
|
||||||
`1`.
|
|
||||||
|
|
||||||
|
|
||||||
## Principles:
|
## Principles:
|
||||||
|
|
||||||
The following principles were used in deciding on which functions to add, and
|
The following principles were used in deciding on which functions to add, and
|
||||||
|
148
vendor/github.com/Masterminds/sprig/crypto.go
generated
vendored
Normal file
148
vendor/github.com/Masterminds/sprig/crypto.go
generated
vendored
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
package sprig
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/dsa"
|
||||||
|
"crypto/ecdsa"
|
||||||
|
"crypto/elliptic"
|
||||||
|
"crypto/hmac"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/rsa"
|
||||||
|
"crypto/sha256"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/asn1"
|
||||||
|
"encoding/binary"
|
||||||
|
"encoding/hex"
|
||||||
|
"encoding/pem"
|
||||||
|
"fmt"
|
||||||
|
"math/big"
|
||||||
|
|
||||||
|
uuid "github.com/satori/go.uuid"
|
||||||
|
"golang.org/x/crypto/scrypt"
|
||||||
|
)
|
||||||
|
|
||||||
|
func sha256sum(input string) string {
|
||||||
|
hash := sha256.Sum256([]byte(input))
|
||||||
|
return hex.EncodeToString(hash[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
// uuidv4 provides a safe and secure UUID v4 implementation
|
||||||
|
func uuidv4() string {
|
||||||
|
return fmt.Sprintf("%s", uuid.NewV4())
|
||||||
|
}
|
||||||
|
|
||||||
|
var master_password_seed = "com.lyndir.masterpassword"
|
||||||
|
|
||||||
|
var password_type_templates = map[string][][]byte{
|
||||||
|
"maximum": {[]byte("anoxxxxxxxxxxxxxxxxx"), []byte("axxxxxxxxxxxxxxxxxno")},
|
||||||
|
"long": {[]byte("CvcvnoCvcvCvcv"), []byte("CvcvCvcvnoCvcv"), []byte("CvcvCvcvCvcvno"), []byte("CvccnoCvcvCvcv"), []byte("CvccCvcvnoCvcv"),
|
||||||
|
[]byte("CvccCvcvCvcvno"), []byte("CvcvnoCvccCvcv"), []byte("CvcvCvccnoCvcv"), []byte("CvcvCvccCvcvno"), []byte("CvcvnoCvcvCvcc"),
|
||||||
|
[]byte("CvcvCvcvnoCvcc"), []byte("CvcvCvcvCvccno"), []byte("CvccnoCvccCvcv"), []byte("CvccCvccnoCvcv"), []byte("CvccCvccCvcvno"),
|
||||||
|
[]byte("CvcvnoCvccCvcc"), []byte("CvcvCvccnoCvcc"), []byte("CvcvCvccCvccno"), []byte("CvccnoCvcvCvcc"), []byte("CvccCvcvnoCvcc"),
|
||||||
|
[]byte("CvccCvcvCvccno")},
|
||||||
|
"medium": {[]byte("CvcnoCvc"), []byte("CvcCvcno")},
|
||||||
|
"short": {[]byte("Cvcn")},
|
||||||
|
"basic": {[]byte("aaanaaan"), []byte("aannaaan"), []byte("aaannaaa")},
|
||||||
|
"pin": {[]byte("nnnn")},
|
||||||
|
}
|
||||||
|
|
||||||
|
var template_characters = map[byte]string{
|
||||||
|
'V': "AEIOU",
|
||||||
|
'C': "BCDFGHJKLMNPQRSTVWXYZ",
|
||||||
|
'v': "aeiou",
|
||||||
|
'c': "bcdfghjklmnpqrstvwxyz",
|
||||||
|
'A': "AEIOUBCDFGHJKLMNPQRSTVWXYZ",
|
||||||
|
'a': "AEIOUaeiouBCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz",
|
||||||
|
'n': "0123456789",
|
||||||
|
'o': "@&%?,=[]_:-+*$#!'^~;()/.",
|
||||||
|
'x': "AEIOUaeiouBCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz0123456789!@#$%^&*()",
|
||||||
|
}
|
||||||
|
|
||||||
|
func derivePassword(counter uint32, password_type, password, user, site string) string {
|
||||||
|
var templates = password_type_templates[password_type]
|
||||||
|
if templates == nil {
|
||||||
|
return fmt.Sprintf("cannot find password template %s", password_type)
|
||||||
|
}
|
||||||
|
|
||||||
|
var buffer bytes.Buffer
|
||||||
|
buffer.WriteString(master_password_seed)
|
||||||
|
binary.Write(&buffer, binary.BigEndian, uint32(len(user)))
|
||||||
|
buffer.WriteString(user)
|
||||||
|
|
||||||
|
salt := buffer.Bytes()
|
||||||
|
key, err := scrypt.Key([]byte(password), salt, 32768, 8, 2, 64)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("failed to derive password: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer.Truncate(len(master_password_seed))
|
||||||
|
binary.Write(&buffer, binary.BigEndian, uint32(len(site)))
|
||||||
|
buffer.WriteString(site)
|
||||||
|
binary.Write(&buffer, binary.BigEndian, counter)
|
||||||
|
|
||||||
|
var hmacv = hmac.New(sha256.New, key)
|
||||||
|
hmacv.Write(buffer.Bytes())
|
||||||
|
var seed = hmacv.Sum(nil)
|
||||||
|
var temp = templates[int(seed[0])%len(templates)]
|
||||||
|
|
||||||
|
buffer.Truncate(0)
|
||||||
|
for i, element := range temp {
|
||||||
|
pass_chars := template_characters[element]
|
||||||
|
pass_char := pass_chars[int(seed[i+1])%len(pass_chars)]
|
||||||
|
buffer.WriteByte(pass_char)
|
||||||
|
}
|
||||||
|
|
||||||
|
return buffer.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func generatePrivateKey(typ string) string {
|
||||||
|
var priv interface{}
|
||||||
|
var err error
|
||||||
|
switch typ {
|
||||||
|
case "", "rsa":
|
||||||
|
// good enough for government work
|
||||||
|
priv, err = rsa.GenerateKey(rand.Reader, 4096)
|
||||||
|
case "dsa":
|
||||||
|
key := new(dsa.PrivateKey)
|
||||||
|
// again, good enough for government work
|
||||||
|
if err = dsa.GenerateParameters(&key.Parameters, rand.Reader, dsa.L2048N256); err != nil {
|
||||||
|
return fmt.Sprintf("failed to generate dsa params: %s", err)
|
||||||
|
}
|
||||||
|
err = dsa.GenerateKey(key, rand.Reader)
|
||||||
|
priv = key
|
||||||
|
case "ecdsa":
|
||||||
|
// again, good enough for government work
|
||||||
|
priv, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||||
|
default:
|
||||||
|
return "Unknown type " + typ
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Sprintf("failed to generate private key: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(pem.EncodeToMemory(pemBlockForKey(priv)))
|
||||||
|
}
|
||||||
|
|
||||||
|
type DSAKeyFormat struct {
|
||||||
|
Version int
|
||||||
|
P, Q, G, Y, X *big.Int
|
||||||
|
}
|
||||||
|
|
||||||
|
func pemBlockForKey(priv interface{}) *pem.Block {
|
||||||
|
switch k := priv.(type) {
|
||||||
|
case *rsa.PrivateKey:
|
||||||
|
return &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(k)}
|
||||||
|
case *dsa.PrivateKey:
|
||||||
|
val := DSAKeyFormat{
|
||||||
|
P: k.P, Q: k.Q, G: k.G,
|
||||||
|
Y: k.Y, X: k.X,
|
||||||
|
}
|
||||||
|
bytes, _ := asn1.Marshal(val)
|
||||||
|
return &pem.Block{Type: "DSA PRIVATE KEY", Bytes: bytes}
|
||||||
|
case *ecdsa.PrivateKey:
|
||||||
|
b, _ := x509.MarshalECPrivateKey(k)
|
||||||
|
return &pem.Block{Type: "EC PRIVATE KEY", Bytes: b}
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
110
vendor/github.com/Masterminds/sprig/crypto_test.go
generated
vendored
Normal file
110
vendor/github.com/Masterminds/sprig/crypto_test.go
generated
vendored
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
package sprig
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSha256Sum(t *testing.T) {
|
||||||
|
tpl := `{{"abc" | sha256sum}}`
|
||||||
|
if err := runt(tpl, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDerivePassword(t *testing.T) {
|
||||||
|
expectations := map[string]string{
|
||||||
|
`{{derivePassword 1 "long" "password" "user" "example.com"}}`: "ZedaFaxcZaso9*",
|
||||||
|
`{{derivePassword 2 "long" "password" "user" "example.com"}}`: "Fovi2@JifpTupx",
|
||||||
|
`{{derivePassword 1 "maximum" "password" "user" "example.com"}}`: "pf4zS1LjCg&LjhsZ7T2~",
|
||||||
|
`{{derivePassword 1 "medium" "password" "user" "example.com"}}`: "ZedJuz8$",
|
||||||
|
`{{derivePassword 1 "basic" "password" "user" "example.com"}}`: "pIS54PLs",
|
||||||
|
`{{derivePassword 1 "short" "password" "user" "example.com"}}`: "Zed5",
|
||||||
|
`{{derivePassword 1 "pin" "password" "user" "example.com"}}`: "6685",
|
||||||
|
}
|
||||||
|
|
||||||
|
for tpl, result := range expectations {
|
||||||
|
out, err := runRaw(tpl, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if 0 != strings.Compare(out, result) {
|
||||||
|
t.Error("Generated password does not match for", tpl)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE(bacongobbler): this test is really _slow_ because of how long it takes to compute
|
||||||
|
// and generate a new crypto key.
|
||||||
|
func TestGenPrivateKey(t *testing.T) {
|
||||||
|
// test that calling by default generates an RSA private key
|
||||||
|
tpl := `{{genPrivateKey ""}}`
|
||||||
|
out, err := runRaw(tpl, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(out, "RSA PRIVATE KEY") {
|
||||||
|
t.Error("Expected RSA PRIVATE KEY")
|
||||||
|
}
|
||||||
|
// test all acceptable arguments
|
||||||
|
tpl = `{{genPrivateKey "rsa"}}`
|
||||||
|
out, err = runRaw(tpl, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(out, "RSA PRIVATE KEY") {
|
||||||
|
t.Error("Expected RSA PRIVATE KEY")
|
||||||
|
}
|
||||||
|
tpl = `{{genPrivateKey "dsa"}}`
|
||||||
|
out, err = runRaw(tpl, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(out, "DSA PRIVATE KEY") {
|
||||||
|
t.Error("Expected DSA PRIVATE KEY")
|
||||||
|
}
|
||||||
|
tpl = `{{genPrivateKey "ecdsa"}}`
|
||||||
|
out, err = runRaw(tpl, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(out, "EC PRIVATE KEY") {
|
||||||
|
t.Error("Expected EC PRIVATE KEY")
|
||||||
|
}
|
||||||
|
// test bad
|
||||||
|
tpl = `{{genPrivateKey "bad"}}`
|
||||||
|
out, err = runRaw(tpl, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if out != "Unknown type bad" {
|
||||||
|
t.Error("Expected type 'bad' to be an unknown crypto algorithm")
|
||||||
|
}
|
||||||
|
// ensure that we can base64 encode the string
|
||||||
|
tpl = `{{genPrivateKey "rsa" | b64enc}}`
|
||||||
|
out, err = runRaw(tpl, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUUIDGeneration(t *testing.T) {
|
||||||
|
tpl := `{{uuidv4}}`
|
||||||
|
out, err := runRaw(tpl, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(out) != 36 {
|
||||||
|
t.Error("Expected UUID of length 36")
|
||||||
|
}
|
||||||
|
|
||||||
|
out2, err := runRaw(tpl, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if out == out2 {
|
||||||
|
t.Error("Expected subsequent UUID generations to be different")
|
||||||
|
}
|
||||||
|
}
|
53
vendor/github.com/Masterminds/sprig/date.go
generated
vendored
Normal file
53
vendor/github.com/Masterminds/sprig/date.go
generated
vendored
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
package sprig
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Given a format and a date, format the date string.
|
||||||
|
//
|
||||||
|
// Date can be a `time.Time` or an `int, int32, int64`.
|
||||||
|
// In the later case, it is treated as seconds since UNIX
|
||||||
|
// epoch.
|
||||||
|
func date(fmt string, date interface{}) string {
|
||||||
|
return dateInZone(fmt, date, "Local")
|
||||||
|
}
|
||||||
|
|
||||||
|
func htmlDate(date interface{}) string {
|
||||||
|
return dateInZone("2006-01-02", date, "Local")
|
||||||
|
}
|
||||||
|
|
||||||
|
func htmlDateInZone(date interface{}, zone string) string {
|
||||||
|
return dateInZone("2006-01-02", date, zone)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dateInZone(fmt string, date interface{}, zone string) string {
|
||||||
|
var t time.Time
|
||||||
|
switch date := date.(type) {
|
||||||
|
default:
|
||||||
|
t = time.Now()
|
||||||
|
case time.Time:
|
||||||
|
t = date
|
||||||
|
case int64:
|
||||||
|
t = time.Unix(date, 0)
|
||||||
|
case int:
|
||||||
|
t = time.Unix(int64(date), 0)
|
||||||
|
case int32:
|
||||||
|
t = time.Unix(int64(date), 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
loc, err := time.LoadLocation(zone)
|
||||||
|
if err != nil {
|
||||||
|
loc, _ = time.LoadLocation("UTC")
|
||||||
|
}
|
||||||
|
|
||||||
|
return t.In(loc).Format(fmt)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dateModify(fmt string, date time.Time) time.Time {
|
||||||
|
d, err := time.ParseDuration(fmt)
|
||||||
|
if err != nil {
|
||||||
|
return date
|
||||||
|
}
|
||||||
|
return date.Add(d)
|
||||||
|
}
|
13
vendor/github.com/Masterminds/sprig/date_test.go
generated
vendored
Normal file
13
vendor/github.com/Masterminds/sprig/date_test.go
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package sprig
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestHtmlDate(t *testing.T) {
|
||||||
|
t.Skip()
|
||||||
|
tpl := `{{ htmlDate 0}}`
|
||||||
|
if err := runt(tpl, "1970-01-01"); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
75
vendor/github.com/Masterminds/sprig/defaults.go
generated
vendored
Normal file
75
vendor/github.com/Masterminds/sprig/defaults.go
generated
vendored
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
package sprig
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
// dfault checks whether `given` is set, and returns default if not set.
|
||||||
|
//
|
||||||
|
// This returns `d` if `given` appears not to be set, and `given` otherwise.
|
||||||
|
//
|
||||||
|
// For numeric types 0 is unset.
|
||||||
|
// For strings, maps, arrays, and slices, len() = 0 is considered unset.
|
||||||
|
// For bool, false is unset.
|
||||||
|
// Structs are never considered unset.
|
||||||
|
//
|
||||||
|
// For everything else, including pointers, a nil value is unset.
|
||||||
|
func dfault(d interface{}, given ...interface{}) interface{} {
|
||||||
|
|
||||||
|
if empty(given) || empty(given[0]) {
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
return given[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
// empty returns true if the given value has the zero value for its type.
|
||||||
|
func empty(given interface{}) bool {
|
||||||
|
g := reflect.ValueOf(given)
|
||||||
|
if !g.IsValid() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Basically adapted from text/template.isTrue
|
||||||
|
switch g.Kind() {
|
||||||
|
default:
|
||||||
|
return g.IsNil()
|
||||||
|
case reflect.Array, reflect.Slice, reflect.Map, reflect.String:
|
||||||
|
return g.Len() == 0
|
||||||
|
case reflect.Bool:
|
||||||
|
return g.Bool() == false
|
||||||
|
case reflect.Complex64, reflect.Complex128:
|
||||||
|
return g.Complex() == 0
|
||||||
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
|
return g.Int() == 0
|
||||||
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||||
|
return g.Uint() == 0
|
||||||
|
case reflect.Float32, reflect.Float64:
|
||||||
|
return g.Float() == 0
|
||||||
|
case reflect.Struct:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// coalesce returns the first non-empty value.
|
||||||
|
func coalesce(v ...interface{}) interface{} {
|
||||||
|
for _, val := range v {
|
||||||
|
if !empty(val) {
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// toJson encodes an item into a JSON string
|
||||||
|
func toJson(v interface{}) string {
|
||||||
|
output, _ := json.Marshal(v)
|
||||||
|
return string(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
// toPrettyJson encodes an item into a pretty (indented) JSON string
|
||||||
|
func toPrettyJson(v interface{}) string {
|
||||||
|
output, _ := json.MarshalIndent(v, "", " ")
|
||||||
|
return string(output)
|
||||||
|
}
|
107
vendor/github.com/Masterminds/sprig/defaults_test.go
generated
vendored
Normal file
107
vendor/github.com/Masterminds/sprig/defaults_test.go
generated
vendored
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
package sprig
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDefault(t *testing.T) {
|
||||||
|
tpl := `{{"" | default "foo"}}`
|
||||||
|
if err := runt(tpl, "foo"); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
tpl = `{{default "foo" 234}}`
|
||||||
|
if err := runt(tpl, "234"); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
tpl = `{{default "foo" 2.34}}`
|
||||||
|
if err := runt(tpl, "2.34"); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tpl = `{{ .Nothing | default "123" }}`
|
||||||
|
if err := runt(tpl, "123"); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
tpl = `{{ default "123" }}`
|
||||||
|
if err := runt(tpl, "123"); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEmpty(t *testing.T) {
|
||||||
|
tpl := `{{if empty 1}}1{{else}}0{{end}}`
|
||||||
|
if err := runt(tpl, "0"); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
tpl = `{{if empty 0}}1{{else}}0{{end}}`
|
||||||
|
if err := runt(tpl, "1"); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
tpl = `{{if empty ""}}1{{else}}0{{end}}`
|
||||||
|
if err := runt(tpl, "1"); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
tpl = `{{if empty 0.0}}1{{else}}0{{end}}`
|
||||||
|
if err := runt(tpl, "1"); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
tpl = `{{if empty false}}1{{else}}0{{end}}`
|
||||||
|
if err := runt(tpl, "1"); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
dict := map[string]interface{}{"top": map[string]interface{}{}}
|
||||||
|
tpl = `{{if empty .top.NoSuchThing}}1{{else}}0{{end}}`
|
||||||
|
if err := runtv(tpl, "1", dict); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
tpl = `{{if empty .bottom.NoSuchThing}}1{{else}}0{{end}}`
|
||||||
|
if err := runtv(tpl, "1", dict); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func TestCoalesce(t *testing.T) {
|
||||||
|
tests := map[string]string{
|
||||||
|
`{{ coalesce 1 }}`: "1",
|
||||||
|
`{{ coalesce "" 0 nil 2 }}`: "2",
|
||||||
|
`{{ $two := 2 }}{{ coalesce "" 0 nil $two }}`: "2",
|
||||||
|
`{{ $two := 2 }}{{ coalesce "" $two 0 0 0 }}`: "2",
|
||||||
|
`{{ $two := 2 }}{{ coalesce "" $two 3 4 5 }}`: "2",
|
||||||
|
`{{ coalesce }}`: "<no value>",
|
||||||
|
}
|
||||||
|
for tpl, expect := range tests {
|
||||||
|
assert.NoError(t, runt(tpl, expect))
|
||||||
|
}
|
||||||
|
|
||||||
|
dict := map[string]interface{}{"top": map[string]interface{}{}}
|
||||||
|
tpl := `{{ coalesce .top.NoSuchThing .bottom .bottom.dollar "airplane"}}`
|
||||||
|
if err := runtv(tpl, "airplane", dict); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToJson(t *testing.T) {
|
||||||
|
dict := map[string]interface{}{"Top": map[string]interface{}{"bool": true, "string": "test", "number": 42}}
|
||||||
|
|
||||||
|
tpl := `{{.Top | toJson}}`
|
||||||
|
expected := `{"bool":true,"number":42,"string":"test"}`
|
||||||
|
if err := runtv(tpl, expected, dict); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToPrettyJson(t *testing.T) {
|
||||||
|
dict := map[string]interface{}{"Top": map[string]interface{}{"bool": true, "string": "test", "number": 42}}
|
||||||
|
tpl := `{{.Top | toPrettyJson}}`
|
||||||
|
expected := `{
|
||||||
|
"bool": true,
|
||||||
|
"number": 42,
|
||||||
|
"string": "test"
|
||||||
|
}`
|
||||||
|
if err := runtv(tpl, expected, dict); err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user