Compare commits
109 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
6cbc8f35eb | ||
|
37d97c5693 | ||
|
ab2a5d5181 | ||
|
a8d641cd8d | ||
|
af1ed8a2fb | ||
|
54eb016142 | ||
|
81310a7f0f | ||
|
2c01e8b298 | ||
|
6b43c020b1 | ||
|
e8cdc47633 | ||
|
f8c091326e | ||
|
c73331e20d | ||
|
83f2bd1514 | ||
|
22d4ea9a44 | ||
|
791af18420 | ||
|
6106c552da | ||
|
7e6c4a9401 | ||
|
d6aa9a06cc | ||
|
877e93aa7c | ||
|
cdb9580565 | ||
|
f78470553b | ||
|
c26d72d1ad | ||
|
91e9114c24 | ||
|
f5d97d64c7 | ||
|
4c53f8ebdd | ||
|
718f9120e4 | ||
|
f4f2300417 | ||
|
c7d0a86fec | ||
|
453376e811 | ||
|
49d31f1636 | ||
|
7d8a132834 | ||
|
e8332ed718 | ||
|
e4160940c1 | ||
|
88a2f47522 | ||
|
5dd30f38f3 | ||
|
d0ac6afb18 | ||
|
15db6dce00 | ||
|
19ea337fa6 | ||
|
536162a58b | ||
|
304fba375b | ||
|
5c6941cf87 | ||
|
2195db0de2 | ||
|
eec84d5d96 | ||
|
d38fbfccf0 | ||
|
abbd30d34c | ||
|
5b8c9e93dc | ||
|
9cde3288ea | ||
|
d3abcca373 | ||
|
df709b52ac | ||
|
f9e191c6a4 | ||
|
8e2be0f866 | ||
|
869e37e163 | ||
|
5489e68995 | ||
|
1bbb1dfb26 | ||
|
6beb5868e8 | ||
|
da88fbcbd7 | ||
|
add7ae7237 | ||
|
d7c3d1a32b | ||
|
8b7fa40e87 | ||
|
2ab581a1d6 | ||
|
3b5a2eeafe | ||
|
e48d798d89 | ||
|
f48fe29ebf | ||
|
ae44a872b3 | ||
|
7907fb79d9 | ||
|
407790640d | ||
|
14d2af18a7 | ||
|
ba1f64a731 | ||
|
aa9a474262 | ||
|
1afdd936da | ||
|
a4749d98ae | ||
|
b4d1f1f9bb | ||
|
2ae75f6702 | ||
|
6c946d62eb | ||
|
514198f2e5 | ||
|
ff7e469a05 | ||
|
9c249b45a4 | ||
|
b34abe8c12 | ||
|
c83a19d80c | ||
|
0336dc5217 | ||
|
dbd5ed55a4 | ||
|
d252b157c9 | ||
|
605610f7d0 | ||
|
8b771ff4c7 | ||
|
701c12f866 | ||
|
d748878896 | ||
|
f62940ac4c | ||
|
bffc245a82 | ||
|
71fb53ae02 | ||
|
ce8f7bd8a7 | ||
|
218f25ab56 | ||
|
09a9873bad | ||
|
664498d53d | ||
|
1146013b1f | ||
|
19a3db3d08 | ||
|
b6d7814f97 | ||
|
8958575958 | ||
|
9222cc15b9 | ||
|
136f9b6fba | ||
|
32072ab20b | ||
|
84c1d65fd3 | ||
|
9e79d2b3aa | ||
|
83232e17d7 | ||
|
efcd958d6b | ||
|
96b930de07 | ||
|
65f289bea7 | ||
|
e0c9f987fc | ||
|
c8c607ae0d | ||
|
7ec1769398 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,5 @@
|
||||
/protoc-gen-gotemplate
|
||||
|
||||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
|
14
.travis.yml
Normal file
14
.travis.yml
Normal file
@@ -0,0 +1,14 @@
|
||||
language: go
|
||||
go: 1.7.x
|
||||
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/gogo/protobuf/protoc-gen-gogo
|
||||
script:
|
||||
- make install
|
||||
- make test
|
||||
cache:
|
||||
directories:
|
||||
- $HOME/local
|
||||
env:
|
||||
global:
|
||||
- "PATH=$PATH:$HOME/local/bin"
|
5
Dockerfile
Normal file
5
Dockerfile
Normal file
@@ -0,0 +1,5 @@
|
||||
FROM znly/protoc
|
||||
RUN apk --update add make git go rsync
|
||||
COPY . /go/src/github.com/moul/protoc-gen-gotemplate
|
||||
WORKDIR /go/src/github.com/moul/protoc-gen-gotemplate
|
||||
RUN go install .
|
20
Makefile
Normal file
20
Makefile
Normal file
@@ -0,0 +1,20 @@
|
||||
.PHONY: build
|
||||
build:
|
||||
go build -o protoc-gen-gotemplate .
|
||||
|
||||
.PHONY: install
|
||||
install:
|
||||
go install .
|
||||
|
||||
.PHONY: test
|
||||
test: build
|
||||
cd examples/dummy && make
|
||||
cd examples/flow && make
|
||||
|
||||
.PHONY: docker.build
|
||||
docker.build:
|
||||
docker build --pull -t moul/protoc-gen-gotemplate .
|
||||
|
||||
.PHONY: docker.push
|
||||
docker.push: docker.build
|
||||
docker push moul/protoc-gen-gotemplate
|
69
README.md
69
README.md
@@ -1,2 +1,69 @@
|
||||
# protoc-gen-gotemplate
|
||||
# `protoc-gen-gotemplate`
|
||||
: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).
|
||||
|
||||
---
|
||||
|
||||
This 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.
|
||||
|
||||
## Usage
|
||||
|
||||
`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.
|
||||
|
||||
---
|
||||
|
||||
```console
|
||||
$> ls -R
|
||||
input.proto templates/doc.txt.tmpl templates/config.json.tmpl
|
||||
$> protoc --gotemplate_out=. input.proto
|
||||
$> ls -R
|
||||
input.proto templates/doc.txt.tmpl templates/config.json.tmpl
|
||||
doc.txt config.json
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
You can specify a custom `template_dir` or enable `debug`:
|
||||
|
||||
```console
|
||||
$> protoc --gotemplate_out=debug=true,template_dir=/path/to/template/directory:. input.proto
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
See [examples](./examples).
|
||||
|
||||
## Funcmap
|
||||
|
||||
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:
|
||||
|
||||
* **all the functions from [sprig](https://github.com/Masterminds/sprig)**
|
||||
* `json`
|
||||
* `prettyjson`
|
||||
* `first`
|
||||
* `last`
|
||||
|
||||
See the project helpers for the complete list.
|
||||
|
||||
## Install
|
||||
|
||||
* Install the **Go** compiler and tools from https://golang.org/doc/install
|
||||
* Install **protobuf**: `go get -u github.com/golang/protobuf/{proto,protoc-gen-go}`
|
||||
* Install **protoc-gen-gotemplate**: `go get -u github.com/moul/protoc-gen-gotemplate`
|
||||
|
||||
## Projects using `protoc-gen-gotemplate`
|
||||
|
||||
* [kafka-gateway](https://github.com/moul/kafka-gateway/): Kafka gateway/proxy (gRPC + http) using Go-Kit
|
||||
* [translator](https://github.com/moul/translator): Translator Micro-service using Gettext and Go-Kit
|
||||
* [acl](https://github.com/moul/acl): ACL micro-service (gRPC/protobuf + http/json)
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
|
20
contrib/homebrew/protoc-gen-gotemplate.rb
Normal file
20
contrib/homebrew/protoc-gen-gotemplate.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
require "language/go"
|
||||
|
||||
class ProtocGenGotemplate < Formula
|
||||
desc "protocol generator + golang text/template (protobuf)"
|
||||
homepage "https://github.com/moul/protoc-gen-gotemplate"
|
||||
url "https://github.com/moul/protoc-gen-gotemplate/archive/v1.0.0.tar.gz"
|
||||
sha256 "1ff57cd8513f1e871cf71dc8f2099bf64204af0df1b7397370827083e95bbb82"
|
||||
head "https://github.com/moul/protoc-gen-gotemplate.git"
|
||||
|
||||
depends_on "go" => :build
|
||||
|
||||
def install
|
||||
ENV["GOPATH"] = buildpath
|
||||
ENV["GOBIN"] = buildpath
|
||||
ENV["GO15VENDOREXPERIMENT"] = "1"
|
||||
(buildpath/"src/github.com/moul/protoc-gen-gotemplate").install Dir["*"]
|
||||
|
||||
system "go", "build", "-o", "#{bin}/protoc-gen-gotemplate", "-v", "github.com/moul/protoc-gen-gotemplate"
|
||||
end
|
||||
end
|
192
encoder.go
192
encoder.go
@@ -2,107 +2,177 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/golang/protobuf/protoc-gen-go/descriptor"
|
||||
"github.com/golang/protobuf/protoc-gen-go/plugin"
|
||||
"github.com/kr/fs"
|
||||
"github.com/moul/funcmap"
|
||||
)
|
||||
|
||||
type GenericTemplateBasedEncoder struct {
|
||||
templateDir string
|
||||
service *descriptor.ServiceDescriptorProto
|
||||
file *descriptor.FileDescriptorProto
|
||||
templateDir string
|
||||
service *descriptor.ServiceDescriptorProto
|
||||
file *descriptor.FileDescriptorProto
|
||||
debug bool
|
||||
destinationDir string
|
||||
}
|
||||
|
||||
type Ast struct {
|
||||
Filename string
|
||||
Service *descriptor.ServiceDescriptorProto
|
||||
File *descriptor.FileDescriptorProto
|
||||
BuildDate time.Time `json:"build-date"`
|
||||
BuildHostname string `json:"build-hostname"`
|
||||
BuildUser string `json:"build-user"`
|
||||
GoPWD string `json:"go-pwd,omitempty"`
|
||||
PWD string `json:"pwd"`
|
||||
Debug bool `json:"debug"`
|
||||
DestinationDir string `json:"destination-dir"`
|
||||
File *descriptor.FileDescriptorProto `json:"file"`
|
||||
RawFilename string `json:"raw-filename"`
|
||||
Filename string `json:"filename"`
|
||||
TemplateDir string `json:"template-dir"`
|
||||
Service *descriptor.ServiceDescriptorProto `json:"service"`
|
||||
Environment []string `json:"environment"`
|
||||
}
|
||||
|
||||
func NewGenericTemplateBasedEncoder(templateDir string, service *descriptor.ServiceDescriptorProto, file *descriptor.FileDescriptorProto) (e *GenericTemplateBasedEncoder) {
|
||||
func NewGenericTemplateBasedEncoder(templateDir string, service *descriptor.ServiceDescriptorProto, file *descriptor.FileDescriptorProto, debug bool, destinationDir string) (e *GenericTemplateBasedEncoder) {
|
||||
e = &GenericTemplateBasedEncoder{
|
||||
service: service,
|
||||
file: file,
|
||||
templateDir: templateDir,
|
||||
service: service,
|
||||
file: file,
|
||||
templateDir: templateDir,
|
||||
debug: debug,
|
||||
destinationDir: destinationDir,
|
||||
}
|
||||
|
||||
if debug {
|
||||
log.Printf("new encoder: file=%q service=%q template-dir=%q", file.GetName(), service.GetName(), templateDir)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (e *GenericTemplateBasedEncoder) templates() ([]string, error) {
|
||||
filenames := []string{}
|
||||
|
||||
walker := fs.Walk(e.templateDir)
|
||||
for walker.Step() {
|
||||
if err := walker.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if walker.Stat().IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
if filepath.Ext(walker.Path()) != ".tmpl" {
|
||||
continue
|
||||
}
|
||||
|
||||
rel, err := filepath.Rel(e.templateDir, walker.Path())
|
||||
err := filepath.Walk(e.templateDir, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
if filepath.Ext(path) != ".tmpl" {
|
||||
return nil
|
||||
}
|
||||
rel, err := filepath.Rel(e.templateDir, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if e.debug {
|
||||
log.Printf("new template: %q", rel)
|
||||
}
|
||||
|
||||
filenames = append(filenames, rel)
|
||||
}
|
||||
|
||||
return filenames, nil
|
||||
return nil
|
||||
})
|
||||
return filenames, err
|
||||
}
|
||||
|
||||
func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (string, error) {
|
||||
fullPath := filepath.Join(e.templateDir, templateFilename)
|
||||
|
||||
tmpl, err := template.New(templateFilename).Funcs(funcmap.FuncMap).ParseFiles(fullPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
func (e *GenericTemplateBasedEncoder) genAst(templateFilename string) (*Ast, error) {
|
||||
// prepare the ast passed to the template engine
|
||||
hostname, _ := os.Hostname()
|
||||
pwd, _ := os.Getwd()
|
||||
goPwd := ""
|
||||
if os.Getenv("GOPATH") != "" {
|
||||
goPwd, _ = filepath.Rel(os.Getenv("GOPATH")+"/src", pwd)
|
||||
if strings.Contains(goPwd, "../") {
|
||||
goPwd = ""
|
||||
}
|
||||
}
|
||||
|
||||
ast := Ast{
|
||||
Filename: templateFilename,
|
||||
Service: e.service,
|
||||
File: e.file,
|
||||
BuildDate: time.Now(),
|
||||
BuildHostname: hostname,
|
||||
BuildUser: os.Getenv("USER"),
|
||||
PWD: pwd,
|
||||
GoPWD: goPwd,
|
||||
File: e.file,
|
||||
TemplateDir: e.templateDir,
|
||||
DestinationDir: e.destinationDir,
|
||||
RawFilename: templateFilename,
|
||||
Filename: "",
|
||||
Environment: os.Environ(),
|
||||
Service: e.service,
|
||||
}
|
||||
buffer := new(bytes.Buffer)
|
||||
tmpl, err := template.New("").Funcs(ProtoHelpersFuncMap).Parse(templateFilename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := tmpl.Execute(buffer, ast); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ast.Filename = buffer.String()
|
||||
return &ast, nil
|
||||
}
|
||||
|
||||
func (e *GenericTemplateBasedEncoder) buildContent(templateFilename string) (string, string, error) {
|
||||
// initialize template engine
|
||||
fullPath := filepath.Join(e.templateDir, templateFilename)
|
||||
templateName := filepath.Base(fullPath)
|
||||
tmpl, err := template.New(templateName).Funcs(ProtoHelpersFuncMap).ParseFiles(fullPath)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
ast, err := e.genAst(templateFilename)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
// generate the content
|
||||
buffer := new(bytes.Buffer)
|
||||
if err := tmpl.Execute(buffer, ast); err != nil {
|
||||
return "", err
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
return buffer.String(), nil
|
||||
return buffer.String(), ast.Filename, nil
|
||||
}
|
||||
|
||||
func (e *GenericTemplateBasedEncoder) Files() []*plugin_go.CodeGeneratorResponse_File {
|
||||
files := []*plugin_go.CodeGeneratorResponse_File{}
|
||||
|
||||
templates, err := e.templates()
|
||||
if err != nil {
|
||||
log.Fatalf("cannot get templates from %q: %v", e.templateDir, err)
|
||||
}
|
||||
|
||||
length := len(templates)
|
||||
files := make([]*plugin_go.CodeGeneratorResponse_File, 0, length)
|
||||
errChan := make(chan error, length)
|
||||
resultChan := make(chan *plugin_go.CodeGeneratorResponse_File, length)
|
||||
for _, templateFilename := range templates {
|
||||
go func(tmpl string) {
|
||||
content, translatedFilename, err := e.buildContent(tmpl)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
return
|
||||
}
|
||||
filename := translatedFilename[:len(translatedFilename)-len(".tmpl")]
|
||||
|
||||
resultChan <- &plugin_go.CodeGeneratorResponse_File{
|
||||
Content: &content,
|
||||
Name: &filename,
|
||||
}
|
||||
}(templateFilename)
|
||||
}
|
||||
for i := 0; i < length; i++ {
|
||||
select {
|
||||
case f := <-resultChan:
|
||||
files = append(files, f)
|
||||
case err = <-errChan:
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for _, templateFilename := range templates {
|
||||
filename := templateFilename[0 : len(templateFilename)-len(".tmpl")]
|
||||
|
||||
content, err := e.buildContent(templateFilename)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
files = append(files, &plugin_go.CodeGeneratorResponse_File{
|
||||
Content: &content,
|
||||
Name: &filename,
|
||||
})
|
||||
}
|
||||
|
||||
return files
|
||||
}
|
||||
|
@@ -1,7 +1,7 @@
|
||||
.PHONY: build
|
||||
build:
|
||||
mkdir -p output
|
||||
protoc -I. --gotemplate_out=output *.proto
|
||||
protoc -I. --gotemplate_out=template_dir=templates,debug=true:output *.proto
|
||||
|
||||
|
||||
.PHONY: re
|
881
examples/dummy/output/export.json
Normal file
881
examples/dummy/output/export.json
Normal file
@@ -0,0 +1,881 @@
|
||||
{
|
||||
"build-date": "2016-12-20T11:30:36.474403064+01:00",
|
||||
"build-hostname": "Zoon-MacBook.local",
|
||||
"build-user": "zoon",
|
||||
"go-pwd": "github.com/protoc-gen-gotemplate/examples/dummy",
|
||||
"pwd": "/Users/zoon/Projects/gopath/src/github.com/protoc-gen-gotemplate/examples/dummy",
|
||||
"debug": false,
|
||||
"destination-dir": ".",
|
||||
"file": {
|
||||
"name": "dummy.proto",
|
||||
"package": "dummy",
|
||||
"message_type": [
|
||||
{
|
||||
"name": "Dummy1",
|
||||
"field": [
|
||||
{
|
||||
"name": "aaa",
|
||||
"number": 1,
|
||||
"label": 1,
|
||||
"type": 2,
|
||||
"json_name": "aaa"
|
||||
},
|
||||
{
|
||||
"name": "bbb",
|
||||
"number": 2,
|
||||
"label": 1,
|
||||
"type": 9,
|
||||
"json_name": "bbb"
|
||||
},
|
||||
{
|
||||
"name": "ccc",
|
||||
"number": 3,
|
||||
"label": 1,
|
||||
"type": 5,
|
||||
"json_name": "ccc"
|
||||
},
|
||||
{
|
||||
"name": "ddd",
|
||||
"number": 4,
|
||||
"label": 1,
|
||||
"type": 3,
|
||||
"json_name": "ddd"
|
||||
},
|
||||
{
|
||||
"name": "eee",
|
||||
"number": 5,
|
||||
"label": 3,
|
||||
"type": 9,
|
||||
"json_name": "eee"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Dummy2",
|
||||
"field": [
|
||||
{
|
||||
"name": "fff",
|
||||
"number": 1,
|
||||
"label": 1,
|
||||
"type": 2,
|
||||
"json_name": "fff"
|
||||
},
|
||||
{
|
||||
"name": "ggg",
|
||||
"number": 2,
|
||||
"label": 1,
|
||||
"type": 11,
|
||||
"type_name": ".dummy.Dummy1",
|
||||
"json_name": "ggg"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Dummy3"
|
||||
}
|
||||
],
|
||||
"service": [
|
||||
{
|
||||
"name": "DummyService",
|
||||
"method": [
|
||||
{
|
||||
"name": "Hhh",
|
||||
"input_type": ".dummy.Dummy1",
|
||||
"output_type": ".dummy.Dummy2",
|
||||
"options": {}
|
||||
},
|
||||
{
|
||||
"name": "Iii",
|
||||
"input_type": ".dummy.Dummy2",
|
||||
"output_type": ".dummy.Dummy1",
|
||||
"options": {}
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"source_code_info": {
|
||||
"location": [
|
||||
{
|
||||
"span": [
|
||||
0,
|
||||
0,
|
||||
22,
|
||||
1
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
12
|
||||
],
|
||||
"span": [
|
||||
0,
|
||||
0,
|
||||
18
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
2
|
||||
],
|
||||
"span": [
|
||||
2,
|
||||
8,
|
||||
13
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0
|
||||
],
|
||||
"span": [
|
||||
4,
|
||||
0,
|
||||
10,
|
||||
1
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
1
|
||||
],
|
||||
"span": [
|
||||
4,
|
||||
8,
|
||||
14
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
0
|
||||
],
|
||||
"span": [
|
||||
5,
|
||||
2,
|
||||
16
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
0,
|
||||
4
|
||||
],
|
||||
"span": [
|
||||
5,
|
||||
2,
|
||||
4,
|
||||
16
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
0,
|
||||
5
|
||||
],
|
||||
"span": [
|
||||
5,
|
||||
2,
|
||||
7
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
0,
|
||||
1
|
||||
],
|
||||
"span": [
|
||||
5,
|
||||
8,
|
||||
11
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
0,
|
||||
3
|
||||
],
|
||||
"span": [
|
||||
5,
|
||||
14,
|
||||
15
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
1
|
||||
],
|
||||
"span": [
|
||||
6,
|
||||
2,
|
||||
17
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
1,
|
||||
4
|
||||
],
|
||||
"span": [
|
||||
6,
|
||||
2,
|
||||
5,
|
||||
16
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
1,
|
||||
5
|
||||
],
|
||||
"span": [
|
||||
6,
|
||||
2,
|
||||
8
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
1,
|
||||
1
|
||||
],
|
||||
"span": [
|
||||
6,
|
||||
9,
|
||||
12
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
1,
|
||||
3
|
||||
],
|
||||
"span": [
|
||||
6,
|
||||
15,
|
||||
16
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
2
|
||||
],
|
||||
"span": [
|
||||
7,
|
||||
2,
|
||||
16
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
2,
|
||||
4
|
||||
],
|
||||
"span": [
|
||||
7,
|
||||
2,
|
||||
6,
|
||||
17
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
2,
|
||||
5
|
||||
],
|
||||
"span": [
|
||||
7,
|
||||
2,
|
||||
7
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
2,
|
||||
1
|
||||
],
|
||||
"span": [
|
||||
7,
|
||||
8,
|
||||
11
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
2,
|
||||
3
|
||||
],
|
||||
"span": [
|
||||
7,
|
||||
14,
|
||||
15
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
3
|
||||
],
|
||||
"span": [
|
||||
8,
|
||||
2,
|
||||
16
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
3,
|
||||
4
|
||||
],
|
||||
"span": [
|
||||
8,
|
||||
2,
|
||||
7,
|
||||
16
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
3,
|
||||
5
|
||||
],
|
||||
"span": [
|
||||
8,
|
||||
2,
|
||||
7
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
3,
|
||||
1
|
||||
],
|
||||
"span": [
|
||||
8,
|
||||
8,
|
||||
11
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
3,
|
||||
3
|
||||
],
|
||||
"span": [
|
||||
8,
|
||||
14,
|
||||
15
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
4
|
||||
],
|
||||
"span": [
|
||||
9,
|
||||
2,
|
||||
26
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
4,
|
||||
4
|
||||
],
|
||||
"span": [
|
||||
9,
|
||||
2,
|
||||
10
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
4,
|
||||
5
|
||||
],
|
||||
"span": [
|
||||
9,
|
||||
11,
|
||||
17
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
4,
|
||||
1
|
||||
],
|
||||
"span": [
|
||||
9,
|
||||
18,
|
||||
21
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
0,
|
||||
2,
|
||||
4,
|
||||
3
|
||||
],
|
||||
"span": [
|
||||
9,
|
||||
24,
|
||||
25
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
1
|
||||
],
|
||||
"span": [
|
||||
12,
|
||||
0,
|
||||
15,
|
||||
1
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
1,
|
||||
1
|
||||
],
|
||||
"span": [
|
||||
12,
|
||||
8,
|
||||
14
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
1,
|
||||
2,
|
||||
0
|
||||
],
|
||||
"span": [
|
||||
13,
|
||||
2,
|
||||
16
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
1,
|
||||
2,
|
||||
0,
|
||||
4
|
||||
],
|
||||
"span": [
|
||||
13,
|
||||
2,
|
||||
12,
|
||||
16
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
1,
|
||||
2,
|
||||
0,
|
||||
5
|
||||
],
|
||||
"span": [
|
||||
13,
|
||||
2,
|
||||
7
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
1,
|
||||
2,
|
||||
0,
|
||||
1
|
||||
],
|
||||
"span": [
|
||||
13,
|
||||
8,
|
||||
11
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
1,
|
||||
2,
|
||||
0,
|
||||
3
|
||||
],
|
||||
"span": [
|
||||
13,
|
||||
14,
|
||||
15
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
1,
|
||||
2,
|
||||
1
|
||||
],
|
||||
"span": [
|
||||
14,
|
||||
2,
|
||||
17
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
1,
|
||||
2,
|
||||
1,
|
||||
4
|
||||
],
|
||||
"span": [
|
||||
14,
|
||||
2,
|
||||
13,
|
||||
16
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
1,
|
||||
2,
|
||||
1,
|
||||
6
|
||||
],
|
||||
"span": [
|
||||
14,
|
||||
2,
|
||||
8
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
1,
|
||||
2,
|
||||
1,
|
||||
1
|
||||
],
|
||||
"span": [
|
||||
14,
|
||||
9,
|
||||
12
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
1,
|
||||
2,
|
||||
1,
|
||||
3
|
||||
],
|
||||
"span": [
|
||||
14,
|
||||
15,
|
||||
16
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
2
|
||||
],
|
||||
"span": [
|
||||
17,
|
||||
0,
|
||||
17
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
4,
|
||||
2,
|
||||
1
|
||||
],
|
||||
"span": [
|
||||
17,
|
||||
8,
|
||||
14
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
6,
|
||||
0
|
||||
],
|
||||
"span": [
|
||||
19,
|
||||
0,
|
||||
22,
|
||||
1
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
6,
|
||||
0,
|
||||
1
|
||||
],
|
||||
"span": [
|
||||
19,
|
||||
8,
|
||||
20
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
6,
|
||||
0,
|
||||
2,
|
||||
0
|
||||
],
|
||||
"span": [
|
||||
20,
|
||||
2,
|
||||
37
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
6,
|
||||
0,
|
||||
2,
|
||||
0,
|
||||
1
|
||||
],
|
||||
"span": [
|
||||
20,
|
||||
6,
|
||||
9
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
6,
|
||||
0,
|
||||
2,
|
||||
0,
|
||||
2
|
||||
],
|
||||
"span": [
|
||||
20,
|
||||
10,
|
||||
16
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
6,
|
||||
0,
|
||||
2,
|
||||
0,
|
||||
3
|
||||
],
|
||||
"span": [
|
||||
20,
|
||||
27,
|
||||
33
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
6,
|
||||
0,
|
||||
2,
|
||||
1
|
||||
],
|
||||
"span": [
|
||||
21,
|
||||
2,
|
||||
37
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
6,
|
||||
0,
|
||||
2,
|
||||
1,
|
||||
1
|
||||
],
|
||||
"span": [
|
||||
21,
|
||||
6,
|
||||
9
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
6,
|
||||
0,
|
||||
2,
|
||||
1,
|
||||
2
|
||||
],
|
||||
"span": [
|
||||
21,
|
||||
10,
|
||||
16
|
||||
]
|
||||
},
|
||||
{
|
||||
"path": [
|
||||
6,
|
||||
0,
|
||||
2,
|
||||
1,
|
||||
3
|
||||
],
|
||||
"span": [
|
||||
21,
|
||||
27,
|
||||
33
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"syntax": "proto3"
|
||||
},
|
||||
"raw-filename": "export.json.tmpl",
|
||||
"filename": "export.json.tmpl",
|
||||
"template-dir": "templates",
|
||||
"service": {
|
||||
"name": "DummyService",
|
||||
"method": [
|
||||
{
|
||||
"name": "Hhh",
|
||||
"input_type": ".dummy.Dummy1",
|
||||
"output_type": ".dummy.Dummy2",
|
||||
"options": {}
|
||||
},
|
||||
{
|
||||
"name": "Iii",
|
||||
"input_type": ".dummy.Dummy2",
|
||||
"output_type": ".dummy.Dummy1",
|
||||
"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"
|
||||
]
|
||||
}
|
1
examples/dummy/templates/export.json.tmpl
Normal file
1
examples/dummy/templates/export.json.tmpl
Normal file
@@ -0,0 +1 @@
|
||||
{{ . | prettyjson }}
|
13
examples/flow/Makefile
Normal file
13
examples/flow/Makefile
Normal file
@@ -0,0 +1,13 @@
|
||||
.PHONY: build
|
||||
build:
|
||||
mkdir -p output
|
||||
protoc -I. --gotemplate_out=template_dir=templates,debug=true:output ./protos/*.proto
|
||||
|
||||
|
||||
.PHONY: re
|
||||
re: clean build
|
||||
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -rf output
|
204
examples/flow/output/test/test_grpc_js.js
Normal file
204
examples/flow/output/test/test_grpc_js.js
Normal file
@@ -0,0 +1,204 @@
|
||||
// @flow
|
||||
// GENERATED CODE -- DO NOT EDIT!
|
||||
|
||||
import grpc from 'grpc'
|
||||
import pbFile from './pbFile.js'
|
||||
|
||||
export type TestEnum =
|
||||
| 'ELEMENT_A'
|
||||
| 'ELEMENT_B'
|
||||
;
|
||||
|
||||
export type TestMessage = {|
|
||||
a?: string;
|
||||
b?: number;
|
||||
c?: number;
|
||||
d?: number;
|
||||
e?: number;
|
||||
n?: Array<string>;
|
||||
o?: Array<number>;
|
||||
p?: Array<number>;
|
||||
q?: Array<number>;
|
||||
r?: Array<number>;
|
||||
s?:
|
||||
| 'ELEMENT_C'
|
||||
| 'ELEMENT_D'
|
||||
;
|
||||
|};
|
||||
|
||||
export type TestNoStreamRequest = {|
|
||||
message?: TestMessage;
|
||||
|};
|
||||
|
||||
export type TestNoStreamReply = {|
|
||||
message?: TestMessage;
|
||||
err_msg?: string;
|
||||
|};
|
||||
|
||||
export type TestStreamRequestRequest = {|
|
||||
message?: TestMessage;
|
||||
|};
|
||||
|
||||
export type TestStreamRequestReply = {|
|
||||
message?: TestMessage;
|
||||
err_msg?: string;
|
||||
|};
|
||||
|
||||
export type TestStreamReplyRequest = {|
|
||||
message?: TestMessage;
|
||||
|};
|
||||
|
||||
export type TestStreamReplyReply = {|
|
||||
message?: TestMessage;
|
||||
err_msg?: string;
|
||||
|};
|
||||
|
||||
export type TestStreamBothRequest = {|
|
||||
message?: TestMessage;
|
||||
|};
|
||||
|
||||
export type TestStreamBothReply = {|
|
||||
message?: TestMessage;
|
||||
err_msg?: string;
|
||||
|};
|
||||
|
||||
|
||||
function serialize_test_TestNoStreamRequest(arg: TestNoStreamRequest) {
|
||||
if (!(arg instanceof pbFile.TestNoStreamRequest)) {
|
||||
throw new Error('Expected argument of type TestNoStreamRequest')
|
||||
}
|
||||
return new Buffer(arg.serializeBinary())
|
||||
}
|
||||
|
||||
function deserialize_test_TestNoStreamRequest(buffer_arg: Array<number>) {
|
||||
return pbFile.TestNoStreamRequest.deserializeBinary(new Uint8Array(buffer_arg))
|
||||
}
|
||||
|
||||
function serialize_test_TestNoStreamReply(arg: TestNoStreamReply) {
|
||||
if (!(arg instanceof pbFile.TestNoStreamReply)) {
|
||||
throw new Error('Expected argument of type TestNoStreamReply')
|
||||
}
|
||||
return new Buffer(arg.serializeBinary())
|
||||
}
|
||||
|
||||
function deserialize_test_TestNoStreamReply(buffer_arg: Array<number>) {
|
||||
return pbFile.TestNoStreamReply.deserializeBinary(new Uint8Array(buffer_arg))
|
||||
}
|
||||
|
||||
function serialize_test_TestStreamRequestRequest(arg: TestStreamRequestRequest) {
|
||||
if (!(arg instanceof pbFile.TestStreamRequestRequest)) {
|
||||
throw new Error('Expected argument of type TestStreamRequestRequest')
|
||||
}
|
||||
return new Buffer(arg.serializeBinary())
|
||||
}
|
||||
|
||||
function deserialize_test_TestStreamRequestRequest(buffer_arg: Array<number>) {
|
||||
return pbFile.TestStreamRequestRequest.deserializeBinary(new Uint8Array(buffer_arg))
|
||||
}
|
||||
|
||||
function serialize_test_TestStreamRequestReply(arg: TestStreamRequestReply) {
|
||||
if (!(arg instanceof pbFile.TestStreamRequestReply)) {
|
||||
throw new Error('Expected argument of type TestStreamRequestReply')
|
||||
}
|
||||
return new Buffer(arg.serializeBinary())
|
||||
}
|
||||
|
||||
function deserialize_test_TestStreamRequestReply(buffer_arg: Array<number>) {
|
||||
return pbFile.TestStreamRequestReply.deserializeBinary(new Uint8Array(buffer_arg))
|
||||
}
|
||||
|
||||
function serialize_test_TestStreamReplyRequest(arg: TestStreamReplyRequest) {
|
||||
if (!(arg instanceof pbFile.TestStreamReplyRequest)) {
|
||||
throw new Error('Expected argument of type TestStreamReplyRequest')
|
||||
}
|
||||
return new Buffer(arg.serializeBinary())
|
||||
}
|
||||
|
||||
function deserialize_test_TestStreamReplyRequest(buffer_arg: Array<number>) {
|
||||
return pbFile.TestStreamReplyRequest.deserializeBinary(new Uint8Array(buffer_arg))
|
||||
}
|
||||
|
||||
function serialize_test_TestStreamReplyReply(arg: TestStreamReplyReply) {
|
||||
if (!(arg instanceof pbFile.TestStreamReplyReply)) {
|
||||
throw new Error('Expected argument of type TestStreamReplyReply')
|
||||
}
|
||||
return new Buffer(arg.serializeBinary())
|
||||
}
|
||||
|
||||
function deserialize_test_TestStreamReplyReply(buffer_arg: Array<number>) {
|
||||
return pbFile.TestStreamReplyReply.deserializeBinary(new Uint8Array(buffer_arg))
|
||||
}
|
||||
|
||||
function serialize_test_TestStreamBothRequest(arg: TestStreamBothRequest) {
|
||||
if (!(arg instanceof pbFile.TestStreamBothRequest)) {
|
||||
throw new Error('Expected argument of type TestStreamBothRequest')
|
||||
}
|
||||
return new Buffer(arg.serializeBinary())
|
||||
}
|
||||
|
||||
function deserialize_test_TestStreamBothRequest(buffer_arg: Array<number>) {
|
||||
return pbFile.TestStreamBothRequest.deserializeBinary(new Uint8Array(buffer_arg))
|
||||
}
|
||||
|
||||
function serialize_test_TestStreamBothReply(arg: TestStreamBothReply) {
|
||||
if (!(arg instanceof pbFile.TestStreamBothReply)) {
|
||||
throw new Error('Expected argument of type TestStreamBothReply')
|
||||
}
|
||||
return new Buffer(arg.serializeBinary())
|
||||
}
|
||||
|
||||
function deserialize_test_TestStreamBothReply(buffer_arg: Array<number>) {
|
||||
return pbFile.TestStreamBothReply.deserializeBinary(new Uint8Array(buffer_arg))
|
||||
}
|
||||
|
||||
|
||||
export const TestServiceService = {
|
||||
|
||||
testNoStream: {
|
||||
path: '/test.TestService/TestNoStream',
|
||||
requestStream: false,
|
||||
responseStream: false,
|
||||
requestType: pbFile.TestNoStreamRequest,
|
||||
responseType: pbFile.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: pbFile.TestStreamRequestRequest,
|
||||
responseType: pbFile.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: pbFile.TestStreamReplyRequest,
|
||||
responseType: pbFile.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: pbFile.TestStreamBothRequest,
|
||||
responseType: pbFile.TestStreamBothReply,
|
||||
requestSerialize: serialize_test_TestStreamBothRequest,
|
||||
requestDeserialize: deserialize_test_TestStreamBothRequest,
|
||||
responseSerialize: serialize_test_TestStreamBothReply,
|
||||
responseDeserialize: deserialize_test_TestStreamBothReply,
|
||||
},
|
||||
|
||||
}
|
||||
|
||||
export const TestServiceClient = grpc.makeGenericClientConstructor(TestServiceService)
|
43
examples/flow/protos/test.proto
Normal file
43
examples/flow/protos/test.proto
Normal file
@@ -0,0 +1,43 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package test;
|
||||
|
||||
option go_package = "github.com/united-drivers/models/go/test;testpb";
|
||||
|
||||
service TestService {
|
||||
rpc TestNoStream(TestNoStreamRequest) returns (TestNoStreamReply);
|
||||
rpc TestStreamRequest(stream TestStreamRequestRequest) returns (TestStreamRequestReply);
|
||||
rpc TestStreamReply(TestStreamReplyRequest) returns (stream TestStreamReplyReply);
|
||||
rpc TestStreamBoth(stream TestStreamBothRequest) returns (stream TestStreamBothReply);
|
||||
}
|
||||
|
||||
enum TestEnum {
|
||||
ELEMENT_A = 0;
|
||||
ELEMENT_B = 1;
|
||||
}
|
||||
|
||||
message TestMessage {
|
||||
string a = 1;
|
||||
int32 b = 2;
|
||||
int64 c = 3;
|
||||
float d = 4;
|
||||
double e = 5;
|
||||
repeated string n = 14;
|
||||
repeated int32 o = 15;
|
||||
repeated int64 p = 16;
|
||||
repeated float q = 17;
|
||||
repeated double r = 18;
|
||||
enum s {
|
||||
ELEMENT_C = 0;
|
||||
ELEMENT_D = 1;
|
||||
}
|
||||
}
|
||||
|
||||
message TestNoStreamRequest { TestMessage message = 1; }
|
||||
message TestNoStreamReply { TestMessage message = 1; string err_msg = 2; }
|
||||
message TestStreamRequestRequest { TestMessage message = 1; }
|
||||
message TestStreamRequestReply { TestMessage message = 1; string err_msg = 2; }
|
||||
message TestStreamReplyRequest { TestMessage message = 1; }
|
||||
message TestStreamReplyReply { TestMessage message = 1; string err_msg = 2; }
|
||||
message TestStreamBothRequest { TestMessage message = 1; }
|
||||
message TestStreamBothReply { TestMessage message = 1; string err_msg = 2; }
|
@@ -0,0 +1,58 @@
|
||||
// @flow
|
||||
// GENERATED CODE -- DO NOT EDIT!
|
||||
{{$Package:=.File.Package}}
|
||||
import grpc from 'grpc'
|
||||
import pbFile from './pbFile.js'
|
||||
{{range .File.EnumType}}
|
||||
export type {{.Name}} = {{range .Value}}
|
||||
| '{{.Name}}'{{end}}
|
||||
;{{end}}
|
||||
{{range .File.MessageType}}
|
||||
export type {{.Name}} = {|{{range .Field}}
|
||||
{{.Name}}?: {{. | jsType}};{{end}}{{range .EnumType}}
|
||||
{{.Name}}?:{{range .Value}}
|
||||
| '{{.Name}}'{{end}}
|
||||
;{{end}}
|
||||
|};
|
||||
{{end}}
|
||||
{{range .File.Service}}{{range .Method}}
|
||||
function serialize_{{$Package}}_{{.InputType | shortType}}(arg: {{.InputType | shortType}}) {
|
||||
if (!(arg instanceof pbFile.{{.InputType | shortType}})) {
|
||||
throw new Error('Expected argument of type {{.InputType | shortType}}')
|
||||
}
|
||||
return new Buffer(arg.serializeBinary())
|
||||
}
|
||||
|
||||
function deserialize_{{$Package}}_{{.InputType | shortType}}(buffer_arg: Array<number>) {
|
||||
return pbFile.{{.InputType | shortType}}.deserializeBinary(new Uint8Array(buffer_arg))
|
||||
}
|
||||
|
||||
function serialize_{{$Package}}_{{.OutputType | shortType}}(arg: {{.OutputType | shortType}}) {
|
||||
if (!(arg instanceof pbFile.{{.OutputType | shortType}})) {
|
||||
throw new Error('Expected argument of type {{.OutputType | shortType}}')
|
||||
}
|
||||
return new Buffer(arg.serializeBinary())
|
||||
}
|
||||
|
||||
function deserialize_{{$Package}}_{{.OutputType | shortType}}(buffer_arg: Array<number>) {
|
||||
return pbFile.{{.OutputType | shortType}}.deserializeBinary(new Uint8Array(buffer_arg))
|
||||
}
|
||||
{{end}}{{end}}
|
||||
{{range .File.Service}}
|
||||
export const {{.Name}}Service = {
|
||||
{{$serviceName:=.Name}}
|
||||
{{range .Method}}{{.Name | lowerCamelCase}}: {
|
||||
path: '/{{$Package}}.{{$serviceName}}/{{.Name}}',
|
||||
requestStream: {{.ClientStreaming | default "false"}},
|
||||
responseStream: {{.ServerStreaming | default "false"}},
|
||||
requestType: pbFile.{{.InputType | shortType}},
|
||||
responseType: pbFile.{{.OutputType | shortType}},
|
||||
requestSerialize: serialize_{{$Package}}_{{.InputType | shortType}},
|
||||
requestDeserialize: deserialize_{{$Package}}_{{.InputType | shortType}},
|
||||
responseSerialize: serialize_{{$Package}}_{{.OutputType | shortType}},
|
||||
responseDeserialize: deserialize_{{$Package}}_{{.OutputType | shortType}},
|
||||
},
|
||||
{{end}}
|
||||
}
|
||||
|
||||
export const {{.Name}}Client = grpc.makeGenericClientConstructor({{.Name}}Service){{end}}
|
1
examples/go-kit/.gitignore
vendored
Normal file
1
examples/go-kit/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/server
|
23
examples/go-kit/Makefile
Normal file
23
examples/go-kit/Makefile
Normal file
@@ -0,0 +1,23 @@
|
||||
SOURCES := $(shell find . -name "*.proto" -not -path ./vendor/\*)
|
||||
|
||||
TARGETS_GO := $(foreach source, $(SOURCES), $(source)_go)
|
||||
TARGETS_TMPL := $(foreach source, $(SOURCES), $(source)_tmpl)
|
||||
|
||||
service_name = $(word 2,$(subst /, ,$1))
|
||||
|
||||
.PHONY: build
|
||||
build: server
|
||||
|
||||
server: $(TARGETS_GO) $(TARGETS_TMPL)
|
||||
go build -o server .
|
||||
|
||||
$(TARGETS_GO): %_go:
|
||||
protoc --gogo_out=plugins=grpc:. "$*"
|
||||
@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
|
||||
|
||||
$(TARGETS_TMPL): %_tmpl:
|
||||
@mkdir -p $(dir $*)gen
|
||||
protoc -I. --gotemplate_out=destination_dir=services/$(call service_name,$*)/gen,template_dir=templates:services "$*"
|
||||
@rm -rf services/services # need to investigate why this directory is created
|
||||
gofmt -w $(dir $*)gen
|
95
examples/go-kit/main.go
Normal file
95
examples/go-kit/main.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/go-kit/kit/log"
|
||||
"github.com/gorilla/handlers"
|
||||
"google.golang.org/grpc"
|
||||
|
||||
session_svc "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session"
|
||||
session_endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/endpoints"
|
||||
session_pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/pb"
|
||||
session_grpctransport "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/transports/grpc"
|
||||
session_httptransport "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/transports/http"
|
||||
|
||||
sprint_svc "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint"
|
||||
sprint_endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/endpoints"
|
||||
sprint_pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/pb"
|
||||
sprint_grpctransport "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/transports/grpc"
|
||||
sprint_httptransport "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/transports/http"
|
||||
|
||||
user_svc "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user"
|
||||
user_endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/endpoints"
|
||||
user_pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/pb"
|
||||
user_grpctransport "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/transports/grpc"
|
||||
user_httptransport "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/transports/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
mux := http.NewServeMux()
|
||||
ctx := context.Background()
|
||||
errc := make(chan error)
|
||||
s := grpc.NewServer()
|
||||
var logger log.Logger
|
||||
{
|
||||
logger = log.NewLogfmtLogger(os.Stdout)
|
||||
logger = log.NewContext(logger).With("ts", log.DefaultTimestampUTC)
|
||||
logger = log.NewContext(logger).With("caller", log.DefaultCaller)
|
||||
}
|
||||
|
||||
// initialize services
|
||||
{
|
||||
svc := session_svc.New()
|
||||
endpoints := session_endpoints.MakeEndpoints(svc)
|
||||
srv := session_grpctransport.MakeGRPCServer(ctx, endpoints)
|
||||
session_pb.RegisterSessionServiceServer(s, srv)
|
||||
session_httptransport.RegisterHandlers(ctx, svc, mux, endpoints)
|
||||
}
|
||||
{
|
||||
svc := sprint_svc.New()
|
||||
endpoints := sprint_endpoints.MakeEndpoints(svc)
|
||||
srv := sprint_grpctransport.MakeGRPCServer(ctx, endpoints)
|
||||
sprint_pb.RegisterSprintServiceServer(s, srv)
|
||||
sprint_httptransport.RegisterHandlers(ctx, svc, mux, endpoints)
|
||||
}
|
||||
{
|
||||
svc := user_svc.New()
|
||||
endpoints := user_endpoints.MakeEndpoints(svc)
|
||||
srv := user_grpctransport.MakeGRPCServer(ctx, endpoints)
|
||||
user_pb.RegisterUserServiceServer(s, srv)
|
||||
user_httptransport.RegisterHandlers(ctx, svc, mux, endpoints)
|
||||
}
|
||||
|
||||
// start servers
|
||||
go func() {
|
||||
c := make(chan os.Signal, 1)
|
||||
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
||||
errc <- fmt.Errorf("%s", <-c)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
logger := log.NewContext(logger).With("transport", "HTTP")
|
||||
logger.Log("addr", ":8000")
|
||||
errc <- http.ListenAndServe(":8000", handlers.LoggingHandler(os.Stderr, mux))
|
||||
}()
|
||||
|
||||
go func() {
|
||||
logger := log.NewContext(logger).With("transport", "gRPC")
|
||||
ln, err := net.Listen("tcp", ":9000")
|
||||
if err != nil {
|
||||
errc <- err
|
||||
return
|
||||
}
|
||||
logger.Log("addr", ":9000")
|
||||
errc <- s.Serve(ln)
|
||||
}()
|
||||
|
||||
logger.Log("exit", <-errc)
|
||||
}
|
0
examples/go-kit/services/session/gen/README.md
Normal file
0
examples/go-kit/services/session/gen/README.md
Normal file
44
examples/go-kit/services/session/gen/client/grpc/client.go
Normal file
44
examples/go-kit/services/session/gen/client/grpc/client.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package session_clientgrpc
|
||||
|
||||
import (
|
||||
jwt "github.com/go-kit/kit/auth/jwt"
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
"github.com/go-kit/kit/log"
|
||||
grpctransport "github.com/go-kit/kit/transport/grpc"
|
||||
context "golang.org/x/net/context"
|
||||
"google.golang.org/grpc"
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
func New(conn *grpc.ClientConn, logger log.Logger) pb.SessionServiceServer {
|
||||
|
||||
var loginEndpoint endpoint.Endpoint
|
||||
{
|
||||
loginEndpoint = grpctransport.NewClient(
|
||||
conn,
|
||||
"session.SessionService",
|
||||
"Login",
|
||||
EncodeLoginRequest,
|
||||
DecodeLoginResponse,
|
||||
pb.LoginResponse{},
|
||||
append([]grpctransport.ClientOption{}, grpctransport.ClientBefore(jwt.FromGRPCContext()))...,
|
||||
).Endpoint()
|
||||
}
|
||||
|
||||
return &endpoints.Endpoints{
|
||||
|
||||
LoginEndpoint: loginEndpoint,
|
||||
}
|
||||
}
|
||||
|
||||
func EncodeLoginRequest(_ context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(*pb.LoginRequest)
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func DecodeLoginResponse(_ context.Context, grpcResponse interface{}) (interface{}, error) {
|
||||
response := grpcResponse.(*pb.LoginResponse)
|
||||
return response, nil
|
||||
}
|
43
examples/go-kit/services/session/gen/endpoints/endpoints.go
Normal file
43
examples/go-kit/services/session/gen/endpoints/endpoints.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package session_endpoints
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/pb"
|
||||
context "golang.org/x/net/context"
|
||||
)
|
||||
|
||||
var _ = fmt.Errorf
|
||||
|
||||
type StreamEndpoint func(server interface{}, req interface{}) (err error)
|
||||
|
||||
type Endpoints struct {
|
||||
LoginEndpoint endpoint.Endpoint
|
||||
}
|
||||
|
||||
func (e *Endpoints) Login(ctx context.Context, in *pb.LoginRequest) (*pb.LoginResponse, error) {
|
||||
out, err := e.LoginEndpoint(ctx, in)
|
||||
if err != nil {
|
||||
return &pb.LoginResponse{ErrMsg: err.Error()}, err
|
||||
}
|
||||
return out.(*pb.LoginResponse), err
|
||||
}
|
||||
|
||||
func MakeLoginEndpoint(svc pb.SessionServiceServer) endpoint.Endpoint {
|
||||
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(*pb.LoginRequest)
|
||||
rep, err := svc.Login(ctx, req)
|
||||
if err != nil {
|
||||
return &pb.LoginResponse{ErrMsg: err.Error()}, err
|
||||
}
|
||||
return rep, nil
|
||||
}
|
||||
}
|
||||
|
||||
func MakeEndpoints(svc pb.SessionServiceServer) Endpoints {
|
||||
return Endpoints{
|
||||
|
||||
LoginEndpoint: MakeLoginEndpoint(svc),
|
||||
}
|
||||
}
|
178
examples/go-kit/services/session/gen/pb/pb.go
Normal file
178
examples/go-kit/services/session/gen/pb/pb.go
Normal file
@@ -0,0 +1,178 @@
|
||||
// Code generated by protoc-gen-gogo.
|
||||
// source: services/session/session.proto
|
||||
// DO NOT EDIT!
|
||||
|
||||
/*
|
||||
Package session is a generated protocol buffer package.
|
||||
|
||||
It is generated from these files:
|
||||
services/session/session.proto
|
||||
|
||||
It has these top-level messages:
|
||||
LoginRequest
|
||||
LoginResponse
|
||||
*/
|
||||
package session
|
||||
|
||||
import proto "github.com/gogo/protobuf/proto"
|
||||
import fmt "fmt"
|
||||
import math "math"
|
||||
|
||||
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.GoGoProtoPackageIsVersion2 // please upgrade the proto package
|
||||
|
||||
type LoginRequest struct {
|
||||
Username string `protobuf:"bytes,1,opt,name=username,proto3" json:"username,omitempty"`
|
||||
Password string `protobuf:"bytes,2,opt,name=password,proto3" json:"password,omitempty"`
|
||||
}
|
||||
|
||||
func (m *LoginRequest) Reset() { *m = LoginRequest{} }
|
||||
func (m *LoginRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*LoginRequest) ProtoMessage() {}
|
||||
func (*LoginRequest) Descriptor() ([]byte, []int) { return fileDescriptorSession, []int{0} }
|
||||
|
||||
func (m *LoginRequest) GetUsername() string {
|
||||
if m != nil {
|
||||
return m.Username
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *LoginRequest) GetPassword() string {
|
||||
if m != nil {
|
||||
return m.Password
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type LoginResponse struct {
|
||||
Token string `protobuf:"bytes,1,opt,name=token,proto3" json:"token,omitempty"`
|
||||
ErrMsg string `protobuf:"bytes,2,opt,name=err_msg,json=errMsg,proto3" json:"err_msg,omitempty"`
|
||||
}
|
||||
|
||||
func (m *LoginResponse) Reset() { *m = LoginResponse{} }
|
||||
func (m *LoginResponse) String() string { return proto.CompactTextString(m) }
|
||||
func (*LoginResponse) ProtoMessage() {}
|
||||
func (*LoginResponse) Descriptor() ([]byte, []int) { return fileDescriptorSession, []int{1} }
|
||||
|
||||
func (m *LoginResponse) GetToken() string {
|
||||
if m != nil {
|
||||
return m.Token
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *LoginResponse) GetErrMsg() string {
|
||||
if m != nil {
|
||||
return m.ErrMsg
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*LoginRequest)(nil), "session.LoginRequest")
|
||||
proto.RegisterType((*LoginResponse)(nil), "session.LoginResponse")
|
||||
}
|
||||
|
||||
// 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 SessionService service
|
||||
|
||||
type SessionServiceClient interface {
|
||||
Login(ctx context.Context, in *LoginRequest, opts ...grpc.CallOption) (*LoginResponse, error)
|
||||
}
|
||||
|
||||
type sessionServiceClient struct {
|
||||
cc *grpc.ClientConn
|
||||
}
|
||||
|
||||
func NewSessionServiceClient(cc *grpc.ClientConn) SessionServiceClient {
|
||||
return &sessionServiceClient{cc}
|
||||
}
|
||||
|
||||
func (c *sessionServiceClient) Login(ctx context.Context, in *LoginRequest, opts ...grpc.CallOption) (*LoginResponse, error) {
|
||||
out := new(LoginResponse)
|
||||
err := grpc.Invoke(ctx, "/session.SessionService/Login", in, out, c.cc, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Server API for SessionService service
|
||||
|
||||
type SessionServiceServer interface {
|
||||
Login(context.Context, *LoginRequest) (*LoginResponse, error)
|
||||
}
|
||||
|
||||
func RegisterSessionServiceServer(s *grpc.Server, srv SessionServiceServer) {
|
||||
s.RegisterService(&_SessionService_serviceDesc, srv)
|
||||
}
|
||||
|
||||
func _SessionService_Login_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(LoginRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(SessionServiceServer).Login(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/session.SessionService/Login",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(SessionServiceServer).Login(ctx, req.(*LoginRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
var _SessionService_serviceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "session.SessionService",
|
||||
HandlerType: (*SessionServiceServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "Login",
|
||||
Handler: _SessionService_Login_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "services/session/session.proto",
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("services/session/session.proto", fileDescriptorSession) }
|
||||
|
||||
var fileDescriptorSession = []byte{
|
||||
// 188 bytes of a gzipped FileDescriptorProto
|
||||
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,
|
||||
0x45, 0xf9, 0x25, 0xf9, 0x42, 0xec, 0x50, 0xae, 0x92, 0x1b, 0x17, 0x8f, 0x4f, 0x7e, 0x7a, 0x66,
|
||||
0x5e, 0x50, 0x6a, 0x61, 0x69, 0x6a, 0x71, 0x89, 0x90, 0x14, 0x17, 0x47, 0x69, 0x71, 0x6a, 0x51,
|
||||
0x5e, 0x62, 0x6e, 0xaa, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0x67, 0x10, 0x9c, 0x0f, 0x92, 0x2b, 0x48,
|
||||
0x2c, 0x2e, 0x2e, 0xcf, 0x2f, 0x4a, 0x91, 0x60, 0x82, 0xc8, 0xc1, 0xf8, 0x4a, 0x76, 0x5c, 0xbc,
|
||||
0x50, 0x73, 0x8a, 0x0b, 0xf2, 0xf3, 0x8a, 0x53, 0x85, 0x44, 0xb8, 0x58, 0x4b, 0xf2, 0xb3, 0x53,
|
||||
0xf3, 0xa0, 0xa6, 0x40, 0x38, 0x42, 0xe2, 0x5c, 0xec, 0xa9, 0x45, 0x45, 0xf1, 0xb9, 0xc5, 0xe9,
|
||||
0x50, 0x13, 0xd8, 0x52, 0x8b, 0x8a, 0x7c, 0x8b, 0xd3, 0x8d, 0xbc, 0xb8, 0xf8, 0x82, 0x21, 0x4e,
|
||||
0x0a, 0x86, 0xb8, 0x5c, 0xc8, 0x82, 0x8b, 0x15, 0x6c, 0xa2, 0x90, 0xa8, 0x1e, 0xcc, 0xed, 0xc8,
|
||||
0x2e, 0x95, 0x12, 0x43, 0x17, 0x86, 0x58, 0xac, 0xc4, 0x90, 0xc4, 0x06, 0xf6, 0xa3, 0x31, 0x20,
|
||||
0x00, 0x00, 0xff, 0xff, 0x29, 0x3f, 0x91, 0xc7, 0x05, 0x01, 0x00, 0x00,
|
||||
}
|
64
examples/go-kit/services/session/gen/transports/grpc/grpc.go
Normal file
64
examples/go-kit/services/session/gen/transports/grpc/grpc.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package session_grpctransport
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
grpctransport "github.com/go-kit/kit/transport/grpc"
|
||||
context "golang.org/x/net/context"
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
// avoid import errors
|
||||
var _ = fmt.Errorf
|
||||
|
||||
func MakeGRPCServer(ctx context.Context, endpoints endpoints.Endpoints) pb.SessionServiceServer {
|
||||
options := []grpctransport.ServerOption{}
|
||||
return &grpcServer{
|
||||
|
||||
login: grpctransport.NewServer(
|
||||
ctx,
|
||||
endpoints.LoginEndpoint,
|
||||
decodeRequest,
|
||||
encodeLoginResponse,
|
||||
options...,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
type grpcServer struct {
|
||||
login grpctransport.Handler
|
||||
}
|
||||
|
||||
func (s *grpcServer) Login(ctx context.Context, req *pb.LoginRequest) (*pb.LoginResponse, error) {
|
||||
_, rep, err := s.login.ServeGRPC(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rep.(*pb.LoginResponse), nil
|
||||
}
|
||||
|
||||
func encodeLoginResponse(ctx context.Context, response interface{}) (interface{}, error) {
|
||||
resp := response.(*pb.LoginResponse)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func decodeRequest(ctx context.Context, grpcReq interface{}) (interface{}, error) {
|
||||
return grpcReq, nil
|
||||
}
|
||||
|
||||
type streamHandler interface {
|
||||
Do(server interface{}, req interface{}) (err error)
|
||||
}
|
||||
|
||||
type server struct {
|
||||
e endpoints.StreamEndpoint
|
||||
}
|
||||
|
||||
func (s server) Do(server interface{}, req interface{}) (err error) {
|
||||
if err := s.e(server, req); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
43
examples/go-kit/services/session/gen/transports/http/http.go
Normal file
43
examples/go-kit/services/session/gen/transports/http/http.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package session_httptransport
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
context "golang.org/x/net/context"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
gokit_endpoint "github.com/go-kit/kit/endpoint"
|
||||
httptransport "github.com/go-kit/kit/transport/http"
|
||||
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"
|
||||
)
|
||||
|
||||
func MakeLoginHandler(ctx context.Context, svc pb.SessionServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
||||
return httptransport.NewServer(
|
||||
ctx,
|
||||
endpoint,
|
||||
decodeLoginRequest,
|
||||
encodeResponse,
|
||||
[]httptransport.ServerOption{}...,
|
||||
)
|
||||
}
|
||||
|
||||
func decodeLoginRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
||||
var req pb.LoginRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &req, nil
|
||||
}
|
||||
|
||||
func encodeResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
|
||||
return json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func RegisterHandlers(ctx context.Context, svc pb.SessionServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error {
|
||||
|
||||
log.Println("new HTTP endpoint: \"/Login\" (service=Session)")
|
||||
mux.Handle("/Login", MakeLoginHandler(ctx, svc, endpoints.LoginEndpoint))
|
||||
|
||||
return nil
|
||||
}
|
19
examples/go-kit/services/session/service.go
Normal file
19
examples/go-kit/services/session/service.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package sessionsvc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/pb"
|
||||
)
|
||||
|
||||
type Service struct{}
|
||||
|
||||
func New() pb.SessionServiceServer {
|
||||
return &Service{}
|
||||
}
|
||||
|
||||
func (svc *Service) Login(ctx context.Context, in *pb.LoginRequest) (*pb.LoginResponse, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
17
examples/go-kit/services/session/session.proto
Normal file
17
examples/go-kit/services/session/session.proto
Normal file
@@ -0,0 +1,17 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package session;
|
||||
|
||||
service SessionService {
|
||||
rpc Login(LoginRequest) returns (LoginResponse) {}
|
||||
}
|
||||
|
||||
message LoginRequest {
|
||||
string username = 1;
|
||||
string password = 2;
|
||||
}
|
||||
|
||||
message LoginResponse {
|
||||
string token = 1;
|
||||
string err_msg = 2;
|
||||
}
|
0
examples/go-kit/services/sprint/gen/README.md
Normal file
0
examples/go-kit/services/sprint/gen/README.md
Normal file
94
examples/go-kit/services/sprint/gen/client/grpc/client.go
Normal file
94
examples/go-kit/services/sprint/gen/client/grpc/client.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package sprint_clientgrpc
|
||||
|
||||
import (
|
||||
jwt "github.com/go-kit/kit/auth/jwt"
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
"github.com/go-kit/kit/log"
|
||||
grpctransport "github.com/go-kit/kit/transport/grpc"
|
||||
context "golang.org/x/net/context"
|
||||
"google.golang.org/grpc"
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
func New(conn *grpc.ClientConn, logger log.Logger) pb.SprintServiceServer {
|
||||
|
||||
var addsprintEndpoint endpoint.Endpoint
|
||||
{
|
||||
addsprintEndpoint = grpctransport.NewClient(
|
||||
conn,
|
||||
"sprint.SprintService",
|
||||
"AddSprint",
|
||||
EncodeAddSprintRequest,
|
||||
DecodeAddSprintResponse,
|
||||
pb.AddSprintResponse{},
|
||||
append([]grpctransport.ClientOption{}, grpctransport.ClientBefore(jwt.FromGRPCContext()))...,
|
||||
).Endpoint()
|
||||
}
|
||||
|
||||
var closesprintEndpoint endpoint.Endpoint
|
||||
{
|
||||
closesprintEndpoint = grpctransport.NewClient(
|
||||
conn,
|
||||
"sprint.SprintService",
|
||||
"CloseSprint",
|
||||
EncodeCloseSprintRequest,
|
||||
DecodeCloseSprintResponse,
|
||||
pb.CloseSprintResponse{},
|
||||
append([]grpctransport.ClientOption{}, grpctransport.ClientBefore(jwt.FromGRPCContext()))...,
|
||||
).Endpoint()
|
||||
}
|
||||
|
||||
var getsprintEndpoint endpoint.Endpoint
|
||||
{
|
||||
getsprintEndpoint = grpctransport.NewClient(
|
||||
conn,
|
||||
"sprint.SprintService",
|
||||
"GetSprint",
|
||||
EncodeGetSprintRequest,
|
||||
DecodeGetSprintResponse,
|
||||
pb.GetSprintResponse{},
|
||||
append([]grpctransport.ClientOption{}, grpctransport.ClientBefore(jwt.FromGRPCContext()))...,
|
||||
).Endpoint()
|
||||
}
|
||||
|
||||
return &endpoints.Endpoints{
|
||||
|
||||
AddSprintEndpoint: addsprintEndpoint,
|
||||
|
||||
CloseSprintEndpoint: closesprintEndpoint,
|
||||
|
||||
GetSprintEndpoint: getsprintEndpoint,
|
||||
}
|
||||
}
|
||||
|
||||
func EncodeAddSprintRequest(_ context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(*pb.AddSprintRequest)
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func DecodeAddSprintResponse(_ context.Context, grpcResponse interface{}) (interface{}, error) {
|
||||
response := grpcResponse.(*pb.AddSprintResponse)
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func EncodeCloseSprintRequest(_ context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(*pb.CloseSprintRequest)
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func DecodeCloseSprintResponse(_ context.Context, grpcResponse interface{}) (interface{}, error) {
|
||||
response := grpcResponse.(*pb.CloseSprintResponse)
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func EncodeGetSprintRequest(_ context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(*pb.GetSprintRequest)
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func DecodeGetSprintResponse(_ context.Context, grpcResponse interface{}) (interface{}, error) {
|
||||
response := grpcResponse.(*pb.GetSprintResponse)
|
||||
return response, nil
|
||||
}
|
89
examples/go-kit/services/sprint/gen/endpoints/endpoints.go
Normal file
89
examples/go-kit/services/sprint/gen/endpoints/endpoints.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package sprint_endpoints
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/pb"
|
||||
context "golang.org/x/net/context"
|
||||
)
|
||||
|
||||
var _ = fmt.Errorf
|
||||
|
||||
type StreamEndpoint func(server interface{}, req interface{}) (err error)
|
||||
|
||||
type Endpoints struct {
|
||||
AddSprintEndpoint endpoint.Endpoint
|
||||
|
||||
CloseSprintEndpoint endpoint.Endpoint
|
||||
|
||||
GetSprintEndpoint endpoint.Endpoint
|
||||
}
|
||||
|
||||
func (e *Endpoints) AddSprint(ctx context.Context, in *pb.AddSprintRequest) (*pb.AddSprintResponse, error) {
|
||||
out, err := e.AddSprintEndpoint(ctx, in)
|
||||
if err != nil {
|
||||
return &pb.AddSprintResponse{ErrMsg: err.Error()}, err
|
||||
}
|
||||
return out.(*pb.AddSprintResponse), err
|
||||
}
|
||||
|
||||
func (e *Endpoints) CloseSprint(ctx context.Context, in *pb.CloseSprintRequest) (*pb.CloseSprintResponse, error) {
|
||||
out, err := e.CloseSprintEndpoint(ctx, in)
|
||||
if err != nil {
|
||||
return &pb.CloseSprintResponse{ErrMsg: err.Error()}, err
|
||||
}
|
||||
return out.(*pb.CloseSprintResponse), err
|
||||
}
|
||||
|
||||
func (e *Endpoints) GetSprint(ctx context.Context, in *pb.GetSprintRequest) (*pb.GetSprintResponse, error) {
|
||||
out, err := e.GetSprintEndpoint(ctx, in)
|
||||
if err != nil {
|
||||
return &pb.GetSprintResponse{ErrMsg: err.Error()}, err
|
||||
}
|
||||
return out.(*pb.GetSprintResponse), err
|
||||
}
|
||||
|
||||
func MakeAddSprintEndpoint(svc pb.SprintServiceServer) endpoint.Endpoint {
|
||||
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(*pb.AddSprintRequest)
|
||||
rep, err := svc.AddSprint(ctx, req)
|
||||
if err != nil {
|
||||
return &pb.AddSprintResponse{ErrMsg: err.Error()}, err
|
||||
}
|
||||
return rep, nil
|
||||
}
|
||||
}
|
||||
|
||||
func MakeCloseSprintEndpoint(svc pb.SprintServiceServer) endpoint.Endpoint {
|
||||
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(*pb.CloseSprintRequest)
|
||||
rep, err := svc.CloseSprint(ctx, req)
|
||||
if err != nil {
|
||||
return &pb.CloseSprintResponse{ErrMsg: err.Error()}, err
|
||||
}
|
||||
return rep, nil
|
||||
}
|
||||
}
|
||||
|
||||
func MakeGetSprintEndpoint(svc pb.SprintServiceServer) endpoint.Endpoint {
|
||||
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(*pb.GetSprintRequest)
|
||||
rep, err := svc.GetSprint(ctx, req)
|
||||
if err != nil {
|
||||
return &pb.GetSprintResponse{ErrMsg: err.Error()}, err
|
||||
}
|
||||
return rep, nil
|
||||
}
|
||||
}
|
||||
|
||||
func MakeEndpoints(svc pb.SprintServiceServer) Endpoints {
|
||||
return Endpoints{
|
||||
|
||||
AddSprintEndpoint: MakeAddSprintEndpoint(svc),
|
||||
|
||||
CloseSprintEndpoint: MakeCloseSprintEndpoint(svc),
|
||||
|
||||
GetSprintEndpoint: MakeGetSprintEndpoint(svc),
|
||||
}
|
||||
}
|
357
examples/go-kit/services/sprint/gen/pb/pb.go
Normal file
357
examples/go-kit/services/sprint/gen/pb/pb.go
Normal file
@@ -0,0 +1,357 @@
|
||||
// Code generated by protoc-gen-gogo.
|
||||
// source: services/sprint/sprint.proto
|
||||
// DO NOT EDIT!
|
||||
|
||||
/*
|
||||
Package sprint is a generated protocol buffer package.
|
||||
|
||||
It is generated from these files:
|
||||
services/sprint/sprint.proto
|
||||
|
||||
It has these top-level messages:
|
||||
AddSprintRequest
|
||||
AddSprintResponse
|
||||
CloseSprintRequest
|
||||
CloseSprintResponse
|
||||
GetSprintRequest
|
||||
GetSprintResponse
|
||||
Sprint
|
||||
*/
|
||||
package sprint
|
||||
|
||||
import proto "github.com/gogo/protobuf/proto"
|
||||
import fmt "fmt"
|
||||
import math "math"
|
||||
|
||||
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.GoGoProtoPackageIsVersion2 // please upgrade the proto package
|
||||
|
||||
type AddSprintRequest struct {
|
||||
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
|
||||
}
|
||||
|
||||
func (m *AddSprintRequest) Reset() { *m = AddSprintRequest{} }
|
||||
func (m *AddSprintRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*AddSprintRequest) ProtoMessage() {}
|
||||
func (*AddSprintRequest) Descriptor() ([]byte, []int) { return fileDescriptorSprint, []int{0} }
|
||||
|
||||
func (m *AddSprintRequest) GetName() string {
|
||||
if m != nil {
|
||||
return m.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type AddSprintResponse struct {
|
||||
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"`
|
||||
}
|
||||
|
||||
func (m *AddSprintResponse) Reset() { *m = AddSprintResponse{} }
|
||||
func (m *AddSprintResponse) String() string { return proto.CompactTextString(m) }
|
||||
func (*AddSprintResponse) ProtoMessage() {}
|
||||
func (*AddSprintResponse) Descriptor() ([]byte, []int) { return fileDescriptorSprint, []int{1} }
|
||||
|
||||
func (m *AddSprintResponse) GetSprint() *Sprint {
|
||||
if m != nil {
|
||||
return m.Sprint
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *AddSprintResponse) GetErrMsg() string {
|
||||
if m != nil {
|
||||
return m.ErrMsg
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type CloseSprintRequest struct {
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
}
|
||||
|
||||
func (m *CloseSprintRequest) Reset() { *m = CloseSprintRequest{} }
|
||||
func (m *CloseSprintRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*CloseSprintRequest) ProtoMessage() {}
|
||||
func (*CloseSprintRequest) Descriptor() ([]byte, []int) { return fileDescriptorSprint, []int{2} }
|
||||
|
||||
func (m *CloseSprintRequest) GetId() string {
|
||||
if m != nil {
|
||||
return m.Id
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type CloseSprintResponse struct {
|
||||
ErrMsg string `protobuf:"bytes,1,opt,name=err_msg,json=errMsg,proto3" json:"err_msg,omitempty"`
|
||||
}
|
||||
|
||||
func (m *CloseSprintResponse) Reset() { *m = CloseSprintResponse{} }
|
||||
func (m *CloseSprintResponse) String() string { return proto.CompactTextString(m) }
|
||||
func (*CloseSprintResponse) ProtoMessage() {}
|
||||
func (*CloseSprintResponse) Descriptor() ([]byte, []int) { return fileDescriptorSprint, []int{3} }
|
||||
|
||||
func (m *CloseSprintResponse) GetErrMsg() string {
|
||||
if m != nil {
|
||||
return m.ErrMsg
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type GetSprintRequest struct {
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
}
|
||||
|
||||
func (m *GetSprintRequest) Reset() { *m = GetSprintRequest{} }
|
||||
func (m *GetSprintRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*GetSprintRequest) ProtoMessage() {}
|
||||
func (*GetSprintRequest) Descriptor() ([]byte, []int) { return fileDescriptorSprint, []int{4} }
|
||||
|
||||
func (m *GetSprintRequest) GetId() string {
|
||||
if m != nil {
|
||||
return m.Id
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type GetSprintResponse struct {
|
||||
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"`
|
||||
}
|
||||
|
||||
func (m *GetSprintResponse) Reset() { *m = GetSprintResponse{} }
|
||||
func (m *GetSprintResponse) String() string { return proto.CompactTextString(m) }
|
||||
func (*GetSprintResponse) ProtoMessage() {}
|
||||
func (*GetSprintResponse) Descriptor() ([]byte, []int) { return fileDescriptorSprint, []int{5} }
|
||||
|
||||
func (m *GetSprintResponse) GetSprint() *Sprint {
|
||||
if m != nil {
|
||||
return m.Sprint
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *GetSprintResponse) GetErrMsg() string {
|
||||
if m != nil {
|
||||
return m.ErrMsg
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type Sprint struct {
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
CreatedAt uint32 `protobuf:"varint,2,opt,name=created_at,json=createdAt,proto3" json:"created_at,omitempty"`
|
||||
Name string `protobuf:"bytes,3,opt,name=name,proto3" json:"name,omitempty"`
|
||||
}
|
||||
|
||||
func (m *Sprint) Reset() { *m = Sprint{} }
|
||||
func (m *Sprint) String() string { return proto.CompactTextString(m) }
|
||||
func (*Sprint) ProtoMessage() {}
|
||||
func (*Sprint) Descriptor() ([]byte, []int) { return fileDescriptorSprint, []int{6} }
|
||||
|
||||
func (m *Sprint) GetId() string {
|
||||
if m != nil {
|
||||
return m.Id
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *Sprint) GetCreatedAt() uint32 {
|
||||
if m != nil {
|
||||
return m.CreatedAt
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (m *Sprint) GetName() string {
|
||||
if m != nil {
|
||||
return m.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*AddSprintRequest)(nil), "sprint.AddSprintRequest")
|
||||
proto.RegisterType((*AddSprintResponse)(nil), "sprint.AddSprintResponse")
|
||||
proto.RegisterType((*CloseSprintRequest)(nil), "sprint.CloseSprintRequest")
|
||||
proto.RegisterType((*CloseSprintResponse)(nil), "sprint.CloseSprintResponse")
|
||||
proto.RegisterType((*GetSprintRequest)(nil), "sprint.GetSprintRequest")
|
||||
proto.RegisterType((*GetSprintResponse)(nil), "sprint.GetSprintResponse")
|
||||
proto.RegisterType((*Sprint)(nil), "sprint.Sprint")
|
||||
}
|
||||
|
||||
// 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 SprintService service
|
||||
|
||||
type SprintServiceClient interface {
|
||||
AddSprint(ctx context.Context, in *AddSprintRequest, opts ...grpc.CallOption) (*AddSprintResponse, error)
|
||||
CloseSprint(ctx context.Context, in *CloseSprintRequest, opts ...grpc.CallOption) (*CloseSprintResponse, error)
|
||||
GetSprint(ctx context.Context, in *GetSprintRequest, opts ...grpc.CallOption) (*GetSprintResponse, error)
|
||||
}
|
||||
|
||||
type sprintServiceClient struct {
|
||||
cc *grpc.ClientConn
|
||||
}
|
||||
|
||||
func NewSprintServiceClient(cc *grpc.ClientConn) SprintServiceClient {
|
||||
return &sprintServiceClient{cc}
|
||||
}
|
||||
|
||||
func (c *sprintServiceClient) AddSprint(ctx context.Context, in *AddSprintRequest, opts ...grpc.CallOption) (*AddSprintResponse, error) {
|
||||
out := new(AddSprintResponse)
|
||||
err := grpc.Invoke(ctx, "/sprint.SprintService/AddSprint", in, out, c.cc, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *sprintServiceClient) CloseSprint(ctx context.Context, in *CloseSprintRequest, opts ...grpc.CallOption) (*CloseSprintResponse, error) {
|
||||
out := new(CloseSprintResponse)
|
||||
err := grpc.Invoke(ctx, "/sprint.SprintService/CloseSprint", in, out, c.cc, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *sprintServiceClient) GetSprint(ctx context.Context, in *GetSprintRequest, opts ...grpc.CallOption) (*GetSprintResponse, error) {
|
||||
out := new(GetSprintResponse)
|
||||
err := grpc.Invoke(ctx, "/sprint.SprintService/GetSprint", in, out, c.cc, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Server API for SprintService service
|
||||
|
||||
type SprintServiceServer interface {
|
||||
AddSprint(context.Context, *AddSprintRequest) (*AddSprintResponse, error)
|
||||
CloseSprint(context.Context, *CloseSprintRequest) (*CloseSprintResponse, error)
|
||||
GetSprint(context.Context, *GetSprintRequest) (*GetSprintResponse, error)
|
||||
}
|
||||
|
||||
func RegisterSprintServiceServer(s *grpc.Server, srv SprintServiceServer) {
|
||||
s.RegisterService(&_SprintService_serviceDesc, srv)
|
||||
}
|
||||
|
||||
func _SprintService_AddSprint_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(AddSprintRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(SprintServiceServer).AddSprint(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/sprint.SprintService/AddSprint",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(SprintServiceServer).AddSprint(ctx, req.(*AddSprintRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _SprintService_CloseSprint_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(CloseSprintRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(SprintServiceServer).CloseSprint(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/sprint.SprintService/CloseSprint",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(SprintServiceServer).CloseSprint(ctx, req.(*CloseSprintRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _SprintService_GetSprint_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(GetSprintRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(SprintServiceServer).GetSprint(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/sprint.SprintService/GetSprint",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(SprintServiceServer).GetSprint(ctx, req.(*GetSprintRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
var _SprintService_serviceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "sprint.SprintService",
|
||||
HandlerType: (*SprintServiceServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "AddSprint",
|
||||
Handler: _SprintService_AddSprint_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "CloseSprint",
|
||||
Handler: _SprintService_CloseSprint_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "GetSprint",
|
||||
Handler: _SprintService_GetSprint_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "services/sprint/sprint.proto",
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("services/sprint/sprint.proto", fileDescriptorSprint) }
|
||||
|
||||
var fileDescriptorSprint = []byte{
|
||||
// 290 bytes of a gzipped FileDescriptorProto
|
||||
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,
|
||||
0xea, 0x2f, 0x68, 0x3d, 0x28, 0x88, 0x97, 0xb4, 0xf7, 0x12, 0xbb, 0x43, 0x09, 0xd8, 0x24, 0xee,
|
||||
0xac, 0xfe, 0x5f, 0xff, 0x89, 0xb0, 0xbb, 0xcd, 0x57, 0x8b, 0x27, 0x4f, 0xc9, 0xec, 0xbc, 0x7d,
|
||||
0x6f, 0xe6, 0xbd, 0x85, 0x6b, 0x26, 0xf5, 0x9d, 0x6d, 0x88, 0x1f, 0xb8, 0x54, 0x59, 0xae, 0xdd,
|
||||
0x27, 0x2e, 0x55, 0xa1, 0x0b, 0xf4, 0x6d, 0x25, 0xa6, 0x30, 0x9e, 0x4b, 0xb9, 0x34, 0x45, 0x42,
|
||||
0x9f, 0x5f, 0xc4, 0x1a, 0x11, 0x4e, 0xf3, 0x74, 0x47, 0xa1, 0x77, 0xeb, 0xdd, 0x07, 0x89, 0xf9,
|
||||
0x17, 0x2b, 0x98, 0x34, 0x70, 0x5c, 0x16, 0x39, 0x13, 0x4e, 0xc1, 0xd1, 0x18, 0xe8, 0x60, 0x36,
|
||||
0x8a, 0x9d, 0x86, 0xc3, 0xb9, 0x2e, 0x5e, 0xc0, 0x19, 0x29, 0xb5, 0xde, 0xf1, 0x36, 0xec, 0x1b,
|
||||
0x4e, 0x9f, 0x94, 0x7a, 0xe3, 0xad, 0xb8, 0x03, 0x7c, 0xfa, 0x28, 0x98, 0xda, 0xfa, 0x23, 0xe8,
|
||||
0x67, 0xd2, 0xa9, 0xf7, 0x33, 0x29, 0x62, 0x38, 0x6f, 0xa1, 0x9c, 0x7a, 0x83, 0xd5, 0x6b, 0xb1,
|
||||
0x0a, 0x18, 0x3f, 0x93, 0xfe, 0x9b, 0x73, 0x05, 0x93, 0x06, 0xe6, 0xbf, 0xf6, 0x79, 0x05, 0xdf,
|
||||
0x42, 0xbb, 0x7a, 0x78, 0x03, 0xb0, 0x51, 0x94, 0x6a, 0x92, 0xeb, 0x54, 0x9b, 0x5b, 0xc3, 0x24,
|
||||
0x70, 0x27, 0xf3, 0xda, 0xf2, 0x93, 0xda, 0xf2, 0xd9, 0x8f, 0x07, 0x43, 0xcb, 0xb6, 0xb4, 0x49,
|
||||
0xe2, 0x02, 0x82, 0x2a, 0x04, 0x0c, 0xf7, 0xc3, 0x75, 0xf3, 0x8b, 0x2e, 0x8f, 0x74, 0xec, 0x86,
|
||||
0xa2, 0x87, 0x2f, 0x30, 0x68, 0x98, 0x89, 0xd1, 0x1e, 0x7b, 0x98, 0x43, 0x74, 0x75, 0xb4, 0x57,
|
||||
0x31, 0x2d, 0x20, 0xa8, 0x2c, 0xac, 0xa7, 0xe9, 0x3a, 0x5f, 0x4f, 0x73, 0xe0, 0xb7, 0xe8, 0xbd,
|
||||
0xfb, 0xe6, 0x35, 0x3e, 0xfe, 0x06, 0x00, 0x00, 0xff, 0xff, 0xf0, 0x9e, 0xb2, 0x1e, 0xad, 0x02,
|
||||
0x00, 0x00,
|
||||
}
|
110
examples/go-kit/services/sprint/gen/transports/grpc/grpc.go
Normal file
110
examples/go-kit/services/sprint/gen/transports/grpc/grpc.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package sprint_grpctransport
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
grpctransport "github.com/go-kit/kit/transport/grpc"
|
||||
context "golang.org/x/net/context"
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
// avoid import errors
|
||||
var _ = fmt.Errorf
|
||||
|
||||
func MakeGRPCServer(ctx context.Context, endpoints endpoints.Endpoints) pb.SprintServiceServer {
|
||||
options := []grpctransport.ServerOption{}
|
||||
return &grpcServer{
|
||||
|
||||
addsprint: grpctransport.NewServer(
|
||||
ctx,
|
||||
endpoints.AddSprintEndpoint,
|
||||
decodeRequest,
|
||||
encodeAddSprintResponse,
|
||||
options...,
|
||||
),
|
||||
|
||||
closesprint: grpctransport.NewServer(
|
||||
ctx,
|
||||
endpoints.CloseSprintEndpoint,
|
||||
decodeRequest,
|
||||
encodeCloseSprintResponse,
|
||||
options...,
|
||||
),
|
||||
|
||||
getsprint: grpctransport.NewServer(
|
||||
ctx,
|
||||
endpoints.GetSprintEndpoint,
|
||||
decodeRequest,
|
||||
encodeGetSprintResponse,
|
||||
options...,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
type grpcServer struct {
|
||||
addsprint grpctransport.Handler
|
||||
|
||||
closesprint grpctransport.Handler
|
||||
|
||||
getsprint grpctransport.Handler
|
||||
}
|
||||
|
||||
func (s *grpcServer) AddSprint(ctx context.Context, req *pb.AddSprintRequest) (*pb.AddSprintResponse, error) {
|
||||
_, rep, err := s.addsprint.ServeGRPC(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rep.(*pb.AddSprintResponse), nil
|
||||
}
|
||||
|
||||
func encodeAddSprintResponse(ctx context.Context, response interface{}) (interface{}, error) {
|
||||
resp := response.(*pb.AddSprintResponse)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *grpcServer) CloseSprint(ctx context.Context, req *pb.CloseSprintRequest) (*pb.CloseSprintResponse, error) {
|
||||
_, rep, err := s.closesprint.ServeGRPC(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rep.(*pb.CloseSprintResponse), nil
|
||||
}
|
||||
|
||||
func encodeCloseSprintResponse(ctx context.Context, response interface{}) (interface{}, error) {
|
||||
resp := response.(*pb.CloseSprintResponse)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *grpcServer) GetSprint(ctx context.Context, req *pb.GetSprintRequest) (*pb.GetSprintResponse, error) {
|
||||
_, rep, err := s.getsprint.ServeGRPC(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rep.(*pb.GetSprintResponse), nil
|
||||
}
|
||||
|
||||
func encodeGetSprintResponse(ctx context.Context, response interface{}) (interface{}, error) {
|
||||
resp := response.(*pb.GetSprintResponse)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func decodeRequest(ctx context.Context, grpcReq interface{}) (interface{}, error) {
|
||||
return grpcReq, nil
|
||||
}
|
||||
|
||||
type streamHandler interface {
|
||||
Do(server interface{}, req interface{}) (err error)
|
||||
}
|
||||
|
||||
type server struct {
|
||||
e endpoints.StreamEndpoint
|
||||
}
|
||||
|
||||
func (s server) Do(server interface{}, req interface{}) (err error) {
|
||||
if err := s.e(server, req); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
85
examples/go-kit/services/sprint/gen/transports/http/http.go
Normal file
85
examples/go-kit/services/sprint/gen/transports/http/http.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package sprint_httptransport
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
context "golang.org/x/net/context"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
gokit_endpoint "github.com/go-kit/kit/endpoint"
|
||||
httptransport "github.com/go-kit/kit/transport/http"
|
||||
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"
|
||||
)
|
||||
|
||||
func MakeAddSprintHandler(ctx context.Context, svc pb.SprintServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
||||
return httptransport.NewServer(
|
||||
ctx,
|
||||
endpoint,
|
||||
decodeAddSprintRequest,
|
||||
encodeResponse,
|
||||
[]httptransport.ServerOption{}...,
|
||||
)
|
||||
}
|
||||
|
||||
func decodeAddSprintRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
||||
var req pb.AddSprintRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &req, nil
|
||||
}
|
||||
|
||||
func MakeCloseSprintHandler(ctx context.Context, svc pb.SprintServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
||||
return httptransport.NewServer(
|
||||
ctx,
|
||||
endpoint,
|
||||
decodeCloseSprintRequest,
|
||||
encodeResponse,
|
||||
[]httptransport.ServerOption{}...,
|
||||
)
|
||||
}
|
||||
|
||||
func decodeCloseSprintRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
||||
var req pb.CloseSprintRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &req, nil
|
||||
}
|
||||
|
||||
func MakeGetSprintHandler(ctx context.Context, svc pb.SprintServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
||||
return httptransport.NewServer(
|
||||
ctx,
|
||||
endpoint,
|
||||
decodeGetSprintRequest,
|
||||
encodeResponse,
|
||||
[]httptransport.ServerOption{}...,
|
||||
)
|
||||
}
|
||||
|
||||
func decodeGetSprintRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
||||
var req pb.GetSprintRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &req, nil
|
||||
}
|
||||
|
||||
func encodeResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
|
||||
return json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func RegisterHandlers(ctx context.Context, svc pb.SprintServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error {
|
||||
|
||||
log.Println("new HTTP endpoint: \"/AddSprint\" (service=Sprint)")
|
||||
mux.Handle("/AddSprint", MakeAddSprintHandler(ctx, svc, endpoints.AddSprintEndpoint))
|
||||
|
||||
log.Println("new HTTP endpoint: \"/CloseSprint\" (service=Sprint)")
|
||||
mux.Handle("/CloseSprint", MakeCloseSprintHandler(ctx, svc, endpoints.CloseSprintEndpoint))
|
||||
|
||||
log.Println("new HTTP endpoint: \"/GetSprint\" (service=Sprint)")
|
||||
mux.Handle("/GetSprint", MakeGetSprintHandler(ctx, svc, endpoints.GetSprintEndpoint))
|
||||
|
||||
return nil
|
||||
}
|
27
examples/go-kit/services/sprint/service.go
Normal file
27
examples/go-kit/services/sprint/service.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package sprintsvc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/sprint/gen/pb"
|
||||
)
|
||||
|
||||
type Service struct{}
|
||||
|
||||
func New() pb.SprintServiceServer {
|
||||
return &Service{}
|
||||
}
|
||||
|
||||
func (svc *Service) AddSprint(ctx context.Context, in *pb.AddSprintRequest) (*pb.AddSprintResponse, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func (svc *Service) CloseSprint(ctx context.Context, in *pb.CloseSprintRequest) (*pb.CloseSprintResponse, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func (svc *Service) GetSprint(ctx context.Context, in *pb.GetSprintRequest) (*pb.GetSprintResponse, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
38
examples/go-kit/services/sprint/sprint.proto
Normal file
38
examples/go-kit/services/sprint/sprint.proto
Normal file
@@ -0,0 +1,38 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package sprint;
|
||||
|
||||
service SprintService {
|
||||
rpc AddSprint(AddSprintRequest) returns (AddSprintResponse) {}
|
||||
rpc CloseSprint(CloseSprintRequest) returns (CloseSprintResponse) {}
|
||||
rpc GetSprint(GetSprintRequest) returns (GetSprintResponse) {}
|
||||
}
|
||||
|
||||
message AddSprintRequest {
|
||||
string name = 1;
|
||||
}
|
||||
message AddSprintResponse {
|
||||
Sprint sprint = 1;
|
||||
string err_msg = 2;
|
||||
}
|
||||
|
||||
message CloseSprintRequest {
|
||||
string id = 1;
|
||||
}
|
||||
message CloseSprintResponse {
|
||||
string err_msg = 1;
|
||||
}
|
||||
|
||||
message GetSprintRequest {
|
||||
string id = 1;
|
||||
}
|
||||
message GetSprintResponse {
|
||||
Sprint sprint = 1;
|
||||
string err_msg = 2;
|
||||
}
|
||||
|
||||
message Sprint {
|
||||
string id = 1;
|
||||
uint32 created_at = 2;
|
||||
string name = 3;
|
||||
}
|
0
examples/go-kit/services/user/gen/README.md
Normal file
0
examples/go-kit/services/user/gen/README.md
Normal file
69
examples/go-kit/services/user/gen/client/grpc/client.go
Normal file
69
examples/go-kit/services/user/gen/client/grpc/client.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package user_clientgrpc
|
||||
|
||||
import (
|
||||
jwt "github.com/go-kit/kit/auth/jwt"
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
"github.com/go-kit/kit/log"
|
||||
grpctransport "github.com/go-kit/kit/transport/grpc"
|
||||
context "golang.org/x/net/context"
|
||||
"google.golang.org/grpc"
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
func New(conn *grpc.ClientConn, logger log.Logger) pb.UserServiceServer {
|
||||
|
||||
var createuserEndpoint endpoint.Endpoint
|
||||
{
|
||||
createuserEndpoint = grpctransport.NewClient(
|
||||
conn,
|
||||
"user.UserService",
|
||||
"CreateUser",
|
||||
EncodeCreateUserRequest,
|
||||
DecodeCreateUserResponse,
|
||||
pb.CreateUserResponse{},
|
||||
append([]grpctransport.ClientOption{}, grpctransport.ClientBefore(jwt.FromGRPCContext()))...,
|
||||
).Endpoint()
|
||||
}
|
||||
|
||||
var getuserEndpoint endpoint.Endpoint
|
||||
{
|
||||
getuserEndpoint = grpctransport.NewClient(
|
||||
conn,
|
||||
"user.UserService",
|
||||
"GetUser",
|
||||
EncodeGetUserRequest,
|
||||
DecodeGetUserResponse,
|
||||
pb.GetUserResponse{},
|
||||
append([]grpctransport.ClientOption{}, grpctransport.ClientBefore(jwt.FromGRPCContext()))...,
|
||||
).Endpoint()
|
||||
}
|
||||
|
||||
return &endpoints.Endpoints{
|
||||
|
||||
CreateUserEndpoint: createuserEndpoint,
|
||||
|
||||
GetUserEndpoint: getuserEndpoint,
|
||||
}
|
||||
}
|
||||
|
||||
func EncodeCreateUserRequest(_ context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(*pb.CreateUserRequest)
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func DecodeCreateUserResponse(_ context.Context, grpcResponse interface{}) (interface{}, error) {
|
||||
response := grpcResponse.(*pb.CreateUserResponse)
|
||||
return response, nil
|
||||
}
|
||||
|
||||
func EncodeGetUserRequest(_ context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(*pb.GetUserRequest)
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func DecodeGetUserResponse(_ context.Context, grpcResponse interface{}) (interface{}, error) {
|
||||
response := grpcResponse.(*pb.GetUserResponse)
|
||||
return response, nil
|
||||
}
|
66
examples/go-kit/services/user/gen/endpoints/endpoints.go
Normal file
66
examples/go-kit/services/user/gen/endpoints/endpoints.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package user_endpoints
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/pb"
|
||||
context "golang.org/x/net/context"
|
||||
)
|
||||
|
||||
var _ = fmt.Errorf
|
||||
|
||||
type StreamEndpoint func(server interface{}, req interface{}) (err error)
|
||||
|
||||
type Endpoints struct {
|
||||
CreateUserEndpoint endpoint.Endpoint
|
||||
|
||||
GetUserEndpoint endpoint.Endpoint
|
||||
}
|
||||
|
||||
func (e *Endpoints) CreateUser(ctx context.Context, in *pb.CreateUserRequest) (*pb.CreateUserResponse, error) {
|
||||
out, err := e.CreateUserEndpoint(ctx, in)
|
||||
if err != nil {
|
||||
return &pb.CreateUserResponse{ErrMsg: err.Error()}, err
|
||||
}
|
||||
return out.(*pb.CreateUserResponse), err
|
||||
}
|
||||
|
||||
func (e *Endpoints) GetUser(ctx context.Context, in *pb.GetUserRequest) (*pb.GetUserResponse, error) {
|
||||
out, err := e.GetUserEndpoint(ctx, in)
|
||||
if err != nil {
|
||||
return &pb.GetUserResponse{ErrMsg: err.Error()}, err
|
||||
}
|
||||
return out.(*pb.GetUserResponse), err
|
||||
}
|
||||
|
||||
func MakeCreateUserEndpoint(svc pb.UserServiceServer) endpoint.Endpoint {
|
||||
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(*pb.CreateUserRequest)
|
||||
rep, err := svc.CreateUser(ctx, req)
|
||||
if err != nil {
|
||||
return &pb.CreateUserResponse{ErrMsg: err.Error()}, err
|
||||
}
|
||||
return rep, nil
|
||||
}
|
||||
}
|
||||
|
||||
func MakeGetUserEndpoint(svc pb.UserServiceServer) endpoint.Endpoint {
|
||||
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(*pb.GetUserRequest)
|
||||
rep, err := svc.GetUser(ctx, req)
|
||||
if err != nil {
|
||||
return &pb.GetUserResponse{ErrMsg: err.Error()}, err
|
||||
}
|
||||
return rep, nil
|
||||
}
|
||||
}
|
||||
|
||||
func MakeEndpoints(svc pb.UserServiceServer) Endpoints {
|
||||
return Endpoints{
|
||||
|
||||
CreateUserEndpoint: MakeCreateUserEndpoint(svc),
|
||||
|
||||
GetUserEndpoint: MakeGetUserEndpoint(svc),
|
||||
}
|
||||
}
|
276
examples/go-kit/services/user/gen/pb/pb.go
Normal file
276
examples/go-kit/services/user/gen/pb/pb.go
Normal file
@@ -0,0 +1,276 @@
|
||||
// Code generated by protoc-gen-gogo.
|
||||
// source: services/user/user.proto
|
||||
// DO NOT EDIT!
|
||||
|
||||
/*
|
||||
Package user is a generated protocol buffer package.
|
||||
|
||||
It is generated from these files:
|
||||
services/user/user.proto
|
||||
|
||||
It has these top-level messages:
|
||||
CreateUserRequest
|
||||
CreateUserResponse
|
||||
GetUserRequest
|
||||
GetUserResponse
|
||||
User
|
||||
*/
|
||||
package user
|
||||
|
||||
import proto "github.com/gogo/protobuf/proto"
|
||||
import fmt "fmt"
|
||||
import math "math"
|
||||
|
||||
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.GoGoProtoPackageIsVersion2 // please upgrade the proto package
|
||||
|
||||
type CreateUserRequest struct {
|
||||
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
|
||||
}
|
||||
|
||||
func (m *CreateUserRequest) Reset() { *m = CreateUserRequest{} }
|
||||
func (m *CreateUserRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*CreateUserRequest) ProtoMessage() {}
|
||||
func (*CreateUserRequest) Descriptor() ([]byte, []int) { return fileDescriptorUser, []int{0} }
|
||||
|
||||
func (m *CreateUserRequest) GetName() string {
|
||||
if m != nil {
|
||||
return m.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type CreateUserResponse struct {
|
||||
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"`
|
||||
}
|
||||
|
||||
func (m *CreateUserResponse) Reset() { *m = CreateUserResponse{} }
|
||||
func (m *CreateUserResponse) String() string { return proto.CompactTextString(m) }
|
||||
func (*CreateUserResponse) ProtoMessage() {}
|
||||
func (*CreateUserResponse) Descriptor() ([]byte, []int) { return fileDescriptorUser, []int{1} }
|
||||
|
||||
func (m *CreateUserResponse) GetUser() *User {
|
||||
if m != nil {
|
||||
return m.User
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *CreateUserResponse) GetErrMsg() string {
|
||||
if m != nil {
|
||||
return m.ErrMsg
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type GetUserRequest struct {
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
}
|
||||
|
||||
func (m *GetUserRequest) Reset() { *m = GetUserRequest{} }
|
||||
func (m *GetUserRequest) String() string { return proto.CompactTextString(m) }
|
||||
func (*GetUserRequest) ProtoMessage() {}
|
||||
func (*GetUserRequest) Descriptor() ([]byte, []int) { return fileDescriptorUser, []int{2} }
|
||||
|
||||
func (m *GetUserRequest) GetId() string {
|
||||
if m != nil {
|
||||
return m.Id
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type GetUserResponse struct {
|
||||
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"`
|
||||
}
|
||||
|
||||
func (m *GetUserResponse) Reset() { *m = GetUserResponse{} }
|
||||
func (m *GetUserResponse) String() string { return proto.CompactTextString(m) }
|
||||
func (*GetUserResponse) ProtoMessage() {}
|
||||
func (*GetUserResponse) Descriptor() ([]byte, []int) { return fileDescriptorUser, []int{3} }
|
||||
|
||||
func (m *GetUserResponse) GetUser() *User {
|
||||
if m != nil {
|
||||
return m.User
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *GetUserResponse) GetErrMsg() string {
|
||||
if m != nil {
|
||||
return m.ErrMsg
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type User struct {
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
|
||||
}
|
||||
|
||||
func (m *User) Reset() { *m = User{} }
|
||||
func (m *User) String() string { return proto.CompactTextString(m) }
|
||||
func (*User) ProtoMessage() {}
|
||||
func (*User) Descriptor() ([]byte, []int) { return fileDescriptorUser, []int{4} }
|
||||
|
||||
func (m *User) GetId() string {
|
||||
if m != nil {
|
||||
return m.Id
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (m *User) GetName() string {
|
||||
if m != nil {
|
||||
return m.Name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*CreateUserRequest)(nil), "user.CreateUserRequest")
|
||||
proto.RegisterType((*CreateUserResponse)(nil), "user.CreateUserResponse")
|
||||
proto.RegisterType((*GetUserRequest)(nil), "user.GetUserRequest")
|
||||
proto.RegisterType((*GetUserResponse)(nil), "user.GetUserResponse")
|
||||
proto.RegisterType((*User)(nil), "user.User")
|
||||
}
|
||||
|
||||
// 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 UserService service
|
||||
|
||||
type UserServiceClient interface {
|
||||
CreateUser(ctx context.Context, in *CreateUserRequest, opts ...grpc.CallOption) (*CreateUserResponse, error)
|
||||
GetUser(ctx context.Context, in *GetUserRequest, opts ...grpc.CallOption) (*GetUserResponse, error)
|
||||
}
|
||||
|
||||
type userServiceClient struct {
|
||||
cc *grpc.ClientConn
|
||||
}
|
||||
|
||||
func NewUserServiceClient(cc *grpc.ClientConn) UserServiceClient {
|
||||
return &userServiceClient{cc}
|
||||
}
|
||||
|
||||
func (c *userServiceClient) CreateUser(ctx context.Context, in *CreateUserRequest, opts ...grpc.CallOption) (*CreateUserResponse, error) {
|
||||
out := new(CreateUserResponse)
|
||||
err := grpc.Invoke(ctx, "/user.UserService/CreateUser", in, out, c.cc, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *userServiceClient) GetUser(ctx context.Context, in *GetUserRequest, opts ...grpc.CallOption) (*GetUserResponse, error) {
|
||||
out := new(GetUserResponse)
|
||||
err := grpc.Invoke(ctx, "/user.UserService/GetUser", in, out, c.cc, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Server API for UserService service
|
||||
|
||||
type UserServiceServer interface {
|
||||
CreateUser(context.Context, *CreateUserRequest) (*CreateUserResponse, error)
|
||||
GetUser(context.Context, *GetUserRequest) (*GetUserResponse, error)
|
||||
}
|
||||
|
||||
func RegisterUserServiceServer(s *grpc.Server, srv UserServiceServer) {
|
||||
s.RegisterService(&_UserService_serviceDesc, srv)
|
||||
}
|
||||
|
||||
func _UserService_CreateUser_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(CreateUserRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(UserServiceServer).CreateUser(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/user.UserService/CreateUser",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(UserServiceServer).CreateUser(ctx, req.(*CreateUserRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _UserService_GetUser_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(GetUserRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(UserServiceServer).GetUser(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: "/user.UserService/GetUser",
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(UserServiceServer).GetUser(ctx, req.(*GetUserRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
var _UserService_serviceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "user.UserService",
|
||||
HandlerType: (*UserServiceServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "CreateUser",
|
||||
Handler: _UserService_CreateUser_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "GetUser",
|
||||
Handler: _UserService_GetUser_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "services/user/user.proto",
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("services/user/user.proto", fileDescriptorUser) }
|
||||
|
||||
var fileDescriptorUser = []byte{
|
||||
// 236 bytes of a gzipped FileDescriptorProto
|
||||
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,
|
||||
0xf9, 0x42, 0x2c, 0x20, 0xb6, 0x92, 0x3a, 0x97, 0xa0, 0x73, 0x51, 0x6a, 0x62, 0x49, 0x6a, 0x68,
|
||||
0x71, 0x6a, 0x51, 0x50, 0x6a, 0x61, 0x69, 0x6a, 0x71, 0x89, 0x90, 0x10, 0x17, 0x4b, 0x5e, 0x62,
|
||||
0x6e, 0xaa, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0x67, 0x10, 0x98, 0xad, 0xe4, 0xcb, 0x25, 0x84, 0xac,
|
||||
0xb0, 0xb8, 0x20, 0x3f, 0xaf, 0x38, 0x55, 0x48, 0x8e, 0x0b, 0x6c, 0x0c, 0x58, 0x25, 0xb7, 0x11,
|
||||
0x97, 0x1e, 0xd8, 0x7c, 0xb0, 0x0a, 0xb0, 0xb8, 0x90, 0x38, 0x17, 0x7b, 0x6a, 0x51, 0x51, 0x7c,
|
||||
0x6e, 0x71, 0xba, 0x04, 0x13, 0xd8, 0x30, 0xb6, 0xd4, 0xa2, 0x22, 0xdf, 0xe2, 0x74, 0x25, 0x05,
|
||||
0x2e, 0x3e, 0xf7, 0xd4, 0x12, 0x64, 0x4b, 0xf9, 0xb8, 0x98, 0x32, 0x53, 0xa0, 0x56, 0x32, 0x65,
|
||||
0xa6, 0x28, 0x79, 0x71, 0xf1, 0xc3, 0x55, 0x50, 0x6a, 0x9b, 0x16, 0x17, 0x0b, 0x48, 0x19, 0xba,
|
||||
0x1d, 0x70, 0x8f, 0x32, 0x21, 0x3c, 0x6a, 0xd4, 0xc5, 0xc8, 0xc5, 0x0d, 0x52, 0x1c, 0x0c, 0x09,
|
||||
0x38, 0x21, 0x47, 0x2e, 0x2e, 0x84, 0xc7, 0x85, 0xc4, 0x21, 0x96, 0x62, 0x84, 0x99, 0x94, 0x04,
|
||||
0xa6, 0x04, 0xc4, 0xd5, 0x4a, 0x0c, 0x42, 0x16, 0x5c, 0xec, 0x50, 0xaf, 0x08, 0x89, 0x40, 0x94,
|
||||
0xa1, 0xfa, 0x5d, 0x4a, 0x14, 0x4d, 0x14, 0xa6, 0x33, 0x89, 0x0d, 0x1c, 0x57, 0xc6, 0x80, 0x00,
|
||||
0x00, 0x00, 0xff, 0xff, 0xce, 0xde, 0xa3, 0x2e, 0xc7, 0x01, 0x00, 0x00,
|
||||
}
|
87
examples/go-kit/services/user/gen/transports/grpc/grpc.go
Normal file
87
examples/go-kit/services/user/gen/transports/grpc/grpc.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package user_grpctransport
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
grpctransport "github.com/go-kit/kit/transport/grpc"
|
||||
context "golang.org/x/net/context"
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
// avoid import errors
|
||||
var _ = fmt.Errorf
|
||||
|
||||
func MakeGRPCServer(ctx context.Context, endpoints endpoints.Endpoints) pb.UserServiceServer {
|
||||
options := []grpctransport.ServerOption{}
|
||||
return &grpcServer{
|
||||
|
||||
createuser: grpctransport.NewServer(
|
||||
ctx,
|
||||
endpoints.CreateUserEndpoint,
|
||||
decodeRequest,
|
||||
encodeCreateUserResponse,
|
||||
options...,
|
||||
),
|
||||
|
||||
getuser: grpctransport.NewServer(
|
||||
ctx,
|
||||
endpoints.GetUserEndpoint,
|
||||
decodeRequest,
|
||||
encodeGetUserResponse,
|
||||
options...,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
type grpcServer struct {
|
||||
createuser grpctransport.Handler
|
||||
|
||||
getuser grpctransport.Handler
|
||||
}
|
||||
|
||||
func (s *grpcServer) CreateUser(ctx context.Context, req *pb.CreateUserRequest) (*pb.CreateUserResponse, error) {
|
||||
_, rep, err := s.createuser.ServeGRPC(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rep.(*pb.CreateUserResponse), nil
|
||||
}
|
||||
|
||||
func encodeCreateUserResponse(ctx context.Context, response interface{}) (interface{}, error) {
|
||||
resp := response.(*pb.CreateUserResponse)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *grpcServer) GetUser(ctx context.Context, req *pb.GetUserRequest) (*pb.GetUserResponse, error) {
|
||||
_, rep, err := s.getuser.ServeGRPC(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rep.(*pb.GetUserResponse), nil
|
||||
}
|
||||
|
||||
func encodeGetUserResponse(ctx context.Context, response interface{}) (interface{}, error) {
|
||||
resp := response.(*pb.GetUserResponse)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func decodeRequest(ctx context.Context, grpcReq interface{}) (interface{}, error) {
|
||||
return grpcReq, nil
|
||||
}
|
||||
|
||||
type streamHandler interface {
|
||||
Do(server interface{}, req interface{}) (err error)
|
||||
}
|
||||
|
||||
type server struct {
|
||||
e endpoints.StreamEndpoint
|
||||
}
|
||||
|
||||
func (s server) Do(server interface{}, req interface{}) (err error) {
|
||||
if err := s.e(server, req); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
64
examples/go-kit/services/user/gen/transports/http/http.go
Normal file
64
examples/go-kit/services/user/gen/transports/http/http.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package user_httptransport
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
context "golang.org/x/net/context"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
gokit_endpoint "github.com/go-kit/kit/endpoint"
|
||||
httptransport "github.com/go-kit/kit/transport/http"
|
||||
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"
|
||||
)
|
||||
|
||||
func MakeCreateUserHandler(ctx context.Context, svc pb.UserServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
||||
return httptransport.NewServer(
|
||||
ctx,
|
||||
endpoint,
|
||||
decodeCreateUserRequest,
|
||||
encodeResponse,
|
||||
[]httptransport.ServerOption{}...,
|
||||
)
|
||||
}
|
||||
|
||||
func decodeCreateUserRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
||||
var req pb.CreateUserRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &req, nil
|
||||
}
|
||||
|
||||
func MakeGetUserHandler(ctx context.Context, svc pb.UserServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
||||
return httptransport.NewServer(
|
||||
ctx,
|
||||
endpoint,
|
||||
decodeGetUserRequest,
|
||||
encodeResponse,
|
||||
[]httptransport.ServerOption{}...,
|
||||
)
|
||||
}
|
||||
|
||||
func decodeGetUserRequest(ctx context.Context, r *http.Request) (interface{}, error) {
|
||||
var req pb.GetUserRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &req, nil
|
||||
}
|
||||
|
||||
func encodeResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
|
||||
return json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func RegisterHandlers(ctx context.Context, svc pb.UserServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error {
|
||||
|
||||
log.Println("new HTTP endpoint: \"/CreateUser\" (service=User)")
|
||||
mux.Handle("/CreateUser", MakeCreateUserHandler(ctx, svc, endpoints.CreateUserEndpoint))
|
||||
|
||||
log.Println("new HTTP endpoint: \"/GetUser\" (service=User)")
|
||||
mux.Handle("/GetUser", MakeGetUserHandler(ctx, svc, endpoints.GetUserEndpoint))
|
||||
|
||||
return nil
|
||||
}
|
21
examples/go-kit/services/user/service.go
Normal file
21
examples/go-kit/services/user/service.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package usersvc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/user/gen/pb"
|
||||
)
|
||||
|
||||
type Service struct{}
|
||||
|
||||
func New() pb.UserServiceServer { return &Service{} }
|
||||
|
||||
func (svc *Service) CreateUser(ctx context.Context, in *pb.CreateUserRequest) (*pb.CreateUserResponse, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
|
||||
func (svc *Service) GetUser(ctx context.Context, in *pb.GetUserRequest) (*pb.GetUserResponse, error) {
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
29
examples/go-kit/services/user/user.proto
Normal file
29
examples/go-kit/services/user/user.proto
Normal file
@@ -0,0 +1,29 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package user;
|
||||
|
||||
service UserService {
|
||||
rpc CreateUser(CreateUserRequest) returns (CreateUserResponse) {}
|
||||
rpc GetUser(GetUserRequest) returns (GetUserResponse) {}
|
||||
}
|
||||
|
||||
message CreateUserRequest {
|
||||
string name = 1;
|
||||
}
|
||||
message CreateUserResponse {
|
||||
User user = 1;
|
||||
string err_msg = 2;
|
||||
}
|
||||
|
||||
message GetUserRequest {
|
||||
string id = 1;
|
||||
}
|
||||
message GetUserResponse {
|
||||
User user = 1;
|
||||
string err_msg = 2;
|
||||
}
|
||||
|
||||
message User {
|
||||
string id = 1;
|
||||
string name = 2;
|
||||
}
|
@@ -0,0 +1,56 @@
|
||||
package {{.File.Package}}_clientgrpc
|
||||
|
||||
import (
|
||||
"github.com/go-kit/kit/log"
|
||||
context "golang.org/x/net/context"
|
||||
"google.golang.org/grpc"
|
||||
grpctransport "github.com/go-kit/kit/transport/grpc"
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
jwt "github.com/go-kit/kit/auth/jwt"
|
||||
|
||||
pb "{{cat .GoPWD "/" .DestinationDir | nospace | clean}}/pb"
|
||||
endpoints "{{cat .GoPWD "/" .DestinationDir | nospace | clean}}/endpoints"
|
||||
)
|
||||
|
||||
{{$file:=.File}}
|
||||
|
||||
func New(conn *grpc.ClientConn, logger log.Logger) pb.{{.File.Package | title}}ServiceServer {
|
||||
{{range .Service.Method}}
|
||||
{{if and (not .ServerStreaming) (not .ClientStreaming)}}
|
||||
var {{.Name | lower}}Endpoint endpoint.Endpoint
|
||||
{
|
||||
{{.Name | lower}}Endpoint = grpctransport.NewClient(
|
||||
conn,
|
||||
"{{$file.Package}}.{{$file.Package | title}}Service",
|
||||
"{{.Name}}",
|
||||
Encode{{.Name}}Request,
|
||||
Decode{{.Name}}Response,
|
||||
pb.{{.Name}}Response{},
|
||||
append([]grpctransport.ClientOption{}, grpctransport.ClientBefore(jwt.FromGRPCContext()))...,
|
||||
).Endpoint()
|
||||
}
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
return &endpoints.Endpoints {
|
||||
{{range .Service.Method}}
|
||||
{{if and (not .ServerStreaming) (not .ClientStreaming)}}
|
||||
{{.Name | title}}Endpoint: {{.Name | lower}}Endpoint,
|
||||
{{end}}
|
||||
{{end}}
|
||||
}
|
||||
}
|
||||
|
||||
{{range .Service.Method}}
|
||||
{{if and (not .ServerStreaming) (not .ClientStreaming)}}
|
||||
func Encode{{.Name}}Request(_ context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(*pb.{{.Name}}Request)
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func Decode{{.Name}}Response(_ context.Context, grpcResponse interface{}) (interface{}, error) {
|
||||
response := grpcResponse.(*pb.{{.Name}}Response)
|
||||
return response, nil
|
||||
}
|
||||
{{end}}
|
||||
{{end}}
|
@@ -0,0 +1,88 @@
|
||||
package {{.File.Package}}_endpoints
|
||||
|
||||
{{$file := .File}}
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
context "golang.org/x/net/context"
|
||||
pb "{{cat .GoPWD "/" .DestinationDir | nospace | clean}}/pb"
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
)
|
||||
|
||||
var _ = endpoint.Chain
|
||||
var _ = fmt.Errorf
|
||||
var _ = context.Background
|
||||
|
||||
type StreamEndpoint func(server interface{}, req interface{}) (err error)
|
||||
|
||||
type Endpoints struct {
|
||||
{{range .Service.Method}}
|
||||
{{if or (.ClientStreaming) (.ServerStreaming)}}
|
||||
{{.Name}}Endpoint StreamEndpoint
|
||||
{{else}}
|
||||
{{.Name}}Endpoint endpoint.Endpoint
|
||||
{{end}}
|
||||
{{end}}
|
||||
}
|
||||
|
||||
{{range .Service.Method}}
|
||||
{{if .ServerStreaming}}
|
||||
{{if .ClientStreaming}}
|
||||
func (e *Endpoints){{.Name}}(server pb.{{$file.Package | title}}Service_{{.Name}}Server) error {
|
||||
return fmt.Errorf("not implemented")
|
||||
}
|
||||
{{else}}
|
||||
func (e *Endpoints){{.Name}}(in *pb.{{.Name}}Request, server pb.{{$file.Package | title}}Service_{{.Name}}Server) error {
|
||||
return fmt.Errorf("not implemented")
|
||||
}
|
||||
{{end}}
|
||||
{{else}}
|
||||
{{if .ClientStreaming}}
|
||||
func (e *Endpoints){{.Name}}(server pb.{{$file.Package | title}}Service_{{.Name}}Server) error {
|
||||
return fmt.Errorf("not implemented")
|
||||
}
|
||||
{{else}}
|
||||
func (e *Endpoints){{.Name}}(ctx context.Context, in *pb.{{.InputType | splitArray "." | last}}) (*pb.{{.OutputType | splitArray "." | last}}, error) {
|
||||
out, err := e.{{.Name}}Endpoint(ctx, in)
|
||||
if err != nil {
|
||||
return &pb.{{.OutputType | splitArray "." | last}}{ErrMsg: err.Error()}, err
|
||||
}
|
||||
return out.(*pb.{{.OutputType | splitArray "." | last}}), err
|
||||
}
|
||||
{{end}}
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
{{range .Service.Method}}
|
||||
{{if or (.ServerStreaming) (.ClientStreaming)}}
|
||||
func Make{{.Name}}Endpoint(svc pb.{{$file.Package | title}}ServiceServer) StreamEndpoint {
|
||||
return func(server interface{}, request interface{}) error {
|
||||
{{if .ClientStreaming}}
|
||||
return svc.{{.Name}}(server.(pb.{{$file.Package | title}}Service_{{.Name}}Server))
|
||||
{{else}}
|
||||
return svc.{{.Name}}(request.(*pb.{{.Name}}Request), server.(pb.{{$file.Package | title}}Service_{{.Name}}Server))
|
||||
{{end}}
|
||||
}
|
||||
}
|
||||
{{else}}
|
||||
func Make{{.Name}}Endpoint(svc pb.{{$file.Package | title}}ServiceServer) endpoint.Endpoint {
|
||||
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
||||
req := request.(*pb.{{.InputType | splitArray "." | last}})
|
||||
rep, err := svc.{{.Name}}(ctx, req)
|
||||
if err != nil {
|
||||
return &pb.{{.OutputType | splitArray "." | last}}{ErrMsg: err.Error()}, err
|
||||
}
|
||||
return rep, nil
|
||||
}
|
||||
}
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
func MakeEndpoints(svc pb.{{.File.Package | title}}ServiceServer) Endpoints {
|
||||
return Endpoints{
|
||||
{{range .Service.Method}}
|
||||
{{.Name}}Endpoint: Make{{.Name}}Endpoint(svc),
|
||||
{{end}}
|
||||
}
|
||||
}
|
@@ -0,0 +1,92 @@
|
||||
package {{.File.Package}}_grpctransport
|
||||
|
||||
{{$file := .File}}
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
context "golang.org/x/net/context"
|
||||
grpctransport "github.com/go-kit/kit/transport/grpc"
|
||||
|
||||
pb "{{cat .GoPWD "/" .DestinationDir | nospace | clean}}/pb"
|
||||
endpoints "{{cat .GoPWD "/" .DestinationDir | nospace | clean}}/endpoints"
|
||||
)
|
||||
|
||||
// avoid import errors
|
||||
var _ = fmt.Errorf
|
||||
|
||||
func MakeGRPCServer(ctx context.Context, endpoints endpoints.Endpoints) pb.{{.File.Package | title}}ServiceServer {
|
||||
var options []grpctransport.ServerOption
|
||||
_ = options
|
||||
return &grpcServer{
|
||||
{{range .Service.Method}}
|
||||
{{if or (.ClientStreaming) (.ServerStreaming)}}
|
||||
{{.Name | lower}}: &server{
|
||||
e: endpoints.{{.Name}}Endpoint,
|
||||
},
|
||||
{{else}}
|
||||
{{.Name | lower}}: grpctransport.NewServer(
|
||||
ctx,
|
||||
endpoints.{{.Name}}Endpoint,
|
||||
decodeRequest,
|
||||
encode{{.Name}}Response,
|
||||
options...,
|
||||
),
|
||||
{{end}}
|
||||
{{end}}
|
||||
}
|
||||
}
|
||||
|
||||
type grpcServer struct {
|
||||
{{range .Service.Method}}
|
||||
{{if or (.ClientStreaming) (.ServerStreaming)}}
|
||||
{{.Name | lower}} streamHandler
|
||||
{{else}}
|
||||
{{.Name | lower}} grpctransport.Handler
|
||||
{{end}}
|
||||
{{end}}
|
||||
}
|
||||
|
||||
{{range .Service.Method}}
|
||||
{{if .ClientStreaming}}
|
||||
func (s *grpcServer) {{.Name}}(server pb.{{$file.Package | title}}Service_{{.Name}}Server) error {
|
||||
return s.{{.Name | lower}}.Do(server, nil)
|
||||
}
|
||||
{{else if .ServerStreaming}}
|
||||
func (s *grpcServer) {{.Name}}(req *pb.{{.Name}}Request, server pb.{{$file.Package | title}}Service_{{.Name}}Server) error {
|
||||
return s.{{.Name | lower}}.Do(server, req)
|
||||
}
|
||||
{{else}}
|
||||
func (s *grpcServer) {{.Name}}(ctx context.Context, req *pb.{{.InputType | splitArray "." | last}}) (*pb.{{.OutputType | splitArray "." | last}}, error) {
|
||||
_, rep, err := s.{{.Name | lower}}.ServeGRPC(ctx, req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return rep.(*pb.{{.OutputType | splitArray "." | last}}), nil
|
||||
}
|
||||
|
||||
func encode{{.Name}}Response(ctx context.Context, response interface{}) (interface{}, error) {
|
||||
resp := response.(*pb.{{.OutputType | splitArray "." | last}})
|
||||
return resp, nil
|
||||
}
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
func decodeRequest(ctx context.Context, grpcReq interface{}) (interface{}, error) {
|
||||
return grpcReq, nil
|
||||
}
|
||||
|
||||
type streamHandler interface{
|
||||
Do(server interface{}, req interface{}) (err error)
|
||||
}
|
||||
|
||||
type server struct {
|
||||
e endpoints.StreamEndpoint
|
||||
}
|
||||
|
||||
func (s server) Do(server interface{}, req interface{}) (err error) {
|
||||
if err := s.e(server, req); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
@@ -0,0 +1,56 @@
|
||||
package {{.File.Package}}_httptransport
|
||||
|
||||
{{$file := .File}}
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"encoding/json"
|
||||
context "golang.org/x/net/context"
|
||||
|
||||
pb "{{cat .GoPWD "/" .DestinationDir | nospace | clean}}/pb"
|
||||
gokit_endpoint "github.com/go-kit/kit/endpoint"
|
||||
httptransport "github.com/go-kit/kit/transport/http"
|
||||
endpoints "{{cat .GoPWD "/" .DestinationDir | nospace | clean}}/endpoints"
|
||||
)
|
||||
|
||||
var _ = log.Printf
|
||||
var _ = gokit_endpoint.Chain
|
||||
var _ = httptransport.NewClient
|
||||
|
||||
|
||||
{{range .Service.Method}}
|
||||
{{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 {
|
||||
return httptransport.NewServer(
|
||||
ctx,
|
||||
endpoint,
|
||||
decode{{.Name}}Request,
|
||||
encodeResponse,
|
||||
[]httptransport.ServerOption{}...,
|
||||
)
|
||||
}
|
||||
|
||||
func decode{{.Name}}Request(ctx context.Context, r *http.Request) (interface{}, error) {
|
||||
var req pb.{{.InputType | splitArray "." | last}}
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &req, nil
|
||||
}
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
func encodeResponse(ctx context.Context, w http.ResponseWriter, response interface{}) error {
|
||||
return json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func RegisterHandlers(ctx context.Context, svc pb.{{$file.Package | title}}ServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error {
|
||||
{{range .Service.Method}}
|
||||
{{if and (not .ServerStreaming) (not .ClientStreaming)}}
|
||||
log.Println("new HTTP endpoint: \"/{{.Name}}\" (service={{$file.Package | title}})")
|
||||
mux.Handle("/{{.Name}}", Make{{.Name}}Handler(ctx, svc, endpoints.{{.Name}}Endpoint))
|
||||
{{end}}
|
||||
{{end}}
|
||||
return nil
|
||||
}
|
10
examples/k8s/Makefile
Normal file
10
examples/k8s/Makefile
Normal file
@@ -0,0 +1,10 @@
|
||||
SOURCES := $(shell find . -name "*.proto" -not -path ./vendor/\*)
|
||||
|
||||
TARGETS_K8S := $(foreach source, $(SOURCES), $(source)_k8s)
|
||||
|
||||
.PHONY: build
|
||||
build: $(TARGETS_K8S)
|
||||
|
||||
$(TARGETS_K8S): %_k8s:
|
||||
@mkdir -p $(dir $*)gen
|
||||
protoc $(PROTOC_OPTS) --gotemplate_out=debug=true,template_dir=./templates:$(dir $*)gen "$*"
|
18
examples/k8s/gen/nginx/dpl.yaml
Normal file
18
examples/k8s/gen/nginx/dpl.yaml
Normal file
@@ -0,0 +1,18 @@
|
||||
apiVersion: extensions/v1beta1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: nginx
|
||||
labels:
|
||||
method: Hello-World-nginx
|
||||
spec:
|
||||
replicas: 3
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: nginx
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx
|
||||
ports:
|
||||
- containerPort: 80
|
10
examples/k8s/gen/nginx/svc.yaml
Normal file
10
examples/k8s/gen/nginx/svc.yaml
Normal file
@@ -0,0 +1,10 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: nginx
|
||||
spec:
|
||||
selector:
|
||||
app: nginx
|
||||
ports:
|
||||
- name: http
|
||||
port: 80
|
11
examples/k8s/nginx.proto
Normal file
11
examples/k8s/nginx.proto
Normal file
@@ -0,0 +1,11 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package nginx;
|
||||
|
||||
message Empty {
|
||||
}
|
||||
|
||||
service Nginx {
|
||||
rpc Hello(Empty) returns (Empty) {}
|
||||
rpc World(Empty) returns (Empty) {}
|
||||
}
|
18
examples/k8s/templates/{{.File.Package}}/dpl.yaml.tmpl
Normal file
18
examples/k8s/templates/{{.File.Package}}/dpl.yaml.tmpl
Normal file
@@ -0,0 +1,18 @@
|
||||
apiVersion: extensions/v1beta1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: {{.File.Package}}
|
||||
labels:
|
||||
method: {{range .Service.Method}}{{.Name}}-{{end}}{{.File.Package}}
|
||||
spec:
|
||||
replicas: 3
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: {{.File.Package}}
|
||||
spec:
|
||||
containers:
|
||||
- name: nginx
|
||||
image: nginx
|
||||
ports:
|
||||
- containerPort: 80
|
10
examples/k8s/templates/{{.File.Package}}/svc.yaml.tmpl
Normal file
10
examples/k8s/templates/{{.File.Package}}/svc.yaml.tmpl
Normal file
@@ -0,0 +1,10 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: {{.File.Package}}
|
||||
spec:
|
||||
selector:
|
||||
app: {{.File.Package}}
|
||||
ports:
|
||||
- name: http
|
||||
port: 80
|
21
glide.lock
generated
Normal file
21
glide.lock
generated
Normal file
@@ -0,0 +1,21 @@
|
||||
hash: 488701437e53b39667ed5a84ed1500e727ea8d4902d804c3ea7eb50b1fc022a1
|
||||
updated: 2016-12-21T10:24:42.512734471+01:00
|
||||
imports:
|
||||
- name: github.com/aokoli/goutils
|
||||
version: 9c37978a95bd5c709a15883b6242714ea6709e64
|
||||
- name: github.com/golang/protobuf
|
||||
version: 8ee79997227bf9b34611aee7946ae64735e6fd93
|
||||
subpackages:
|
||||
- proto
|
||||
- protoc-gen-go/descriptor
|
||||
- protoc-gen-go/generator
|
||||
- protoc-gen-go/plugin
|
||||
- name: github.com/huandu/xstrings
|
||||
version: 3959339b333561bf62a38b424fd41517c2c90f40
|
||||
- name: github.com/kr/fs
|
||||
version: 2788f0dbd16903de03cb8186e5c7d97b69ad387b
|
||||
- name: github.com/Masterminds/sprig
|
||||
version: 69011c0cd9b4d2e0733c4d9e2c8e2a5a0d0a2f2f
|
||||
- name: github.com/satori/go.uuid
|
||||
version: 879c5887cd475cd7864858769793b2ceb0d44feb
|
||||
testImports: []
|
11
glide.yaml
Normal file
11
glide.yaml
Normal file
@@ -0,0 +1,11 @@
|
||||
package: github.com/moul/protoc-gen-gotemplate
|
||||
import:
|
||||
- package: github.com/golang/protobuf
|
||||
subpackages:
|
||||
- proto
|
||||
- protoc-gen-go/descriptor
|
||||
- protoc-gen-go/generator
|
||||
- protoc-gen-go/plugin
|
||||
- package: github.com/kr/fs
|
||||
- package: github.com/Masterminds/sprig
|
||||
- package: github.com/huandu/xstrings
|
240
helpers.go
Normal file
240
helpers.go
Normal file
@@ -0,0 +1,240 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/Masterminds/sprig"
|
||||
"github.com/huandu/xstrings"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/golang/protobuf/protoc-gen-go/descriptor"
|
||||
options "github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api"
|
||||
)
|
||||
|
||||
var ProtoHelpersFuncMap = template.FuncMap{
|
||||
"string": func(i interface {
|
||||
String() string
|
||||
}) string {
|
||||
return i.String()
|
||||
},
|
||||
"json": func(v interface{}) string {
|
||||
a, _ := json.Marshal(v)
|
||||
return string(a)
|
||||
},
|
||||
"prettyjson": func(v interface{}) string {
|
||||
a, _ := json.MarshalIndent(v, "", " ")
|
||||
return string(a)
|
||||
},
|
||||
"splitArray": func(sep string, s string) []string {
|
||||
return strings.Split(s, sep)
|
||||
},
|
||||
"first": func(a []string) string {
|
||||
return a[0]
|
||||
},
|
||||
"last": func(a []string) string {
|
||||
return a[len(a)-1]
|
||||
},
|
||||
"upperFirst": func(s string) string {
|
||||
return strings.ToUpper(s[:1]) + s[1:]
|
||||
},
|
||||
"lowerFirst": func(s string) string {
|
||||
return strings.ToLower(s[:1]) + s[1:]
|
||||
},
|
||||
"camelCase": func(s string) string {
|
||||
if len(s) > 1 {
|
||||
return xstrings.ToCamelCase(s)
|
||||
}
|
||||
|
||||
return strings.ToUpper(s[:1])
|
||||
},
|
||||
"lowerCamelCase": func(s string) string {
|
||||
if len(s) > 1 {
|
||||
s = xstrings.ToCamelCase(s)
|
||||
}
|
||||
|
||||
return strings.ToLower(s[:1]) + s[1:]
|
||||
},
|
||||
"snakeCase": func(s string) string {
|
||||
return xstrings.ToSnakeCase(s)
|
||||
},
|
||||
"kebabCase": func(s string) string {
|
||||
return strings.Replace(xstrings.ToSnakeCase(s), "_", "-", -1)
|
||||
},
|
||||
"getMessageType": getMessageType,
|
||||
"isFieldMessage": isFieldMessage,
|
||||
"isFieldRepeated": isFieldRepeated,
|
||||
"goType": goType,
|
||||
"jsType": jsType,
|
||||
"httpVerb": httpVerb,
|
||||
"httpPath": httpPath,
|
||||
"shortType": shortType,
|
||||
}
|
||||
|
||||
func init() {
|
||||
for k, v := range sprig.TxtFuncMap() {
|
||||
ProtoHelpersFuncMap[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
func getMessageType(f *descriptor.FileDescriptorProto, name string) *descriptor.DescriptorProto {
|
||||
// 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 {
|
||||
if target == *m.Name {
|
||||
return m
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func isFieldMessage(f *descriptor.FieldDescriptorProto) bool {
|
||||
if f.Type != nil && *f.Type == descriptor.FieldDescriptorProto_TYPE_MESSAGE {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func isFieldRepeated(f *descriptor.FieldDescriptorProto) bool {
|
||||
if f.Type != nil && f.Label != nil && *f.Label == descriptor.FieldDescriptorProto_LABEL_REPEATED {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func goType(pkg string, f *descriptor.FieldDescriptorProto) string {
|
||||
switch *f.Type {
|
||||
case descriptor.FieldDescriptorProto_TYPE_DOUBLE:
|
||||
return "float64"
|
||||
case descriptor.FieldDescriptorProto_TYPE_FLOAT:
|
||||
return "float32"
|
||||
case descriptor.FieldDescriptorProto_TYPE_INT64:
|
||||
return "int64"
|
||||
case descriptor.FieldDescriptorProto_TYPE_UINT64:
|
||||
return "uint64"
|
||||
case descriptor.FieldDescriptorProto_TYPE_INT32:
|
||||
return "uint32"
|
||||
case descriptor.FieldDescriptorProto_TYPE_BOOL:
|
||||
return "bool"
|
||||
case descriptor.FieldDescriptorProto_TYPE_STRING:
|
||||
return "string"
|
||||
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
|
||||
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:
|
||||
return "byte"
|
||||
case descriptor.FieldDescriptorProto_TYPE_UINT32:
|
||||
return "uint32"
|
||||
case descriptor.FieldDescriptorProto_TYPE_ENUM:
|
||||
return fmt.Sprintf("*%s.%s", pkg, shortType(*f.TypeName))
|
||||
default:
|
||||
return "interface{}"
|
||||
}
|
||||
}
|
||||
|
||||
func jsType(f *descriptor.FieldDescriptorProto) string {
|
||||
template := "%s"
|
||||
if isFieldRepeated(f) == true {
|
||||
template = "Array<%s>"
|
||||
}
|
||||
|
||||
switch *f.Type {
|
||||
case descriptor.FieldDescriptorProto_TYPE_MESSAGE:
|
||||
return fmt.Sprintf(template, shortType(*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, "Array<number>")
|
||||
case descriptor.FieldDescriptorProto_TYPE_STRING:
|
||||
return fmt.Sprintf(template, "string")
|
||||
case descriptor.FieldDescriptorProto_TYPE_ENUM:
|
||||
return fmt.Sprintf(template, "Object")
|
||||
default:
|
||||
return fmt.Sprintf(template, "any")
|
||||
}
|
||||
}
|
||||
|
||||
func shortType(s string) string {
|
||||
t := strings.Split(s, ".")
|
||||
return t[len(t)-1]
|
||||
}
|
||||
|
||||
func httpPath(m *descriptor.MethodDescriptorProto) string {
|
||||
|
||||
ext, err := proto.GetExtension(m.Options, options.E_Http)
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
opts, ok := ext.(*options.HttpRule)
|
||||
if !ok {
|
||||
return fmt.Sprintf("extension is %T; want an HttpRule", ext)
|
||||
}
|
||||
|
||||
switch t := opts.Pattern.(type) {
|
||||
default:
|
||||
return ""
|
||||
case *options.HttpRule_Get:
|
||||
return t.Get
|
||||
case *options.HttpRule_Post:
|
||||
return t.Post
|
||||
case *options.HttpRule_Put:
|
||||
return t.Put
|
||||
case *options.HttpRule_Delete:
|
||||
return t.Delete
|
||||
case *options.HttpRule_Patch:
|
||||
return t.Patch
|
||||
case *options.HttpRule_Custom:
|
||||
return t.Custom.Path
|
||||
}
|
||||
}
|
||||
|
||||
func httpVerb(m *descriptor.MethodDescriptorProto) string {
|
||||
|
||||
ext, err := proto.GetExtension(m.Options, options.E_Http)
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
opts, ok := ext.(*options.HttpRule)
|
||||
if !ok {
|
||||
return fmt.Sprintf("extension is %T; want an HttpRule", ext)
|
||||
}
|
||||
|
||||
switch t := opts.Pattern.(type) {
|
||||
default:
|
||||
return ""
|
||||
case *options.HttpRule_Get:
|
||||
return "GET"
|
||||
case *options.HttpRule_Post:
|
||||
return "POST"
|
||||
case *options.HttpRule_Put:
|
||||
return "PUT"
|
||||
case *options.HttpRule_Delete:
|
||||
return "DELETE"
|
||||
case *options.HttpRule_Patch:
|
||||
return "PATCH"
|
||||
case *options.HttpRule_Custom:
|
||||
return t.Custom.Kind
|
||||
}
|
||||
}
|
39
main.go
39
main.go
@@ -2,7 +2,9 @@ package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/golang/protobuf/protoc-gen-go/generator"
|
||||
@@ -26,10 +28,43 @@ func main() {
|
||||
|
||||
g.CommandLineParameters(g.Request.GetParameter())
|
||||
|
||||
// Generate the clients
|
||||
// Parse parameters
|
||||
templateDir := "./templates"
|
||||
destinationDir := "."
|
||||
debug := false
|
||||
if parameter := g.Request.GetParameter(); parameter != "" {
|
||||
for _, param := range strings.Split(parameter, ",") {
|
||||
parts := strings.Split(param, "=")
|
||||
if len(parts) != 2 {
|
||||
log.Printf("Err: invalid parameter: %q", param)
|
||||
continue
|
||||
}
|
||||
switch parts[0] {
|
||||
case "template_dir":
|
||||
templateDir = parts[1]
|
||||
break
|
||||
case "destination_dir":
|
||||
destinationDir = parts[1]
|
||||
break
|
||||
case "debug":
|
||||
switch strings.ToLower(parts[1]) {
|
||||
case "true", "t":
|
||||
debug = true
|
||||
case "false", "f":
|
||||
default:
|
||||
log.Printf("Err: invalid value for debug: %q", parts[1])
|
||||
}
|
||||
break
|
||||
default:
|
||||
log.Printf("Err: unknown parameter: %q", param)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Generate the encoders
|
||||
for _, file := range g.Request.GetProtoFile() {
|
||||
for _, service := range file.GetService() {
|
||||
encoder := NewGenericTemplateBasedEncoder("templates", service, file)
|
||||
encoder := NewGenericTemplateBasedEncoder(templateDir, service, file, debug, destinationDir)
|
||||
g.Response.File = append(g.Response.File, encoder.Files()...)
|
||||
}
|
||||
}
|
||||
|
247
slides/README.md
Normal file
247
slides/README.md
Normal file
@@ -0,0 +1,247 @@
|
||||
# [fit] Protobuf & Code Generation
|
||||
|
||||
### 2016, by Manfred Touron (@moul)
|
||||
|
||||
---
|
||||
|
||||
# overview
|
||||
|
||||
* go-kit is an amazing framework to develop strong micro services
|
||||
* but it requires a lot of boilerplate code
|
||||
* return on experience on go-kit boilerplate code generation
|
||||
|
||||
---
|
||||
|
||||
# protobuf?
|
||||
|
||||
* limited to exchanges (methods and models)
|
||||
* extendable with plugins
|
||||
* contract-based
|
||||
* universal
|
||||
|
||||
---
|
||||
|
||||
# code generation?
|
||||
|
||||
* the good old ./generate.sh bash script
|
||||
* go:generate
|
||||
* make
|
||||
* protobuf + [protoc-gen-gotemplate](https://github.com/moul/protoc-gen-gotemplate)
|
||||
|
||||
---
|
||||
|
||||
# go-kit
|
||||
|
||||
* protobuf-first, rpc-first service framework in Golang
|
||||
* abstract services, endpoints, transports
|
||||
* requires a lot of boilerplate code in multiple packages
|
||||
|
||||
---
|
||||
|
||||
# example: `session.proto`
|
||||
|
||||
```protobuf
|
||||
syntax = "proto3";
|
||||
package session;
|
||||
|
||||
service SessionService {
|
||||
rpc Login(LoginRequest) returns (LoginResponse) {}
|
||||
}
|
||||
|
||||
message LoginRequest {
|
||||
string username = 1;
|
||||
string password = 2;
|
||||
}
|
||||
|
||||
message LoginResponse {
|
||||
string token = 1;
|
||||
string err_msg = 2;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# example: `session.go`
|
||||
|
||||
```go
|
||||
package sessionsvc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"golang.org/x/net/context"
|
||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/session/gen/pb"
|
||||
)
|
||||
|
||||
type Service struct{}
|
||||
|
||||
func New() pb.SessionServiceServer {
|
||||
return &Service{}
|
||||
}
|
||||
|
||||
func (svc *Service) Login(ctx context.Context, in *pb.LoginRequest) (*pb.LoginResponse, error) {
|
||||
// custon code here
|
||||
return nil, fmt.Errorf("not implemented")
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
##### example: `{{.File.Package}}/gen/transports/http/http.go.tmpl`
|
||||
|
||||
```go
|
||||
// source: templates/{{.File.Package}}/gen/transports/http/http.go.tmpl
|
||||
package {{.File.Package}}_httptransport
|
||||
import (
|
||||
gokit_endpoint "github.com/go-kit/kit/endpoint"
|
||||
httptransport "github.com/go-kit/kit/transport/http"
|
||||
endpoints "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/{{.File.Package}}/gen/endpoints"
|
||||
pb "github.com/moul/protoc-gen-gotemplate/examples/go-kit/services/{{.File.Package}}/gen/pb"
|
||||
)
|
||||
```
|
||||
|
||||
```go
|
||||
// result: services/user/gen/transports/http/http.go
|
||||
package user_httptransport
|
||||
import (
|
||||
gokit_endpoint "github.com/go-kit/kit/endpoint"
|
||||
httptransport "github.com/go-kit/kit/transport/http"
|
||||
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"
|
||||
)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
##### example: `{{.File.Package}}/gen/transports/http/http.go.tmpl`
|
||||
|
||||
```go
|
||||
// source: templates/{{.File.Package}}/gen/transports/http/http.go.tmpl
|
||||
{{range .Service.Method}}
|
||||
func Make{{.Name}}Handler(ctx context.Context, svc pb.{{$file.Package | title}}ServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
||||
return httptransport.NewServer(
|
||||
ctx,
|
||||
endpoint,
|
||||
decode{{.Name}}Request,
|
||||
encode{{.Name}}Response,
|
||||
[]httptransport.ServerOption{}...,
|
||||
)
|
||||
}
|
||||
{{end}}
|
||||
```
|
||||
|
||||
```go
|
||||
// result: services/user/gen/transports/http/http.go
|
||||
func MakeGetUserHandler(ctx context.Context, svc pb.UserServiceServer, endpoint gokit_endpoint.Endpoint) *httptransport.Server {
|
||||
return httptransport.NewServer(
|
||||
ctx,
|
||||
endpoint,
|
||||
decodeGetUserRequest,
|
||||
encodeGetUserResponse,
|
||||
[]httptransport.ServerOption{}...,
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
##### example: `{{.File.Package}}/gen/transports/http/http.go.tmpl`
|
||||
|
||||
```go
|
||||
// source: templates/{{.File.Package}}/gen/transports/http/http.go.tmpl
|
||||
func RegisterHandlers(ctx context.Context, svc pb.{{$file.Package | title}}ServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error {
|
||||
{{range .Service.Method}}
|
||||
log.Println("new HTTP endpoint: \"/{{.Name}}\" (service={{$file.Package | title}})")
|
||||
mux.Handle("/{{.Name}}", Make{{.Name}}Handler(ctx, svc, endpoints.{{.Name}}Endpoint))
|
||||
{{end}}
|
||||
return nil
|
||||
}
|
||||
```
|
||||
|
||||
```go
|
||||
// result: services/user/gen/transports/http/http.go
|
||||
func RegisterHandlers(ctx context.Context, svc pb.UserServiceServer, mux *http.ServeMux, endpoints endpoints.Endpoints) error {
|
||||
|
||||
log.Println("new HTTP endpoint: \"/CreateUser\" (service=User)")
|
||||
mux.Handle("/CreateUser", MakeCreateUserHandler(ctx, svc, endpoints.CreateUserEndpoint))
|
||||
|
||||
log.Println("new HTTP endpoint: \"/GetUser\" (service=User)")
|
||||
mux.Handle("/GetUser", MakeGetUserHandler(ctx, svc, endpoints.GetUserEndpoint))
|
||||
|
||||
return nil
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
#### `protoc --gogo_out=plugins=grpc:. ./services/*/*.proto`
|
||||
|
||||
---
|
||||
|
||||
#### `protoc --gotemplate_out=template_dir=./templates:services ./services/*/*.proto`
|
||||
|
||||
---
|
||||
|
||||

|
||||
|
||||
---
|
||||
|
||||

|
||||
|
||||
## 3 services
|
||||
## 6 methods
|
||||
## 149 custom lines
|
||||
## 1429 generated lines
|
||||
## business focus
|
||||
|
||||
---
|
||||
|
||||
# generation usages
|
||||
|
||||
* go-kit boilerplate (see [examples/go-kit](https://github.com/moul/protoc-gen-gotemplate/tree/master/examples/go-kit))
|
||||
* k8s configuration
|
||||
* Dockerfile
|
||||
* documentation
|
||||
* unit-tests
|
||||
* fun
|
||||
|
||||
---
|
||||
|
||||
# pros
|
||||
|
||||
* small custom codebase
|
||||
* templates shipped with code
|
||||
* hardly typed, no reflects
|
||||
* genericity
|
||||
* contrat terms (protobuf) respected
|
||||
* not limited to a language
|
||||
|
||||
---
|
||||
|
||||
# cons
|
||||
|
||||
* the author needs to write its own templates
|
||||
* sometimes difficult to generate valid code
|
||||
* not enough helpers around the code generation yet
|
||||
|
||||
---
|
||||
|
||||
# improvement ideas
|
||||
|
||||
* Support protobufs extensions (i.e, annotations.probo)
|
||||
* Generate one file from multiple services
|
||||
* Add more helpers around the code generation
|
||||
|
||||
---
|
||||
|
||||
# conclusion
|
||||
|
||||
* Useful to keep everything standard
|
||||
* The awesomeness of go-kit without the hassle of writing boilerplate code
|
||||
* Always up-to-date with the contracts
|
||||
|
||||
---
|
||||
|
||||
# questions?
|
||||
|
||||
### github.com/moul/protoc-gen-gotemplate
|
||||
### @moul
|
BIN
slides/assets/session-wc.png
Normal file
BIN
slides/assets/session-wc.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 48 KiB |
BIN
slides/assets/wc.png
Normal file
BIN
slides/assets/wc.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 124 KiB |
BIN
slides/slides.pdf
Normal file
BIN
slides/slides.pdf
Normal file
Binary file not shown.
@@ -1 +0,0 @@
|
||||
{"Filename":"export.json.tmpl","Service":{"name":"DummyService","method":[{"name":"Hhh","input_type":".dummy.Dummy1","output_type":".dummy.Dummy2","options":{}},{"name":"Iii","input_type":".dummy.Dummy2","output_type":".dummy.Dummy1","options":{}}]},"File":{"name":"dummy.proto","package":"dummy","message_type":[{"name":"Dummy1","field":[{"name":"aaa","number":1,"label":1,"type":2,"json_name":"aaa"},{"name":"bbb","number":2,"label":1,"type":9,"json_name":"bbb"},{"name":"ccc","number":3,"label":1,"type":5,"json_name":"ccc"},{"name":"ddd","number":4,"label":1,"type":3,"json_name":"ddd"},{"name":"eee","number":5,"label":3,"type":9,"json_name":"eee"}]},{"name":"Dummy2","field":[{"name":"fff","number":1,"label":1,"type":2,"json_name":"fff"},{"name":"ggg","number":2,"label":1,"type":11,"type_name":".dummy.Dummy1","json_name":"ggg"}]},{"name":"Dummy3"}],"service":[{"name":"DummyService","method":[{"name":"Hhh","input_type":".dummy.Dummy1","output_type":".dummy.Dummy2","options":{}},{"name":"Iii","input_type":".dummy.Dummy2","output_type":".dummy.Dummy1","options":{}}]}],"source_code_info":{"location":[{"span":[0,0,22,1]},{"path":[12],"span":[0,0,18]},{"path":[2],"span":[2,8,13]},{"path":[4,0],"span":[4,0,10,1]},{"path":[4,0,1],"span":[4,8,14]},{"path":[4,0,2,0],"span":[5,2,16]},{"path":[4,0,2,0,4],"span":[5,2,4,16]},{"path":[4,0,2,0,5],"span":[5,2,7]},{"path":[4,0,2,0,1],"span":[5,8,11]},{"path":[4,0,2,0,3],"span":[5,14,15]},{"path":[4,0,2,1],"span":[6,2,17]},{"path":[4,0,2,1,4],"span":[6,2,5,16]},{"path":[4,0,2,1,5],"span":[6,2,8]},{"path":[4,0,2,1,1],"span":[6,9,12]},{"path":[4,0,2,1,3],"span":[6,15,16]},{"path":[4,0,2,2],"span":[7,2,16]},{"path":[4,0,2,2,4],"span":[7,2,6,17]},{"path":[4,0,2,2,5],"span":[7,2,7]},{"path":[4,0,2,2,1],"span":[7,8,11]},{"path":[4,0,2,2,3],"span":[7,14,15]},{"path":[4,0,2,3],"span":[8,2,16]},{"path":[4,0,2,3,4],"span":[8,2,7,16]},{"path":[4,0,2,3,5],"span":[8,2,7]},{"path":[4,0,2,3,1],"span":[8,8,11]},{"path":[4,0,2,3,3],"span":[8,14,15]},{"path":[4,0,2,4],"span":[9,2,26]},{"path":[4,0,2,4,4],"span":[9,2,10]},{"path":[4,0,2,4,5],"span":[9,11,17]},{"path":[4,0,2,4,1],"span":[9,18,21]},{"path":[4,0,2,4,3],"span":[9,24,25]},{"path":[4,1],"span":[12,0,15,1]},{"path":[4,1,1],"span":[12,8,14]},{"path":[4,1,2,0],"span":[13,2,16]},{"path":[4,1,2,0,4],"span":[13,2,12,16]},{"path":[4,1,2,0,5],"span":[13,2,7]},{"path":[4,1,2,0,1],"span":[13,8,11]},{"path":[4,1,2,0,3],"span":[13,14,15]},{"path":[4,1,2,1],"span":[14,2,17]},{"path":[4,1,2,1,4],"span":[14,2,13,16]},{"path":[4,1,2,1,6],"span":[14,2,8]},{"path":[4,1,2,1,1],"span":[14,9,12]},{"path":[4,1,2,1,3],"span":[14,15,16]},{"path":[4,2],"span":[17,0,17]},{"path":[4,2,1],"span":[17,8,14]},{"path":[6,0],"span":[19,0,22,1]},{"path":[6,0,1],"span":[19,8,20]},{"path":[6,0,2,0],"span":[20,2,37]},{"path":[6,0,2,0,1],"span":[20,6,9]},{"path":[6,0,2,0,2],"span":[20,10,16]},{"path":[6,0,2,0,3],"span":[20,27,33]},{"path":[6,0,2,1],"span":[21,2,37]},{"path":[6,0,2,1,1],"span":[21,6,9]},{"path":[6,0,2,1,2],"span":[21,10,16]},{"path":[6,0,2,1,3],"span":[21,27,33]}]},"syntax":"proto3"}}
|
@@ -1 +0,0 @@
|
||||
{{ . | json }}
|
1
vendor/github.com/Masterminds/sprig/.gitignore
generated
vendored
Normal file
1
vendor/github.com/Masterminds/sprig/.gitignore
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
vendor/
|
21
vendor/github.com/Masterminds/sprig/.travis.yml
generated
vendored
Normal file
21
vendor/github.com/Masterminds/sprig/.travis.yml
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.3
|
||||
- 1.4
|
||||
- 1.5
|
||||
- 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
|
||||
|
||||
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
|
16
vendor/github.com/Masterminds/sprig/CHANGELOG.md
generated
vendored
Normal file
16
vendor/github.com/Masterminds/sprig/CHANGELOG.md
generated
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
# Release 1.2.0 (2016-02-01)
|
||||
|
||||
- Added quote and squote
|
||||
- Added b32enc and b32dec
|
||||
- add now takes varargs
|
||||
- biggest now takes varargs
|
||||
|
||||
# Release 1.1.0 (2015-12-29)
|
||||
|
||||
- Added #4: Added contains function. strings.Contains, but with the arguments
|
||||
switched to simplify common pipelines. (thanks krancour)
|
||||
- Added Travis-CI testing support
|
||||
|
||||
# Release 1.0.0 (2015-12-23)
|
||||
|
||||
- Initial release
|
20
vendor/github.com/Masterminds/sprig/LICENSE.txt
generated
vendored
Normal file
20
vendor/github.com/Masterminds/sprig/LICENSE.txt
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
Sprig
|
||||
Copyright (C) 2013 Masterminds
|
||||
|
||||
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.
|
238
vendor/github.com/Masterminds/sprig/README.md
generated
vendored
Normal file
238
vendor/github.com/Masterminds/sprig/README.md
generated
vendored
Normal file
@@ -0,0 +1,238 @@
|
||||
# Sprig: Template functions for Go templates
|
||||
|
||||
The Go language comes with a [built-in template
|
||||
language](http://golang.org/pkg/text/template/), but not
|
||||
very many template functions. This library provides a group of commonly
|
||||
used template functions.
|
||||
|
||||
It is inspired by the template functions found in
|
||||
[Twig](http://twig.sensiolabs.org/documentation).
|
||||
|
||||
[](https://travis-ci.org/Masterminds/sprig)
|
||||
|
||||
## Usage
|
||||
|
||||
API documentation is available [at GoDoc.org](http://godoc.org/github.com/Masterminds/sprig), but
|
||||
read on for standard usage.
|
||||
|
||||
### Load the Sprig library
|
||||
|
||||
To load the Sprig `FuncMap`:
|
||||
|
||||
```go
|
||||
|
||||
import (
|
||||
"github.com/Masterminds/sprig"
|
||||
"html/template"
|
||||
)
|
||||
|
||||
// This example illustrates that the FuncMap *must* be set before the
|
||||
// templates themselves are loaded.
|
||||
tpl := template.Must(
|
||||
template.New("base").Funcs(sprig.FuncMap()).ParseGlob("*.html")
|
||||
)
|
||||
|
||||
|
||||
```
|
||||
|
||||
### Call the functions inside of templates
|
||||
|
||||
By convention, all functions are lowercase. This seems to follow the Go
|
||||
idiom for template functions (as opposed to template methods, which are
|
||||
TitleCase).
|
||||
|
||||
|
||||
Example:
|
||||
|
||||
```
|
||||
{{ "hello!" | upper | repeat 5 }}
|
||||
```
|
||||
|
||||
Produces:
|
||||
|
||||
```
|
||||
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:
|
||||
|
||||
The following principles were used in deciding on which functions to add, and
|
||||
determining how to implement them.
|
||||
|
||||
- Template functions should be used to build layout. Therefore, the following
|
||||
types of operations are within the domain of template functions:
|
||||
- Formatting
|
||||
- Layout
|
||||
- Simple type conversions
|
||||
- Utilities that assist in handling common formatting and layout needs (e.g. arithmetic)
|
||||
- Template functions should not return errors unless there is no way to print
|
||||
a sensible value. For example, converting a string to an integer should not
|
||||
produce an error if conversion fails. Instead, it should display a default
|
||||
value that can be displayed.
|
||||
- Simple math is necessary for grid layouts, pagers, and so on. Complex math
|
||||
(anything other than arithmetic) should be done outside of templates.
|
||||
- Template functions only deal with the data passed into them. They never retrieve
|
||||
data from a source.
|
||||
- Finally, do not override core Go template functions.
|
25
vendor/github.com/Masterminds/sprig/example_test.go
generated
vendored
Normal file
25
vendor/github.com/Masterminds/sprig/example_test.go
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
package sprig
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
func Example() {
|
||||
// Set up variables and template.
|
||||
vars := map[string]interface{}{"Name": " John Jacob Jingleheimer Schmidt "}
|
||||
tpl := `Hello {{.Name | trim | lower}}`
|
||||
|
||||
// Get the Sprig function map.
|
||||
fmap := TxtFuncMap()
|
||||
t := template.Must(template.New("test").Funcs(fmap).Parse(tpl))
|
||||
|
||||
err := t.Execute(os.Stdout, vars)
|
||||
if err != nil {
|
||||
fmt.Printf("Error during template execution: %s", err)
|
||||
return
|
||||
}
|
||||
// Output:
|
||||
// Hello john jacob jingleheimer schmidt
|
||||
}
|
895
vendor/github.com/Masterminds/sprig/functions.go
generated
vendored
Normal file
895
vendor/github.com/Masterminds/sprig/functions.go
generated
vendored
Normal file
@@ -0,0 +1,895 @@
|
||||
/*
|
||||
Sprig: Template functions for Go.
|
||||
|
||||
This package contains a number of utility functions for working with data
|
||||
inside of Go `html/template` and `text/template` files.
|
||||
|
||||
To add these functions, use the `template.Funcs()` method:
|
||||
|
||||
t := templates.New("foo").Funcs(sprig.FuncMap())
|
||||
|
||||
Note that you should add the function map before you parse any template files.
|
||||
|
||||
In several cases, Sprig reverses the order of arguments from the way they
|
||||
appear in the standard library. This is to make it easier to pipe
|
||||
arguments into functions.
|
||||
|
||||
Date Functions
|
||||
|
||||
- date FORMAT TIME: 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 TIME: Format a date for use in the value field of an HTML "date" form element.
|
||||
- dateInZone FORMAT TIME TZ: Like date, but takes three arguments: format, timestamp,
|
||||
timezone.
|
||||
- htmlDateInZone TIME TZ: Like htmlDate, but takes two arguments: timestamp,
|
||||
timezone.
|
||||
|
||||
String Functions
|
||||
|
||||
- abbrev: Truncate a string with ellipses. `abbrev 5 "hello world"` yields "he..."
|
||||
- abbrevboth: Abbreviate from both sides, yielding "...lo wo..."
|
||||
- trunc: Truncate a string (no suffix). `trunc 5 "Hello World"` yields "hello".
|
||||
- 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 "-" "ends-with-"`
|
||||
- trimPrefix: strings.TrimPrefix, but with the argument order reversed `trimPrefix "$" "$5"`
|
||||
- upper: strings.ToUpper
|
||||
- lower: strings.ToLower
|
||||
- nospace: Remove all space characters from a string. `nospace "h e l l o"` becomes "hello"
|
||||
- title: strings.Title
|
||||
- untitle: Remove title casing
|
||||
- repeat: strings.Repeat, but with the arguments switched: `repeat count str`. (This simplifies common pipelines)
|
||||
- substr: Given string, start, and length, return a substr.
|
||||
- initials: Given a multi-word string, return the initials. `initials "Matt Butcher"` returns "MB"
|
||||
- randAlphaNum: Given a length, generate a random alphanumeric sequence
|
||||
- randAlpha: Given a length, generate an alphabetic string
|
||||
- randAscii: Given a length, generate a random ASCII string (symbols included)
|
||||
- randNumeric: Given a length, generate a string of digits.
|
||||
- wrap: Force a line wrap at the given width. `wrap 80 "imagine a longer string"`
|
||||
- wrapWith: Wrap a line at the given length, but using 'sep' instead of a newline. `wrapWith 50, "<br>", $html`
|
||||
- contains: strings.Contains, but with the arguments switched: `contains substr str`. (This simplifies common pipelines)
|
||||
- hasPrefix: strings.hasPrefix, but with the arguments switched
|
||||
- hasSuffix: strings.hasSuffix, but with the arguments switched
|
||||
- quote: Wrap string(s) in double quotation marks, escape the contents by adding '\' before '"'.
|
||||
- squote: Wrap string(s) in double quotation marks, does not escape content.
|
||||
- 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"`
|
||||
- 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.
|
||||
- in64: Convert a string or another numeric type to an int64.
|
||||
- int: Convert a string or another numeric type to an int.
|
||||
- float64: Convert a string or another numeric type 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: Return true if the given value is the zero value for its type.
|
||||
Caveats: structs are always non-empty. This should match the behavior of
|
||||
{{if pipeline}}, but can be used inside of a pipeline.
|
||||
|
||||
OS:
|
||||
- env: Resolve an environment variable
|
||||
- expandenv: Expand a string through the environment
|
||||
|
||||
File Paths:
|
||||
- base: Return the last element of a path. https://golang.org/pkg/path#Base
|
||||
- dir: Remove the last element of a path. https://golang.org/pkg/path#Dir
|
||||
- clean: Clean a path to the shortest equivalent name. (e.g. remove "foo/.."
|
||||
from "foo/../bar.html") https://golang.org/pkg/path#Clean
|
||||
- ext: https://golang.org/pkg/path#Ext
|
||||
- isAbs: https://golang.org/pkg/path#IsAbs
|
||||
|
||||
Encoding:
|
||||
- b64enc: Base 64 encode a string.
|
||||
- b64dec: Base 64 decode a string.
|
||||
|
||||
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: Compares an interface with a string name and returns true if
|
||||
the interface is that `name` or that `*name`. In other words, if the given
|
||||
value matches the given type or is a pointer to the given type, this returns
|
||||
true.
|
||||
- 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.
|
||||
|
||||
Data Structures:
|
||||
|
||||
- tuple: Takes an arbitrary list of items and returns a slice of items. Its
|
||||
tuple-ish properties are mainly gained through the template idiom, and not
|
||||
through an API provided here.
|
||||
- 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.
|
||||
|
||||
Math Functions:
|
||||
|
||||
Integer functions will convert integers of any width to `int64`. If a
|
||||
string is passed in, functions will attempt to convert with
|
||||
`strconv.ParseInt(s, 1064)`. If this fails, the value will be treated as 0.
|
||||
|
||||
- add1: Increment an integer by 1
|
||||
- add: Sum an arbitrary number of integers
|
||||
- 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
|
||||
- max: Return the biggest of a series of one or more integers
|
||||
- min: Return the smallest of a series of one or more integers
|
||||
- biggest: DEPRECATED. Return the biggest of a series of one or more integers
|
||||
|
||||
Crypto Functions:
|
||||
|
||||
- genPrivateKey: Generate a private key for the given cryptosystem. If no
|
||||
argument is supplied, by default it will generate a private key using
|
||||
the RSA algorithm. Accepted values are `rsa`, `dsa`, and `ecdsa`.
|
||||
|
||||
*/
|
||||
package sprig
|
||||
|
||||
import (
|
||||
"crypto/dsa"
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/sha256"
|
||||
"crypto/x509"
|
||||
"encoding/asn1"
|
||||
"encoding/base32"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"math"
|
||||
"math/big"
|
||||
"os"
|
||||
"path"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
ttemplate "text/template"
|
||||
"time"
|
||||
|
||||
util "github.com/aokoli/goutils"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
)
|
||||
|
||||
// Produce the function map.
|
||||
//
|
||||
// Use this to pass the functions into the template engine:
|
||||
//
|
||||
// tpl := template.New("foo").Funcs(sprig.FuncMap))
|
||||
//
|
||||
func FuncMap() template.FuncMap {
|
||||
return HtmlFuncMap()
|
||||
}
|
||||
|
||||
// HermeticTextFuncMap returns a 'text/template'.FuncMap with only repeatable functions.
|
||||
func HermeticTxtFuncMap() ttemplate.FuncMap {
|
||||
r := TxtFuncMap()
|
||||
for _, name := range nonhermeticFunctions {
|
||||
delete(r, name)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// HermeticHtmlFuncMap returns an 'html/template'.Funcmap with only repeatable functions.
|
||||
func HermeticHtmlFuncMap() template.FuncMap {
|
||||
r := HtmlFuncMap()
|
||||
for _, name := range nonhermeticFunctions {
|
||||
delete(r, name)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// TextFuncMap returns a 'text/template'.FuncMap
|
||||
func TxtFuncMap() ttemplate.FuncMap {
|
||||
return ttemplate.FuncMap(genericMap)
|
||||
}
|
||||
|
||||
// HtmlFuncMap returns an 'html/template'.Funcmap
|
||||
func HtmlFuncMap() template.FuncMap {
|
||||
return template.FuncMap(genericMap)
|
||||
}
|
||||
|
||||
// These functions are not guaranteed to evaluate to the same result for given input, because they
|
||||
// refer to the environemnt or global state.
|
||||
var nonhermeticFunctions = []string{
|
||||
// Date functions
|
||||
"date",
|
||||
"date_in_zone",
|
||||
"date_modify",
|
||||
"now",
|
||||
"htmlDate",
|
||||
"htmlDateInZone",
|
||||
"dateInZone",
|
||||
"dateModify",
|
||||
|
||||
// Strings
|
||||
"randAlphaNum",
|
||||
"randAlpha",
|
||||
"randAscii",
|
||||
"randNumeric",
|
||||
"uuidv4",
|
||||
|
||||
// OS
|
||||
"env",
|
||||
"expandenv",
|
||||
}
|
||||
|
||||
var genericMap = map[string]interface{}{
|
||||
"hello": func() string { return "Hello!" },
|
||||
|
||||
// Date functions
|
||||
"date": date,
|
||||
"date_in_zone": dateInZone,
|
||||
"date_modify": dateModify,
|
||||
"now": func() time.Time { return time.Now() },
|
||||
"htmlDate": htmlDate,
|
||||
"htmlDateInZone": htmlDateInZone,
|
||||
"dateInZone": dateInZone,
|
||||
"dateModify": dateModify,
|
||||
|
||||
// Strings
|
||||
"abbrev": abbrev,
|
||||
"abbrevboth": abbrevboth,
|
||||
"trunc": trunc,
|
||||
"trim": strings.TrimSpace,
|
||||
"upper": strings.ToUpper,
|
||||
"lower": strings.ToLower,
|
||||
"title": strings.Title,
|
||||
"untitle": untitle,
|
||||
"substr": substring,
|
||||
// Switch order so that "foo" | repeat 5
|
||||
"repeat": func(count int, str string) string { return strings.Repeat(str, count) },
|
||||
// Deprecated: Use trimAll.
|
||||
"trimall": func(a, b string) string { return strings.Trim(b, a) },
|
||||
// Switch order so that "$foo" | trimall "$"
|
||||
"trimAll": func(a, b string) string { return strings.Trim(b, a) },
|
||||
"trimSuffix": func(a, b string) string { return strings.TrimSuffix(b, a) },
|
||||
"trimPrefix": func(a, b string) string { return strings.TrimPrefix(b, a) },
|
||||
"nospace": util.DeleteWhiteSpace,
|
||||
"initials": initials,
|
||||
"randAlphaNum": randAlphaNumeric,
|
||||
"randAlpha": randAlpha,
|
||||
"randAscii": randAscii,
|
||||
"randNumeric": randNumeric,
|
||||
"swapcase": util.SwapCase,
|
||||
"wrap": func(l int, s string) string { return util.Wrap(s, l) },
|
||||
"wrapWith": func(l int, sep, str string) string { return util.WrapCustom(str, l, sep, true) },
|
||||
// Switch order so that "foobar" | contains "foo"
|
||||
"contains": func(substr string, str string) bool { return strings.Contains(str, substr) },
|
||||
"hasPrefix": func(substr string, str string) bool { return strings.HasPrefix(str, substr) },
|
||||
"hasSuffix": func(substr string, str string) bool { return strings.HasSuffix(str, substr) },
|
||||
"quote": quote,
|
||||
"squote": squote,
|
||||
"cat": cat,
|
||||
"indent": indent,
|
||||
"replace": replace,
|
||||
"plural": plural,
|
||||
"sha256sum": sha256sum,
|
||||
|
||||
// Wrap Atoi to stop errors.
|
||||
"atoi": func(a string) int { i, _ := strconv.Atoi(a); return i },
|
||||
"int64": toInt64,
|
||||
"int": toInt,
|
||||
"float64": toFloat64,
|
||||
|
||||
//"gt": func(a, b int) bool {return a > b},
|
||||
//"gte": func(a, b int) bool {return a >= b},
|
||||
//"lt": func(a, b int) bool {return a < b},
|
||||
//"lte": func(a, b int) bool {return a <= b},
|
||||
|
||||
// split "/" foo/bar returns map[int]string{0: foo, 1: bar}
|
||||
"split": split,
|
||||
|
||||
"until": until,
|
||||
"untilStep": untilStep,
|
||||
|
||||
// VERY basic arithmetic.
|
||||
"add1": func(i interface{}) int64 { return toInt64(i) + 1 },
|
||||
"add": func(i ...interface{}) int64 {
|
||||
var a int64 = 0
|
||||
for _, b := range i {
|
||||
a += toInt64(b)
|
||||
}
|
||||
return a
|
||||
},
|
||||
"sub": func(a, b interface{}) int64 { return toInt64(a) - toInt64(b) },
|
||||
"div": func(a, b interface{}) int64 { return toInt64(a) / toInt64(b) },
|
||||
"mod": func(a, b interface{}) int64 { return toInt64(a) % toInt64(b) },
|
||||
"mul": func(a interface{}, v ...interface{}) int64 {
|
||||
val := toInt64(a)
|
||||
for _, b := range v {
|
||||
val = val * toInt64(b)
|
||||
}
|
||||
return val
|
||||
},
|
||||
"biggest": max,
|
||||
"max": max,
|
||||
"min": min,
|
||||
|
||||
// string slices. Note that we reverse the order b/c that's better
|
||||
// for template processing.
|
||||
"join": func(sep string, ss []string) string { return strings.Join(ss, sep) },
|
||||
|
||||
// Defaults
|
||||
"default": dfault,
|
||||
"empty": empty,
|
||||
|
||||
// Reflection
|
||||
"typeOf": typeOf,
|
||||
"typeIs": typeIs,
|
||||
"typeIsLike": typeIsLike,
|
||||
"kindOf": kindOf,
|
||||
"kindIs": kindIs,
|
||||
|
||||
// OS:
|
||||
"env": func(s string) string { return os.Getenv(s) },
|
||||
"expandenv": func(s string) string { return os.ExpandEnv(s) },
|
||||
|
||||
// File Paths:
|
||||
"base": path.Base,
|
||||
"dir": path.Dir,
|
||||
"clean": path.Clean,
|
||||
"ext": path.Ext,
|
||||
"isAbs": path.IsAbs,
|
||||
|
||||
// Encoding:
|
||||
"b64enc": base64encode,
|
||||
"b64dec": base64decode,
|
||||
"b32enc": base32encode,
|
||||
"b32dec": base32decode,
|
||||
|
||||
// Data Structures:
|
||||
"tuple": tuple,
|
||||
"dict": dict,
|
||||
"set": set,
|
||||
"unset": unset,
|
||||
"hasKey": hasKey,
|
||||
|
||||
// Crypto:
|
||||
"genPrivateKey": generatePrivateKey,
|
||||
|
||||
// UUIDs:
|
||||
"uuidv4": uuidv4,
|
||||
}
|
||||
|
||||
func split(sep, orig string) map[string]string {
|
||||
parts := strings.Split(orig, sep)
|
||||
res := make(map[string]string, len(parts))
|
||||
for i, v := range parts {
|
||||
res["_"+strconv.Itoa(i)] = v
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// substring creates a substring of the given string.
|
||||
//
|
||||
// If start is < 0, this calls string[:length].
|
||||
//
|
||||
// If start is >= 0 and length < 0, this calls string[start:]
|
||||
//
|
||||
// Otherwise, this calls string[start, length].
|
||||
func substring(start, length int, s string) string {
|
||||
if start < 0 {
|
||||
return s[:length]
|
||||
}
|
||||
if length < 0 {
|
||||
return s[start:]
|
||||
}
|
||||
return s[start:length]
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
func max(a interface{}, i ...interface{}) int64 {
|
||||
aa := toInt64(a)
|
||||
for _, b := range i {
|
||||
bb := toInt64(b)
|
||||
if bb > aa {
|
||||
aa = bb
|
||||
}
|
||||
}
|
||||
return aa
|
||||
}
|
||||
|
||||
func min(a interface{}, i ...interface{}) int64 {
|
||||
aa := toInt64(a)
|
||||
for _, b := range i {
|
||||
bb := toInt64(b)
|
||||
if bb < aa {
|
||||
aa = bb
|
||||
}
|
||||
}
|
||||
return aa
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// typeIs returns true if the src is the type named in target.
|
||||
func typeIs(target string, src interface{}) bool {
|
||||
return target == typeOf(src)
|
||||
}
|
||||
|
||||
func typeIsLike(target string, src interface{}) bool {
|
||||
t := typeOf(src)
|
||||
return target == t || "*"+target == t
|
||||
}
|
||||
|
||||
func typeOf(src interface{}) string {
|
||||
return fmt.Sprintf("%T", src)
|
||||
}
|
||||
|
||||
func kindIs(target string, src interface{}) bool {
|
||||
return target == kindOf(src)
|
||||
}
|
||||
|
||||
func kindOf(src interface{}) string {
|
||||
return reflect.ValueOf(src).Kind().String()
|
||||
}
|
||||
|
||||
func base64encode(v string) string {
|
||||
return base64.StdEncoding.EncodeToString([]byte(v))
|
||||
}
|
||||
|
||||
func base64decode(v string) string {
|
||||
data, err := base64.StdEncoding.DecodeString(v)
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func base32encode(v string) string {
|
||||
return base32.StdEncoding.EncodeToString([]byte(v))
|
||||
}
|
||||
|
||||
func base32decode(v string) string {
|
||||
data, err := base32.StdEncoding.DecodeString(v)
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func abbrev(width int, s string) string {
|
||||
if width < 4 {
|
||||
return s
|
||||
}
|
||||
r, _ := util.Abbreviate(s, width)
|
||||
return r
|
||||
}
|
||||
|
||||
func abbrevboth(left, right int, s string) string {
|
||||
if right < 4 || left > 0 && right < 7 {
|
||||
return s
|
||||
}
|
||||
r, _ := util.AbbreviateFull(s, left, right)
|
||||
return r
|
||||
}
|
||||
func initials(s string) string {
|
||||
// Wrap this just to eliminate the var args, which templates don't do well.
|
||||
return util.Initials(s)
|
||||
}
|
||||
|
||||
func randAlphaNumeric(count int) string {
|
||||
// It is not possible, it appears, to actually generate an error here.
|
||||
r, _ := util.RandomAlphaNumeric(count)
|
||||
return r
|
||||
}
|
||||
|
||||
func randAlpha(count int) string {
|
||||
r, _ := util.RandomAlphabetic(count)
|
||||
return r
|
||||
}
|
||||
|
||||
func randAscii(count int) string {
|
||||
r, _ := util.RandomAscii(count)
|
||||
return r
|
||||
}
|
||||
|
||||
func randNumeric(count int) string {
|
||||
r, _ := util.RandomNumeric(count)
|
||||
return r
|
||||
}
|
||||
|
||||
func untitle(str string) string {
|
||||
return util.Uncapitalize(str)
|
||||
}
|
||||
|
||||
func quote(str ...interface{}) string {
|
||||
out := make([]string, len(str))
|
||||
for i, s := range str {
|
||||
out[i] = fmt.Sprintf("%q", strval(s))
|
||||
}
|
||||
return strings.Join(out, " ")
|
||||
}
|
||||
|
||||
func squote(str ...interface{}) string {
|
||||
out := make([]string, len(str))
|
||||
for i, s := range str {
|
||||
out[i] = fmt.Sprintf("'%v'", s)
|
||||
}
|
||||
return strings.Join(out, " ")
|
||||
}
|
||||
|
||||
func tuple(v ...interface{}) []interface{} {
|
||||
return v
|
||||
}
|
||||
|
||||
func set(d map[string]interface{}, key string, value interface{}) map[string]interface{} {
|
||||
d[key] = value
|
||||
return d
|
||||
}
|
||||
|
||||
func unset(d map[string]interface{}, key string) map[string]interface{} {
|
||||
delete(d, key)
|
||||
return d
|
||||
}
|
||||
|
||||
func hasKey(d map[string]interface{}, key string) bool {
|
||||
_, ok := d[key]
|
||||
return ok
|
||||
}
|
||||
|
||||
func dict(v ...interface{}) map[string]interface{} {
|
||||
dict := map[string]interface{}{}
|
||||
lenv := len(v)
|
||||
for i := 0; i < lenv; i += 2 {
|
||||
key := strval(v[i])
|
||||
if i+1 >= lenv {
|
||||
dict[key] = ""
|
||||
continue
|
||||
}
|
||||
dict[key] = v[i+1]
|
||||
}
|
||||
return dict
|
||||
}
|
||||
|
||||
func strval(v interface{}) string {
|
||||
switch v := v.(type) {
|
||||
case string:
|
||||
return v
|
||||
case []byte:
|
||||
return string(v)
|
||||
case error:
|
||||
return v.Error()
|
||||
case fmt.Stringer:
|
||||
return v.String()
|
||||
default:
|
||||
return fmt.Sprintf("%v", v)
|
||||
}
|
||||
}
|
||||
|
||||
// toFloat64 converts 64-bit floats
|
||||
func toFloat64(v interface{}) float64 {
|
||||
if str, ok := v.(string); ok {
|
||||
iv, err := strconv.ParseFloat(str, 64)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return iv
|
||||
}
|
||||
|
||||
val := reflect.Indirect(reflect.ValueOf(v))
|
||||
switch val.Kind() {
|
||||
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
|
||||
return float64(val.Int())
|
||||
case reflect.Uint8, reflect.Uint16, reflect.Uint32:
|
||||
return float64(val.Uint())
|
||||
case reflect.Uint, reflect.Uint64:
|
||||
return float64(val.Uint())
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return val.Float()
|
||||
case reflect.Bool:
|
||||
if val.Bool() == true {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func toInt(v interface{}) int {
|
||||
//It's not optimal. Bud I don't want duplicate toInt64 code.
|
||||
return int(toInt64(v))
|
||||
}
|
||||
|
||||
// toInt64 converts integer types to 64-bit integers
|
||||
func toInt64(v interface{}) int64 {
|
||||
if str, ok := v.(string); ok {
|
||||
iv, err := strconv.ParseInt(str, 10, 64)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return iv
|
||||
}
|
||||
|
||||
val := reflect.Indirect(reflect.ValueOf(v))
|
||||
switch val.Kind() {
|
||||
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int:
|
||||
return val.Int()
|
||||
case reflect.Uint8, reflect.Uint16, reflect.Uint32:
|
||||
return int64(val.Uint())
|
||||
case reflect.Uint, reflect.Uint64:
|
||||
tv := val.Uint()
|
||||
if tv <= math.MaxInt64 {
|
||||
return int64(tv)
|
||||
}
|
||||
// TODO: What is the sensible thing to do here?
|
||||
return math.MaxInt64
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return int64(val.Float())
|
||||
case reflect.Bool:
|
||||
if val.Bool() == true {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
func trunc(c int, s string) string {
|
||||
if len(s) <= c {
|
||||
return s
|
||||
}
|
||||
return s[0:c]
|
||||
}
|
||||
|
||||
func cat(v ...interface{}) string {
|
||||
r := strings.TrimSpace(strings.Repeat("%v ", len(v)))
|
||||
return fmt.Sprintf(r, v...)
|
||||
}
|
||||
|
||||
func indent(spaces int, v string) string {
|
||||
pad := strings.Repeat(" ", spaces)
|
||||
return pad + strings.Replace(v, "\n", "\n"+pad, -1)
|
||||
}
|
||||
|
||||
func replace(old, new, src string) string {
|
||||
return strings.Replace(src, old, new, -1)
|
||||
}
|
||||
|
||||
func plural(one, many string, count int) string {
|
||||
if count == 1 {
|
||||
return one
|
||||
}
|
||||
return many
|
||||
}
|
||||
|
||||
func sha256sum(input string) string {
|
||||
hash := sha256.Sum256([]byte(input))
|
||||
return hex.EncodeToString(hash[:])
|
||||
}
|
||||
|
||||
func until(count int) []int {
|
||||
step := 1
|
||||
if count < 0 {
|
||||
step = -1
|
||||
}
|
||||
return untilStep(0, count, step)
|
||||
}
|
||||
|
||||
func untilStep(start, stop, step int) []int {
|
||||
v := []int{}
|
||||
|
||||
if stop < start {
|
||||
if step >= 0 {
|
||||
return v
|
||||
}
|
||||
for i := start; i > stop; i += step {
|
||||
v = append(v, i)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
if step <= 0 {
|
||||
return v
|
||||
}
|
||||
for i := start; i < stop; i += step {
|
||||
v = append(v, i)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// uuidv4 provides a safe and secure UUID v4 implementation
|
||||
func uuidv4() string {
|
||||
return fmt.Sprintf("%s", uuid.NewV4())
|
||||
}
|
731
vendor/github.com/Masterminds/sprig/functions_test.go
generated
vendored
Normal file
731
vendor/github.com/Masterminds/sprig/functions_test.go
generated
vendored
Normal file
@@ -0,0 +1,731 @@
|
||||
package sprig
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base32"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"text/template"
|
||||
|
||||
"github.com/aokoli/goutils"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// This is woefully incomplete. Please help.
|
||||
|
||||
func TestSubstr(t *testing.T) {
|
||||
tpl := `{{"fooo" | substr 0 3 }}`
|
||||
if err := runt(tpl, "foo"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrunc(t *testing.T) {
|
||||
tpl := `{{ "foooooo" | trunc 3 }}`
|
||||
if err := runt(tpl, "foo"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQuote(t *testing.T) {
|
||||
tpl := `{{quote "a" "b" "c"}}`
|
||||
if err := runt(tpl, `"a" "b" "c"`); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
tpl = `{{quote "\"a\"" "b" "c"}}`
|
||||
if err := runt(tpl, `"\"a\"" "b" "c"`); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
tpl = `{{quote 1 2 3 }}`
|
||||
if err := runt(tpl, `"1" "2" "3"`); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
func TestSquote(t *testing.T) {
|
||||
tpl := `{{squote "a" "b" "c"}}`
|
||||
if err := runt(tpl, `'a' 'b' 'c'`); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
tpl = `{{squote 1 2 3 }}`
|
||||
if err := runt(tpl, `'1' '2' '3'`); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContains(t *testing.T) {
|
||||
// Mainly, we're just verifying the paramater order swap.
|
||||
tests := []string{
|
||||
`{{if contains "cat" "fair catch"}}1{{end}}`,
|
||||
`{{if hasPrefix "cat" "catch"}}1{{end}}`,
|
||||
`{{if hasSuffix "cat" "ducat"}}1{{end}}`,
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if err := runt(tt, "1"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrim(t *testing.T) {
|
||||
tests := []string{
|
||||
`{{trim " 5.00 "}}`,
|
||||
`{{trimAll "$" "$5.00$"}}`,
|
||||
`{{trimPrefix "$" "$5.00"}}`,
|
||||
`{{trimSuffix "$" "5.00$"}}`,
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if err := runt(tt, "5.00"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdd(t *testing.T) {
|
||||
tpl := `{{ 3 | add 1 2}}`
|
||||
if err := runt(tpl, `6`); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMul(t *testing.T) {
|
||||
tpl := `{{ 1 | mul "2" 3 "4"}}`
|
||||
if err := runt(tpl, `24`); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHtmlDate(t *testing.T) {
|
||||
t.Skip()
|
||||
tpl := `{{ htmlDate 0}}`
|
||||
if err := runt(tpl, "1970-01-01"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBiggest(t *testing.T) {
|
||||
tpl := `{{ biggest 1 2 3 345 5 6 7}}`
|
||||
if err := runt(tpl, `345`); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
tpl = `{{ max 345}}`
|
||||
if err := runt(tpl, `345`); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
func TestMin(t *testing.T) {
|
||||
tpl := `{{ min 1 2 3 345 5 6 7}}`
|
||||
if err := runt(tpl, `1`); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
tpl = `{{ min 345}}`
|
||||
if err := runt(tpl, `345`); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
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 TestToFloat64(t *testing.T) {
|
||||
target := float64(102)
|
||||
if target != toFloat64(int8(102)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if target != toFloat64(int(102)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if target != toFloat64(int32(102)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if target != toFloat64(int16(102)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if target != toFloat64(int64(102)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if target != toFloat64("102") {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if 0 != toFloat64("frankie") {
|
||||
t.Errorf("Expected 0")
|
||||
}
|
||||
if target != toFloat64(uint16(102)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if target != toFloat64(uint64(102)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if 102.1234 != toFloat64(float64(102.1234)) {
|
||||
t.Errorf("Expected 102.1234")
|
||||
}
|
||||
if 1 != toFloat64(true) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
}
|
||||
func TestToInt64(t *testing.T) {
|
||||
target := int64(102)
|
||||
if target != toInt64(int8(102)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if target != toInt64(int(102)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if target != toInt64(int32(102)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if target != toInt64(int16(102)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if target != toInt64(int64(102)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if target != toInt64("102") {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if 0 != toInt64("frankie") {
|
||||
t.Errorf("Expected 0")
|
||||
}
|
||||
if target != toInt64(uint16(102)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if target != toInt64(uint64(102)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if target != toInt64(float64(102.1234)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if 1 != toInt64(true) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
}
|
||||
|
||||
func TestToInt(t *testing.T) {
|
||||
target := int(102)
|
||||
if target != toInt(int8(102)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if target != toInt(int(102)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if target != toInt(int32(102)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if target != toInt(int16(102)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if target != toInt(int64(102)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if target != toInt("102") {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if 0 != toInt("frankie") {
|
||||
t.Errorf("Expected 0")
|
||||
}
|
||||
if target != toInt(uint16(102)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if target != toInt(uint64(102)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if target != toInt(float64(102.1234)) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
if 1 != toInt(true) {
|
||||
t.Errorf("Expected 102")
|
||||
}
|
||||
}
|
||||
|
||||
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 TestSplit(t *testing.T) {
|
||||
tpl := `{{$v := "foo$bar$baz" | split "$"}}{{$v._0}}`
|
||||
if err := runt(tpl, "foo"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
type fixtureTO struct {
|
||||
Name, Value string
|
||||
}
|
||||
|
||||
func TestTypeOf(t *testing.T) {
|
||||
f := &fixtureTO{"hello", "world"}
|
||||
tpl := `{{typeOf .}}`
|
||||
if err := runtv(tpl, "*sprig.fixtureTO", f); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKindOf(t *testing.T) {
|
||||
tpl := `{{kindOf .}}`
|
||||
|
||||
f := fixtureTO{"hello", "world"}
|
||||
if err := runtv(tpl, "struct", f); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
f2 := []string{"hello"}
|
||||
if err := runtv(tpl, "slice", f2); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
var f3 *fixtureTO = nil
|
||||
if err := runtv(tpl, "ptr", f3); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTypeIs(t *testing.T) {
|
||||
f := &fixtureTO{"hello", "world"}
|
||||
tpl := `{{if typeIs "*sprig.fixtureTO" .}}t{{else}}f{{end}}`
|
||||
if err := runtv(tpl, "t", f); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
f2 := "hello"
|
||||
if err := runtv(tpl, "f", f2); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
func TestTypeIsLike(t *testing.T) {
|
||||
f := "foo"
|
||||
tpl := `{{if typeIsLike "string" .}}t{{else}}f{{end}}`
|
||||
if err := runtv(tpl, "t", f); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
// Now make a pointer. Should still match.
|
||||
f2 := &f
|
||||
if err := runtv(tpl, "t", f2); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
func TestKindIs(t *testing.T) {
|
||||
f := &fixtureTO{"hello", "world"}
|
||||
tpl := `{{if kindIs "ptr" .}}t{{else}}f{{end}}`
|
||||
if err := runtv(tpl, "t", f); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
f2 := "hello"
|
||||
if err := runtv(tpl, "f", f2); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnv(t *testing.T) {
|
||||
os.Setenv("FOO", "bar")
|
||||
tpl := `{{env "FOO"}}`
|
||||
if err := runt(tpl, "bar"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExpandEnv(t *testing.T) {
|
||||
os.Setenv("FOO", "bar")
|
||||
tpl := `{{expandenv "Hello $FOO"}}`
|
||||
if err := runt(tpl, "Hello bar"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBase64EncodeDecode(t *testing.T) {
|
||||
magicWord := "coffee"
|
||||
expect := base64.StdEncoding.EncodeToString([]byte(magicWord))
|
||||
|
||||
if expect == magicWord {
|
||||
t.Fatal("Encoder doesn't work.")
|
||||
}
|
||||
|
||||
tpl := `{{b64enc "coffee"}}`
|
||||
if err := runt(tpl, expect); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
tpl = fmt.Sprintf("{{b64dec %q}}", expect)
|
||||
if err := runt(tpl, magicWord); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
func TestBase32EncodeDecode(t *testing.T) {
|
||||
magicWord := "coffee"
|
||||
expect := base32.StdEncoding.EncodeToString([]byte(magicWord))
|
||||
|
||||
if expect == magicWord {
|
||||
t.Fatal("Encoder doesn't work.")
|
||||
}
|
||||
|
||||
tpl := `{{b32enc "coffee"}}`
|
||||
if err := runt(tpl, expect); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
tpl = fmt.Sprintf("{{b32dec %q}}", expect)
|
||||
if err := runt(tpl, magicWord); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoutils(t *testing.T) {
|
||||
tests := map[string]string{
|
||||
`{{abbrev 5 "hello world"}}`: "he...",
|
||||
`{{abbrevboth 5 10 "1234 5678 9123"}}`: "...5678...",
|
||||
`{{nospace "h e l l o "}}`: "hello",
|
||||
`{{untitle "First Try"}}`: "first try", //https://youtu.be/44-RsrF_V_w
|
||||
`{{initials "First Try"}}`: "FT",
|
||||
`{{wrap 5 "Hello World"}}`: "Hello\nWorld",
|
||||
`{{wrapWith 5 "\t" "Hello World"}}`: "Hello\tWorld",
|
||||
}
|
||||
for k, v := range tests {
|
||||
t.Log(k)
|
||||
if err := runt(k, v); err != nil {
|
||||
t.Errorf("Error on tpl %s: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandom(t *testing.T) {
|
||||
// One of the things I love about Go:
|
||||
goutils.RANDOM = rand.New(rand.NewSource(1))
|
||||
|
||||
// Because we're using a random number generator, we need these to go in
|
||||
// a predictable sequence:
|
||||
if err := runt(`{{randAlphaNum 5}}`, "9bzRv"); err != nil {
|
||||
t.Errorf("Error on tpl %s: %s", err)
|
||||
}
|
||||
if err := runt(`{{randAlpha 5}}`, "VjwGe"); err != nil {
|
||||
t.Errorf("Error on tpl %s: %s", err)
|
||||
}
|
||||
if err := runt(`{{randAscii 5}}`, "1KA5p"); err != nil {
|
||||
t.Errorf("Error on tpl %s: %s", err)
|
||||
}
|
||||
if err := runt(`{{randNumeric 5}}`, "26018"); err != nil {
|
||||
t.Errorf("Error on tpl %s: %s", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestCat(t *testing.T) {
|
||||
tpl := `{{$b := "b"}}{{"c" | cat "a" $b}}`
|
||||
if err := runt(tpl, "a b c"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIndent(t *testing.T) {
|
||||
tpl := `{{indent 4 "a\nb\nc"}}`
|
||||
if err := runt(tpl, " a\n b\n c"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplace(t *testing.T) {
|
||||
tpl := `{{"I Am Henry VIII" | replace " " "-"}}`
|
||||
if err := runt(tpl, "I-Am-Henry-VIII"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlural(t *testing.T) {
|
||||
tpl := `{{$num := len "two"}}{{$num}} {{$num | plural "1 char" "chars"}}`
|
||||
if err := runt(tpl, "3 chars"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
tpl = `{{len "t" | plural "cheese" "%d chars"}}`
|
||||
if err := runt(tpl, "cheese"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSha256Sum(t *testing.T) {
|
||||
tpl := `{{"abc" | sha256sum}}`
|
||||
if err := runt(tpl, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTuple(t *testing.T) {
|
||||
tpl := `{{$t := tuple 1 "a" "foo"}}{{index $t 2}}{{index $t 0 }}{{index $t 1}}`
|
||||
if err := runt(tpl, "foo1a"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDict(t *testing.T) {
|
||||
tpl := `{{$d := dict 1 2 "three" "four" 5}}{{range $k, $v := $d}}{{$k}}{{$v}}{{end}}`
|
||||
out, err := runRaw(tpl, nil)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if len(out) != 12 {
|
||||
t.Errorf("Expected length 12, got %d", len(out))
|
||||
}
|
||||
// dict does not guarantee ordering because it is backed by a map.
|
||||
if !strings.Contains(out, "12") {
|
||||
t.Error("Expected grouping 12")
|
||||
}
|
||||
if !strings.Contains(out, "threefour") {
|
||||
t.Error("Expected grouping threefour")
|
||||
}
|
||||
if !strings.Contains(out, "5") {
|
||||
t.Error("Expected 5")
|
||||
}
|
||||
tpl = `{{$t := dict "I" "shot" "the" "albatross"}}{{$t.the}} {{$t.I}}`
|
||||
if err := runt(tpl, "albatross shot"); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnset(t *testing.T) {
|
||||
tpl := `{{- $d := dict "one" 1 "two" 222222 -}}
|
||||
{{- $_ := unset $d "two" -}}
|
||||
{{- range $k, $v := $d}}{{$k}}{{$v}}{{- end -}}
|
||||
`
|
||||
|
||||
expect := "one1"
|
||||
if err := runt(tpl, expect); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
func TestHasKey(t *testing.T) {
|
||||
tpl := `{{- $d := dict "one" 1 "two" 222222 -}}
|
||||
{{- if hasKey $d "one" -}}1{{- end -}}
|
||||
`
|
||||
|
||||
expect := "1"
|
||||
if err := runt(tpl, expect); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSet(t *testing.T) {
|
||||
tpl := `{{- $d := dict "one" 1 "two" 222222 -}}
|
||||
{{- $_ := set $d "two" 2 -}}
|
||||
{{- $_ := set $d "three" 3 -}}
|
||||
{{- if hasKey $d "one" -}}{{$d.one}}{{- end -}}
|
||||
{{- if hasKey $d "two" -}}{{$d.two}}{{- end -}}
|
||||
{{- if hasKey $d "three" -}}{{$d.three}}{{- end -}}
|
||||
`
|
||||
|
||||
expect := "123"
|
||||
if err := runt(tpl, expect); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUntil(t *testing.T) {
|
||||
tests := map[string]string{
|
||||
`{{range $i, $e := until 5}}{{$i}}{{$e}}{{end}}`: "0011223344",
|
||||
`{{range $i, $e := until -5}}{{$i}}{{$e}} {{end}}`: "00 1-1 2-2 3-3 4-4 ",
|
||||
}
|
||||
for tpl, expect := range tests {
|
||||
if err := runt(tpl, expect); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestUntilStep(t *testing.T) {
|
||||
tests := map[string]string{
|
||||
`{{range $i, $e := untilStep 0 5 1}}{{$i}}{{$e}}{{end}}`: "0011223344",
|
||||
`{{range $i, $e := untilStep 3 6 1}}{{$i}}{{$e}}{{end}}`: "031425",
|
||||
`{{range $i, $e := untilStep 0 -10 -2}}{{$i}}{{$e}} {{end}}`: "00 1-2 2-4 3-6 4-8 ",
|
||||
`{{range $i, $e := untilStep 3 0 1}}{{$i}}{{$e}}{{end}}`: "",
|
||||
`{{range $i, $e := untilStep 3 99 0}}{{$i}}{{$e}}{{end}}`: "",
|
||||
`{{range $i, $e := untilStep 3 99 -1}}{{$i}}{{$e}}{{end}}`: "",
|
||||
`{{range $i, $e := untilStep 3 0 0}}{{$i}}{{$e}}{{end}}`: "",
|
||||
}
|
||||
for tpl, expect := range tests {
|
||||
if err := runt(tpl, expect); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestBase(t *testing.T) {
|
||||
assert.NoError(t, runt(`{{ base "foo/bar" }}`, "bar"))
|
||||
}
|
||||
|
||||
func TestDir(t *testing.T) {
|
||||
assert.NoError(t, runt(`{{ dir "foo/bar/baz" }}`, "foo/bar"))
|
||||
}
|
||||
|
||||
func TestIsAbs(t *testing.T) {
|
||||
assert.NoError(t, runt(`{{ isAbs "/foo" }}`, "true"))
|
||||
assert.NoError(t, runt(`{{ isAbs "foo" }}`, "false"))
|
||||
}
|
||||
|
||||
func TestClean(t *testing.T) {
|
||||
assert.NoError(t, runt(`{{ clean "/foo/../foo/../bar" }}`, "/bar"))
|
||||
}
|
||||
|
||||
func TestExt(t *testing.T) {
|
||||
assert.NoError(t, runt(`{{ ext "/foo/bar/baz.txt" }}`, ".txt"))
|
||||
}
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
fmap := TxtFuncMap()
|
||||
delete(fmap, "split")
|
||||
if _, ok := fmap["split"]; ok {
|
||||
t.Error("Failed to delete split from map")
|
||||
}
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
}
|
||||
|
||||
func runt(tpl, expect string) error {
|
||||
return runtv(tpl, expect, map[string]string{})
|
||||
}
|
||||
func runtv(tpl, expect string, vars interface{}) error {
|
||||
fmap := TxtFuncMap()
|
||||
t := template.Must(template.New("test").Funcs(fmap).Parse(tpl))
|
||||
var b bytes.Buffer
|
||||
err := t.Execute(&b, vars)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if expect != b.String() {
|
||||
return fmt.Errorf("Expected '%s', got '%s'", expect, b.String())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func runRaw(tpl string, vars interface{}) (string, error) {
|
||||
fmap := TxtFuncMap()
|
||||
t := template.Must(template.New("test").Funcs(fmap).Parse(tpl))
|
||||
var b bytes.Buffer
|
||||
err := t.Execute(&b, vars)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return b.String(), nil
|
||||
}
|
8
vendor/github.com/Masterminds/sprig/glide.lock
generated
vendored
Normal file
8
vendor/github.com/Masterminds/sprig/glide.lock
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
hash: a8ed42a70698b4d199b5de7fa33e7c48251651e6ccf97d007f546cb72a5d0f8f
|
||||
updated: 2016-09-30T12:23:39.512939213-06:00
|
||||
imports:
|
||||
- name: github.com/aokoli/goutils
|
||||
version: 9c37978a95bd5c709a15883b6242714ea6709e64
|
||||
- name: github.com/satori/go.uuid
|
||||
version: 879c5887cd475cd7864858769793b2ceb0d44feb
|
||||
testImports: []
|
5
vendor/github.com/Masterminds/sprig/glide.yaml
generated
vendored
Normal file
5
vendor/github.com/Masterminds/sprig/glide.yaml
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
package: github.com/Masterminds/sprig
|
||||
import:
|
||||
- package: github.com/aokoli/goutils
|
||||
- package: github.com/satori/go.uuid
|
||||
version: ^1.1.0
|
202
vendor/github.com/aokoli/goutils/LICENSE.txt
generated
vendored
Normal file
202
vendor/github.com/aokoli/goutils/LICENSE.txt
generated
vendored
Normal file
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
72
vendor/github.com/aokoli/goutils/README.md
generated
vendored
Normal file
72
vendor/github.com/aokoli/goutils/README.md
generated
vendored
Normal file
@@ -0,0 +1,72 @@
|
||||
GoUtils
|
||||
===========
|
||||
|
||||
GoUtils provides users with utility functions to manipulate strings in various ways. It is a Go implementation of some
|
||||
string manipulation libraries of Java Apache Commons. GoUtils includes the following Java Apache Commons classes:
|
||||
* WordUtils
|
||||
* RandomStringUtils
|
||||
* StringUtils (partial implementation)
|
||||
|
||||
## Installation
|
||||
If you have Go set up on your system, from the GOPATH directory within the command line/terminal, enter this:
|
||||
|
||||
go get github.com/aokoli/goutils
|
||||
|
||||
If you do not have Go set up on your system, please follow the [Go installation directions from the documenation](http://golang.org/doc/install), and then follow the instructions above to install GoUtils.
|
||||
|
||||
|
||||
## Documentation
|
||||
GoUtils doc is available here: [](https://godoc.org/github.com/aokoli/goutils)
|
||||
|
||||
|
||||
## Usage
|
||||
The code snippets below show examples of how to use GoUtils. Some functions return errors while others do not. The first instance below, which does not return an error, is the `Initials` function (located within the `wordutils.go` file).
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/aokoli/goutils"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
// EXAMPLE 1: A goutils function which returns no errors
|
||||
fmt.Println (goutils.Initials("John Doe Foo")) // Prints out "JDF"
|
||||
|
||||
}
|
||||
Some functions return errors mainly due to illegal arguements used as parameters. The code example below illustrates how to deal with function that returns an error. In this instance, the function is the `Random` function (located within the `randomstringutils.go` file).
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/aokoli/goutils"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
// EXAMPLE 2: A goutils function which returns an error
|
||||
rand1, err1 := goutils.Random (-1, 0, 0, true, true)
|
||||
|
||||
if err1 != nil {
|
||||
fmt.Println(err1) // Prints out error message because -1 was entered as the first parameter in goutils.Random(...)
|
||||
} else {
|
||||
fmt.Println(rand1)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
## License
|
||||
GoUtils is licensed under the Apache License, Version 2.0. Please check the LICENSE.txt file or visit http://www.apache.org/licenses/LICENSE-2.0 for a copy of the license.
|
||||
|
||||
## Issue Reporting
|
||||
Make suggestions or report issues using the Git issue tracker: https://github.com/aokoli/goutils/issues
|
||||
|
||||
## Website
|
||||
* [GoUtils webpage](http://aokoli.github.io/goutils/)
|
||||
|
||||
## Mailing List
|
||||
Contact [okolialex@gmail.com](mailto:okolialex@mail.com) to be added to the mailing list. You will get updates on the
|
||||
status of the project and the potential direction it will be heading.
|
||||
|
259
vendor/github.com/aokoli/goutils/randomstringutils.go
generated
vendored
Normal file
259
vendor/github.com/aokoli/goutils/randomstringutils.go
generated
vendored
Normal file
@@ -0,0 +1,259 @@
|
||||
/*
|
||||
Copyright 2014 Alexander Okoli
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package goutils
|
||||
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unicode"
|
||||
"math"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
||||
// Provides the time-based seed used to generate random #s
|
||||
var RANDOM = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
|
||||
/*
|
||||
RandomNonAlphaNumeric creates a random string whose length is the number of characters specified.
|
||||
Characters will be chosen from the set of all characters (ASCII/Unicode values between 0 to 2,147,483,647 (math.MaxInt32)).
|
||||
|
||||
Parameter:
|
||||
count - the length of random string to create
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
|
||||
*/
|
||||
func RandomNonAlphaNumeric (count int) (string, error) {
|
||||
return RandomAlphaNumericCustom(count, false, false)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
RandomAscii creates a random string whose length is the number of characters specified.
|
||||
Characters will be chosen from the set of characters whose ASCII value is between 32 and 126 (inclusive).
|
||||
|
||||
Parameter:
|
||||
count - the length of random string to create
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
|
||||
*/
|
||||
func RandomAscii(count int) (string, error) {
|
||||
return Random(count, 32, 127, false, false)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
RandomNumeric creates a random string whose length is the number of characters specified.
|
||||
Characters will be chosen from the set of numeric characters.
|
||||
|
||||
Parameter:
|
||||
count - the length of random string to create
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
|
||||
*/
|
||||
func RandomNumeric (count int) (string, error) {
|
||||
return Random(count, 0, 0, false, true)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
RandomAlphabetic creates a random string whose length is the number of characters specified.
|
||||
Characters will be chosen from the set of alpha-numeric characters as indicated by the arguments.
|
||||
|
||||
Parameters:
|
||||
count - the length of random string to create
|
||||
letters - if true, generated string may include alphabetic characters
|
||||
numbers - if true, generated string may include numeric characters
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
|
||||
*/
|
||||
func RandomAlphabetic (count int) (string, error) {
|
||||
return Random(count, 0, 0, true, false)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
RandomAlphaNumeric creates a random string whose length is the number of characters specified.
|
||||
Characters will be chosen from the set of alpha-numeric characters.
|
||||
|
||||
Parameter:
|
||||
count - the length of random string to create
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
|
||||
*/
|
||||
func RandomAlphaNumeric (count int) (string, error) {
|
||||
return Random(count, 0, 0, true, true)
|
||||
}
|
||||
|
||||
/*
|
||||
RandomAlphaNumericCustom creates a random string whose length is the number of characters specified.
|
||||
Characters will be chosen from the set of alpha-numeric characters as indicated by the arguments.
|
||||
|
||||
Parameters:
|
||||
count - the length of random string to create
|
||||
letters - if true, generated string may include alphabetic characters
|
||||
numbers - if true, generated string may include numeric characters
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
|
||||
*/
|
||||
func RandomAlphaNumericCustom (count int, letters bool, numbers bool) (string, error) {
|
||||
return Random(count, 0, 0, letters, numbers)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Random creates a random string based on a variety of options, using default source of randomness.
|
||||
This method has exactly the same semantics as RandomSeed(int, int, int, bool, bool, []char, *rand.Rand), but
|
||||
instead of using an externally supplied source of randomness, it uses the internal *rand.Rand instance.
|
||||
|
||||
Parameters:
|
||||
count - the length of random string to create
|
||||
start - the position in set of chars (ASCII/Unicode int) to start at
|
||||
end - the position in set of chars (ASCII/Unicode int) to end before
|
||||
letters - if true, generated string may include alphabetic characters
|
||||
numbers - if true, generated string may include numeric characters
|
||||
chars - the set of chars to choose randoms from. If nil, then it will use the set of all chars.
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from an invalid parameter within underlying function, RandomSeed(...)
|
||||
*/
|
||||
func Random (count int, start int, end int, letters bool, numbers bool, chars ...rune) (string, error) {
|
||||
return RandomSeed (count, start, end, letters, numbers, chars, RANDOM)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
RandomSeed creates a random string based on a variety of options, using supplied source of randomness.
|
||||
If the parameters start and end are both 0, start and end are set to ' ' and 'z', the ASCII printable characters, will be used,
|
||||
unless letters and numbers are both false, in which case, start and end are set to 0 and math.MaxInt32, respectively.
|
||||
If chars is not nil, characters stored in chars that are between start and end are chosen.
|
||||
This method accepts a user-supplied *rand.Rand instance to use as a source of randomness. By seeding a single *rand.Rand instance
|
||||
with a fixed seed and using it for each call, the same random sequence of strings can be generated repeatedly and predictably.
|
||||
|
||||
Parameters:
|
||||
count - the length of random string to create
|
||||
start - the position in set of chars (ASCII/Unicode decimals) to start at
|
||||
end - the position in set of chars (ASCII/Unicode decimals) to end before
|
||||
letters - if true, generated string may include alphabetic characters
|
||||
numbers - if true, generated string may include numeric characters
|
||||
chars - the set of chars to choose randoms from. If nil, then it will use the set of all chars.
|
||||
random - a source of randomness.
|
||||
|
||||
Returns:
|
||||
string - the random string
|
||||
error - an error stemming from invalid parameters: if count < 0; or the provided chars array is empty; or end <= start; or end > len(chars)
|
||||
*/
|
||||
func RandomSeed (count int, start int, end int, letters bool, numbers bool, chars []rune, random *rand.Rand) (string, error) {
|
||||
|
||||
if count == 0 {
|
||||
return "", nil
|
||||
} else if count < 0 {
|
||||
err := fmt.Errorf("randomstringutils illegal argument: Requested random string length %v is less than 0.", count) // equiv to err := errors.New("...")
|
||||
return "", err
|
||||
}
|
||||
if chars != nil && len(chars) == 0 {
|
||||
err := fmt.Errorf("randomstringutils illegal argument: The chars array must not be empty")
|
||||
return "", err
|
||||
}
|
||||
|
||||
if start == 0 && end == 0 {
|
||||
if chars != nil {
|
||||
end = len(chars)
|
||||
} else {
|
||||
if !letters && !numbers {
|
||||
end = math.MaxInt32
|
||||
} else {
|
||||
end = 'z' + 1
|
||||
start = ' '
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if end <= start {
|
||||
err := fmt.Errorf("randomstringutils illegal argument: Parameter end (%v) must be greater than start (%v)", end, start)
|
||||
return "", err
|
||||
}
|
||||
|
||||
if chars != nil && end > len(chars) {
|
||||
err := fmt.Errorf("randomstringutils illegal argument: Parameter end (%v) cannot be greater than len(chars) (%v)", end, len(chars))
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
buffer := make([]rune, count)
|
||||
gap := end - start
|
||||
|
||||
|
||||
// high-surrogates range, (\uD800-\uDBFF) = 55296 - 56319
|
||||
// low-surrogates range, (\uDC00-\uDFFF) = 56320 - 57343
|
||||
|
||||
for count != 0 {
|
||||
count--
|
||||
var ch rune
|
||||
if chars == nil {
|
||||
ch = rune(random.Intn(gap) + start)
|
||||
} else {
|
||||
ch = chars[random.Intn(gap) + start]
|
||||
}
|
||||
|
||||
if letters && unicode.IsLetter(ch) || numbers && unicode.IsDigit(ch) || !letters && !numbers {
|
||||
if ch >= 56320 && ch <= 57343 { // low surrogate range
|
||||
if count == 0 {
|
||||
count++
|
||||
} else {
|
||||
// Insert low surrogate
|
||||
buffer[count] = ch
|
||||
count--
|
||||
// Insert high surrogate
|
||||
buffer[count] = rune(55296 + random.Intn(128))
|
||||
}
|
||||
} else if ch >= 55296 && ch <= 56191 { // High surrogates range (Partial)
|
||||
if count == 0 {
|
||||
count++
|
||||
} else {
|
||||
// Insert low surrogate
|
||||
buffer[count] = rune(56320 + random.Intn(128))
|
||||
count--
|
||||
// Insert high surrogate
|
||||
buffer[count] = ch
|
||||
}
|
||||
} else if ch >= 56192 && ch <= 56319 {
|
||||
// private high surrogate, skip it
|
||||
count++
|
||||
} else {
|
||||
// not one of the surrogates*
|
||||
buffer[count] = ch
|
||||
}
|
||||
} else {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return string(buffer), nil
|
||||
}
|
95
vendor/github.com/aokoli/goutils/randomstringutils_test.go
generated
vendored
Normal file
95
vendor/github.com/aokoli/goutils/randomstringutils_test.go
generated
vendored
Normal file
@@ -0,0 +1,95 @@
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
package goutils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
// ****************************** TESTS ********************************************
|
||||
|
||||
func TestRandomSeed(t *testing.T) {
|
||||
|
||||
|
||||
|
||||
// count, start, end, letters, numbers := 5, 0, 0, true, true
|
||||
random := rand.New(rand.NewSource(10))
|
||||
out := "3ip9v"
|
||||
|
||||
// Test 1: Simulating RandomAlphaNumeric(count int)
|
||||
if x, _ := RandomSeed (5, 0, 0, true, true, nil, random); x != out {
|
||||
t.Errorf("RandomSeed(%v, %v, %v, %v, %v, %v, %v) = %v, want %v", 5, 0, 0, true, true, nil, random, x, out)
|
||||
}
|
||||
|
||||
// Test 2: Simulating RandomAlphabetic(count int)
|
||||
out = "MBrbj"
|
||||
|
||||
if x, _ := RandomSeed (5, 0, 0, true, false, nil, random); x != out {
|
||||
t.Errorf("RandomSeed(%v, %v, %v, %v, %v, %v, %v) = %v, want %v", 5, 0, 0, true, false, nil, random, x, out)
|
||||
}
|
||||
|
||||
|
||||
// Test 3: Simulating RandomNumeric(count int)
|
||||
out = "88935"
|
||||
|
||||
if x, _ := RandomSeed (5, 0, 0, false, true, nil, random); x != out {
|
||||
t.Errorf("RandomSeed(%v, %v, %v, %v, %v, %v, %v) = %v, want %v", 5, 0, 0, false, true, nil, random, x, out)
|
||||
}
|
||||
|
||||
|
||||
// Test 4: Simulating RandomAscii(count int)
|
||||
out = "H_I;E"
|
||||
|
||||
if x, _ := RandomSeed (5, 32, 127, false, false, nil, random); x != out {
|
||||
t.Errorf("RandomSeed(%v, %v, %v, %v, %v, %v, %v) = %v, want %v", 5, 32, 127, false, false, nil, random, x, out)
|
||||
}
|
||||
|
||||
|
||||
// Test 5: Simulating RandomSeed(...) with custom chars
|
||||
chars := []rune {'1','2','3','a','b','c'}
|
||||
out = "2b2ca"
|
||||
|
||||
if x, _ := RandomSeed (5, 0, 0, false, false, chars, random); x != out {
|
||||
t.Errorf("RandomSeed(%v, %v, %v, %v, %v, %v, %v) = %v, want %v", 5, 0, 0, false, false, chars, random, x, out)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// ****************************** EXAMPLES ********************************************
|
||||
|
||||
|
||||
func ExampleRandomSeed() {
|
||||
|
||||
var seed int64 = 10 // If you change this seed #, the random sequence below will change
|
||||
random := rand.New(rand.NewSource(seed))
|
||||
chars := []rune {'1','2','3','a','b','c'}
|
||||
|
||||
rand1, _ := RandomSeed (5, 0, 0, true, true, nil, random) // RandomAlphaNumeric (Alphabets and numbers possible)
|
||||
rand2, _ := RandomSeed (5, 0, 0, true, false, nil, random) // RandomAlphabetic (Only alphabets)
|
||||
rand3, _ := RandomSeed (5, 0, 0, false, true, nil, random) // RandomNumeric (Only numbers)
|
||||
rand4, _ := RandomSeed (5, 32, 127, false, false, nil, random) // RandomAscii (Alphabets, numbers, and other ASCII chars)
|
||||
rand5, _ := RandomSeed (5, 0, 0, true, true, chars, random) // RandomSeed with custom characters
|
||||
|
||||
fmt.Println(rand1)
|
||||
fmt.Println(rand2)
|
||||
fmt.Println(rand3)
|
||||
fmt.Println(rand4)
|
||||
fmt.Println(rand5)
|
||||
// Output:
|
||||
// 3ip9v
|
||||
// MBrbj
|
||||
// 88935
|
||||
// H_I;E
|
||||
// 2b2ca
|
||||
}
|
232
vendor/github.com/aokoli/goutils/stringutils.go
generated
vendored
Normal file
232
vendor/github.com/aokoli/goutils/stringutils.go
generated
vendored
Normal file
@@ -0,0 +1,232 @@
|
||||
/*
|
||||
Copyright 2014 Alexander Okoli
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package goutils
|
||||
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"unicode"
|
||||
"bytes"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Typically returned by functions where a searched item cannot be found
|
||||
const INDEX_NOT_FOUND = -1
|
||||
|
||||
|
||||
/*
|
||||
Abbreviate abbreviates a string using ellipses. This will turn the string "Now is the time for all good men" into "Now is the time for..."
|
||||
|
||||
Specifically, the algorithm is as follows:
|
||||
|
||||
- If str is less than maxWidth characters long, return it.
|
||||
- Else abbreviate it to (str[0:maxWidth - 3] + "...").
|
||||
- If maxWidth is less than 4, return an illegal argument error.
|
||||
- In no case will it return a string of length greater than maxWidth.
|
||||
|
||||
Parameters:
|
||||
str - the string to check
|
||||
maxWidth - maximum length of result string, must be at least 4
|
||||
|
||||
Returns:
|
||||
string - abbreviated string
|
||||
error - if the width is too small
|
||||
*/
|
||||
func Abbreviate (str string, maxWidth int) (string, error) {
|
||||
return AbbreviateFull(str, 0, maxWidth)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
AbbreviateFull abbreviates a string using ellipses. This will turn the string "Now is the time for all good men" into "...is the time for..."
|
||||
This function works like Abbreviate(string, int), but allows you to specify a "left edge" offset. Note that this left edge is not
|
||||
necessarily going to be the leftmost character in the result, or the first character following the ellipses, but it will appear
|
||||
somewhere in the result.
|
||||
In no case will it return a string of length greater than maxWidth.
|
||||
|
||||
Parameters:
|
||||
str - the string to check
|
||||
offset - left edge of source string
|
||||
maxWidth - maximum length of result string, must be at least 4
|
||||
|
||||
Returns:
|
||||
string - abbreviated string
|
||||
error - if the width is too small
|
||||
*/
|
||||
func AbbreviateFull (str string, offset int, maxWidth int) (string, error) {
|
||||
if str == "" {
|
||||
return "", nil
|
||||
}
|
||||
if maxWidth < 4 {
|
||||
err := fmt.Errorf("stringutils illegal argument: Minimum abbreviation width is 4")
|
||||
return "", err
|
||||
}
|
||||
if len(str) <= maxWidth {
|
||||
return str, nil
|
||||
}
|
||||
if offset > len(str) {
|
||||
offset = len(str)
|
||||
}
|
||||
if len(str) - offset < (maxWidth - 3) { // 15 - 5 < 10 - 3 = 10 < 7
|
||||
offset = len(str) - (maxWidth - 3)
|
||||
}
|
||||
abrevMarker := "..."
|
||||
if offset <= 4 {
|
||||
return str[0:maxWidth - 3] + abrevMarker, nil// str.substring(0, maxWidth - 3) + abrevMarker;
|
||||
}
|
||||
if maxWidth < 7 {
|
||||
err := fmt.Errorf("stringutils illegal argument: Minimum abbreviation width with offset is 7")
|
||||
return "", err
|
||||
}
|
||||
if (offset + maxWidth - 3) < len(str) { // 5 + (10-3) < 15 = 12 < 15
|
||||
abrevStr, _ := Abbreviate(str[offset:len(str)], (maxWidth - 3))
|
||||
return abrevMarker + abrevStr, nil// abrevMarker + abbreviate(str.substring(offset), maxWidth - 3);
|
||||
}
|
||||
return abrevMarker + str[(len(str) - (maxWidth - 3)):len(str)], nil // abrevMarker + str.substring(str.length() - (maxWidth - 3));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
DeleteWhiteSpace deletes all whitespaces from a string as defined by unicode.IsSpace(rune).
|
||||
It returns the string without whitespaces.
|
||||
|
||||
Parameter:
|
||||
str - the string to delete whitespace from, may be nil
|
||||
|
||||
Returns:
|
||||
the string without whitespaces
|
||||
*/
|
||||
func DeleteWhiteSpace(str string) string {
|
||||
if str == "" {
|
||||
return str
|
||||
}
|
||||
sz := len(str)
|
||||
var chs bytes.Buffer
|
||||
count := 0
|
||||
for i := 0; i < sz; i++ {
|
||||
ch := rune(str[i])
|
||||
if !unicode.IsSpace(ch) {
|
||||
chs.WriteRune(ch)
|
||||
count++
|
||||
}
|
||||
}
|
||||
if count == sz {
|
||||
return str
|
||||
}
|
||||
return chs.String()
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
IndexOfDifference compares two strings, and returns the index at which the strings begin to differ.
|
||||
|
||||
Parameters:
|
||||
str1 - the first string
|
||||
str2 - the second string
|
||||
|
||||
Returns:
|
||||
the index where str1 and str2 begin to differ; -1 if they are equal
|
||||
*/
|
||||
func IndexOfDifference(str1 string, str2 string) int {
|
||||
if str1 == str2 {
|
||||
return INDEX_NOT_FOUND
|
||||
}
|
||||
if IsEmpty(str1) || IsEmpty(str2) {
|
||||
return 0
|
||||
}
|
||||
var i int;
|
||||
for i = 0; i < len(str1) && i < len(str2); i++ {
|
||||
if rune(str1[i]) != rune(str2[i]) {
|
||||
break
|
||||
}
|
||||
}
|
||||
if i < len(str2) || i < len(str1) {
|
||||
return i
|
||||
}
|
||||
return INDEX_NOT_FOUND
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
IsBlank checks if a string is whitespace or empty (""). Observe the following behavior:
|
||||
|
||||
goutils.IsBlank("") = true
|
||||
goutils.IsBlank(" ") = true
|
||||
goutils.IsBlank("bob") = false
|
||||
goutils.IsBlank(" bob ") = false
|
||||
|
||||
Parameter:
|
||||
str - the string to check
|
||||
|
||||
Returns:
|
||||
true - if the string is whitespace or empty ("")
|
||||
*/
|
||||
func IsBlank(str string) bool {
|
||||
strLen := len(str)
|
||||
if str == "" || strLen == 0 {
|
||||
return true
|
||||
}
|
||||
for i := 0; i < strLen; i++ {
|
||||
if unicode.IsSpace(rune(str[i])) == false {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
IndexOf returns the index of the first instance of sub in str, with the search beginning from the
|
||||
index start point specified. -1 is returned if sub is not present in str.
|
||||
|
||||
An empty string ("") will return -1 (INDEX_NOT_FOUND). A negative start position is treated as zero.
|
||||
A start position greater than the string length returns -1.
|
||||
|
||||
Parameters:
|
||||
str - the string to check
|
||||
sub - the substring to find
|
||||
start - the start position; negative treated as zero
|
||||
|
||||
Returns:
|
||||
the first index where the sub string was found (always >= start)
|
||||
*/
|
||||
func IndexOf(str string, sub string, start int) int {
|
||||
|
||||
if (start < 0) {
|
||||
start = 0
|
||||
}
|
||||
|
||||
if len(str) < start {
|
||||
return INDEX_NOT_FOUND
|
||||
}
|
||||
|
||||
if IsEmpty(str) || IsEmpty(sub) {
|
||||
return INDEX_NOT_FOUND
|
||||
}
|
||||
|
||||
partialIndex := strings.Index(str[start:len(str)], sub)
|
||||
if partialIndex == -1 {
|
||||
return INDEX_NOT_FOUND
|
||||
}
|
||||
return partialIndex + start
|
||||
}
|
||||
|
||||
// IsEmpty checks if a string is empty (""). Returns true if empty, and false otherwise.
|
||||
func IsEmpty(str string) bool {
|
||||
return len(str) == 0
|
||||
}
|
329
vendor/github.com/aokoli/goutils/stringutils_test.go
generated
vendored
Normal file
329
vendor/github.com/aokoli/goutils/stringutils_test.go
generated
vendored
Normal file
@@ -0,0 +1,329 @@
|
||||
package goutils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// ****************************** TESTS ********************************************
|
||||
|
||||
func TestAbbreviate(t *testing.T) {
|
||||
|
||||
// Test 1
|
||||
in := "abcdefg"
|
||||
out := "abc..."
|
||||
maxWidth := 6
|
||||
|
||||
if x, _ := Abbreviate(in, maxWidth); x != out {
|
||||
t.Errorf("Abbreviate(%v, %v) = %v, want %v", in, maxWidth, x, out)
|
||||
}
|
||||
|
||||
// Test 2
|
||||
out = "abcdefg"
|
||||
maxWidth = 7
|
||||
|
||||
if x, _ := Abbreviate(in, maxWidth); x != out {
|
||||
t.Errorf("Abbreviate(%v, %v) = %v, want %v", in, maxWidth, x, out)
|
||||
}
|
||||
|
||||
|
||||
// Test 3
|
||||
out = "a..."
|
||||
maxWidth = 4
|
||||
|
||||
if x, _ := Abbreviate(in, maxWidth); x != out {
|
||||
t.Errorf("Abbreviate(%v, %v) = %v, want %v", in, maxWidth, x, out)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestAbbreviateFull(t *testing.T) {
|
||||
|
||||
// Test 1
|
||||
in := "abcdefghijklmno"
|
||||
out := "abcdefg..."
|
||||
offset := -1
|
||||
maxWidth := 10
|
||||
|
||||
if x, _ := AbbreviateFull(in, offset, maxWidth); x != out {
|
||||
t.Errorf("AbbreviateFull(%v, %v, %v) = %v, want %v", in, offset, maxWidth, x, out)
|
||||
}
|
||||
|
||||
// Test 2
|
||||
out = "...fghi..."
|
||||
offset = 5
|
||||
maxWidth = 10
|
||||
|
||||
if x, _ := AbbreviateFull(in, offset, maxWidth); x != out {
|
||||
t.Errorf("AbbreviateFull(%v, %v, %v) = %v, want %v", in, offset, maxWidth, x, out)
|
||||
}
|
||||
|
||||
|
||||
// Test 3
|
||||
out = "...ijklmno"
|
||||
offset = 12
|
||||
maxWidth = 10
|
||||
|
||||
|
||||
if x, _ := AbbreviateFull(in, offset, maxWidth); x != out {
|
||||
t.Errorf("AbbreviateFull(%v, %v, %v) = %v, want %v", in, offset, maxWidth, x, out)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func TestIndexOf(t *testing.T) {
|
||||
|
||||
// Test 1
|
||||
str := "abcafgka"
|
||||
sub := "a"
|
||||
start := 0
|
||||
out := 0
|
||||
|
||||
if x := IndexOf(str, sub, start); x != out {
|
||||
t.Errorf("IndexOf(%v, %v, %v) = %v, want %v", str, sub, start, x, out)
|
||||
}
|
||||
|
||||
// Test 2
|
||||
start = 1
|
||||
out = 3
|
||||
|
||||
if x := IndexOf(str, sub, start); x != out {
|
||||
t.Errorf("IndexOf(%v, %v, %v) = %v, want %v", str, sub, start, x, out)
|
||||
}
|
||||
|
||||
// Test 3
|
||||
start = 4
|
||||
out = 7
|
||||
|
||||
if x := IndexOf(str, sub, start); x != out {
|
||||
t.Errorf("IndexOf(%v, %v, %v) = %v, want %v", str, sub, start, x, out)
|
||||
}
|
||||
|
||||
|
||||
// Test 4
|
||||
sub = "z"
|
||||
out = -1
|
||||
|
||||
if x := IndexOf(str, sub, start); x != out {
|
||||
t.Errorf("IndexOf(%v, %v, %v) = %v, want %v", str, sub, start, x, out)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
func TestIsBlank(t *testing.T) {
|
||||
|
||||
// Test 1
|
||||
str := ""
|
||||
out := true
|
||||
|
||||
if x := IsBlank(str); x != out {
|
||||
t.Errorf("IndexOf(%v) = %v, want %v", str, x, out)
|
||||
}
|
||||
|
||||
// Test 2
|
||||
str = " "
|
||||
out = true
|
||||
|
||||
if x := IsBlank(str); x != out {
|
||||
t.Errorf("IndexOf(%v) = %v, want %v", str, x, out)
|
||||
}
|
||||
|
||||
// Test 3
|
||||
str = " abc "
|
||||
out = false
|
||||
|
||||
if x := IsBlank(str); x != out {
|
||||
t.Errorf("IndexOf(%v) = %v, want %v", str, x, out)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func TestDeleteWhiteSpace(t *testing.T) {
|
||||
|
||||
// Test 1
|
||||
str := " a b c "
|
||||
out := "abc"
|
||||
|
||||
if x := DeleteWhiteSpace(str); x != out {
|
||||
t.Errorf("IndexOf(%v) = %v, want %v", str, x, out)
|
||||
}
|
||||
|
||||
// Test 2
|
||||
str = " "
|
||||
out = ""
|
||||
|
||||
if x := DeleteWhiteSpace(str); x != out {
|
||||
t.Errorf("IndexOf(%v) = %v, want %v", str, x, out)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestIndexOfDifference(t *testing.T) {
|
||||
|
||||
str1 := "abc"
|
||||
str2 := "a_c"
|
||||
out := 1
|
||||
|
||||
if x := IndexOfDifference(str1, str2); x != out {
|
||||
t.Errorf("IndexOfDifference(%v, %v) = %v, want %v", str1, str2, x, out)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ****************************** EXAMPLES ********************************************
|
||||
|
||||
func ExampleAbbreviate() {
|
||||
|
||||
str := "abcdefg"
|
||||
out1, _ := Abbreviate(str, 6)
|
||||
out2, _ := Abbreviate(str, 7)
|
||||
out3, _ := Abbreviate(str, 8)
|
||||
out4, _ := Abbreviate(str, 4)
|
||||
_, err1 := Abbreviate(str, 3)
|
||||
|
||||
|
||||
fmt.Println(out1)
|
||||
fmt.Println(out2)
|
||||
fmt.Println(out3)
|
||||
fmt.Println(out4)
|
||||
fmt.Println(err1)
|
||||
// Output:
|
||||
// abc...
|
||||
// abcdefg
|
||||
// abcdefg
|
||||
// a...
|
||||
// stringutils illegal argument: Minimum abbreviation width is 4
|
||||
}
|
||||
|
||||
|
||||
func ExampleAbbreviateFull() {
|
||||
|
||||
str := "abcdefghijklmno"
|
||||
str2 := "abcdefghij"
|
||||
out1, _ := AbbreviateFull(str, -1, 10)
|
||||
out2, _ := AbbreviateFull(str, 0, 10)
|
||||
out3, _ := AbbreviateFull(str, 1, 10)
|
||||
out4, _ := AbbreviateFull(str, 4, 10)
|
||||
out5, _ := AbbreviateFull(str, 5, 10)
|
||||
out6, _ := AbbreviateFull(str, 6, 10)
|
||||
out7, _ := AbbreviateFull(str, 8, 10)
|
||||
out8, _ := AbbreviateFull(str, 10, 10)
|
||||
out9, _ := AbbreviateFull(str, 12, 10)
|
||||
_, err1 := AbbreviateFull(str2, 0, 3)
|
||||
_, err2 := AbbreviateFull(str2, 5, 6)
|
||||
|
||||
|
||||
fmt.Println(out1)
|
||||
fmt.Println(out2)
|
||||
fmt.Println(out3)
|
||||
fmt.Println(out4)
|
||||
fmt.Println(out5)
|
||||
fmt.Println(out6)
|
||||
fmt.Println(out7)
|
||||
fmt.Println(out8)
|
||||
fmt.Println(out9)
|
||||
fmt.Println(err1)
|
||||
fmt.Println(err2)
|
||||
// Output:
|
||||
// abcdefg...
|
||||
// abcdefg...
|
||||
// abcdefg...
|
||||
// abcdefg...
|
||||
// ...fghi...
|
||||
// ...ghij...
|
||||
// ...ijklmno
|
||||
// ...ijklmno
|
||||
// ...ijklmno
|
||||
// stringutils illegal argument: Minimum abbreviation width is 4
|
||||
// stringutils illegal argument: Minimum abbreviation width with offset is 7
|
||||
}
|
||||
|
||||
|
||||
func ExampleIsBlank() {
|
||||
|
||||
out1 := IsBlank("")
|
||||
out2 := IsBlank(" ")
|
||||
out3 := IsBlank("bob")
|
||||
out4 := IsBlank(" bob ")
|
||||
|
||||
fmt.Println(out1)
|
||||
fmt.Println(out2)
|
||||
fmt.Println(out3)
|
||||
fmt.Println(out4)
|
||||
// Output:
|
||||
// true
|
||||
// true
|
||||
// false
|
||||
// false
|
||||
}
|
||||
|
||||
|
||||
func ExampleDeleteWhiteSpace() {
|
||||
|
||||
out1 := DeleteWhiteSpace(" ")
|
||||
out2 := DeleteWhiteSpace("bob")
|
||||
out3 := DeleteWhiteSpace("bob ")
|
||||
out4 := DeleteWhiteSpace(" b o b ")
|
||||
|
||||
fmt.Println(out1)
|
||||
fmt.Println(out2)
|
||||
fmt.Println(out3)
|
||||
fmt.Println(out4)
|
||||
// Output:
|
||||
//
|
||||
// bob
|
||||
// bob
|
||||
// bob
|
||||
}
|
||||
|
||||
|
||||
func ExampleIndexOf() {
|
||||
|
||||
str := "abcdefgehije"
|
||||
out1 := IndexOf(str, "e", 0)
|
||||
out2 := IndexOf(str, "e", 5)
|
||||
out3 := IndexOf(str, "e", 8)
|
||||
out4 := IndexOf(str, "eh", 0)
|
||||
out5 := IndexOf(str, "eh", 22)
|
||||
out6 := IndexOf(str, "z", 0)
|
||||
out7 := IndexOf(str, "", 0)
|
||||
|
||||
fmt.Println(out1)
|
||||
fmt.Println(out2)
|
||||
fmt.Println(out3)
|
||||
fmt.Println(out4)
|
||||
fmt.Println(out5)
|
||||
fmt.Println(out6)
|
||||
fmt.Println(out7)
|
||||
// Output:
|
||||
// 4
|
||||
// 7
|
||||
// 11
|
||||
// 7
|
||||
// -1
|
||||
// -1
|
||||
// -1
|
||||
}
|
||||
|
||||
|
||||
|
||||
func ExampleIndexOfDifference() {
|
||||
|
||||
out1 := IndexOfDifference("abc", "abc")
|
||||
out2 := IndexOfDifference("ab", "abxyz")
|
||||
out3 := IndexOfDifference("", "abc")
|
||||
out4 := IndexOfDifference("abcde", "abxyz")
|
||||
|
||||
fmt.Println(out1)
|
||||
fmt.Println(out2)
|
||||
fmt.Println(out3)
|
||||
fmt.Println(out4)
|
||||
// Output:
|
||||
// -1
|
||||
// 2
|
||||
// 0
|
||||
// 2
|
||||
}
|
365
vendor/github.com/aokoli/goutils/wordutils.go
generated
vendored
Normal file
365
vendor/github.com/aokoli/goutils/wordutils.go
generated
vendored
Normal file
@@ -0,0 +1,365 @@
|
||||
/*
|
||||
Copyright 2014 Alexander Okoli
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
/*
|
||||
Package goutils provides utility functions to manipulate strings in various ways.
|
||||
The code snippets below show examples of how to use goutils. Some functions return
|
||||
errors while others do not, so usage would vary as a result.
|
||||
|
||||
Example:
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/aokoli/goutils"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
// EXAMPLE 1: A goutils function which returns no errors
|
||||
fmt.Println (goutils.Initials("John Doe Foo")) // Prints out "JDF"
|
||||
|
||||
|
||||
|
||||
// EXAMPLE 2: A goutils function which returns an error
|
||||
rand1, err1 := goutils.Random (-1, 0, 0, true, true)
|
||||
|
||||
if err1 != nil {
|
||||
fmt.Println(err1) // Prints out error message because -1 was entered as the first parameter in goutils.Random(...)
|
||||
} else {
|
||||
fmt.Println(rand1)
|
||||
}
|
||||
}
|
||||
*/
|
||||
package goutils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// VERSION indicates the current version of goutils
|
||||
const VERSION = "1.0.0"
|
||||
|
||||
/*
|
||||
Wrap wraps a single line of text, identifying words by ' '.
|
||||
New lines will be separated by '\n'. Very long words, such as URLs will not be wrapped.
|
||||
Leading spaces on a new line are stripped. Trailing spaces are not stripped.
|
||||
|
||||
Parameters:
|
||||
str - the string to be word wrapped
|
||||
wrapLength - the column (a column can fit only one character) to wrap the words at, less than 1 is treated as 1
|
||||
|
||||
Returns:
|
||||
a line with newlines inserted
|
||||
*/
|
||||
func Wrap (str string, wrapLength int) string {
|
||||
return WrapCustom (str, wrapLength, "", false)
|
||||
}
|
||||
|
||||
/*
|
||||
WrapCustom wraps a single line of text, identifying words by ' '.
|
||||
Leading spaces on a new line are stripped. Trailing spaces are not stripped.
|
||||
|
||||
Parameters:
|
||||
str - the string to be word wrapped
|
||||
wrapLength - the column number (a column can fit only one character) to wrap the words at, less than 1 is treated as 1
|
||||
newLineStr - the string to insert for a new line, "" uses '\n'
|
||||
wrapLongWords - true if long words (such as URLs) should be wrapped
|
||||
|
||||
Returns:
|
||||
a line with newlines inserted
|
||||
*/
|
||||
func WrapCustom (str string, wrapLength int, newLineStr string, wrapLongWords bool) string {
|
||||
|
||||
if str == "" {
|
||||
return ""
|
||||
}
|
||||
if newLineStr == "" {
|
||||
newLineStr = "\n" // TODO Assumes "\n" is seperator. Explore SystemUtils.LINE_SEPARATOR from Apache Commons
|
||||
}
|
||||
if wrapLength < 1 {
|
||||
wrapLength = 1
|
||||
}
|
||||
|
||||
inputLineLength := len(str)
|
||||
offset := 0
|
||||
|
||||
var wrappedLine bytes.Buffer
|
||||
|
||||
for inputLineLength-offset > wrapLength {
|
||||
|
||||
if rune(str[offset]) == ' ' {
|
||||
offset++
|
||||
continue
|
||||
}
|
||||
|
||||
end := wrapLength + offset + 1
|
||||
spaceToWrapAt := strings.LastIndex(str[offset:end], " ") + offset
|
||||
|
||||
if spaceToWrapAt >= offset {
|
||||
// normal word (not longer than wrapLength)
|
||||
wrappedLine.WriteString(str[offset:spaceToWrapAt])
|
||||
wrappedLine.WriteString(newLineStr)
|
||||
offset = spaceToWrapAt + 1
|
||||
|
||||
} else {
|
||||
// long word or URL
|
||||
if wrapLongWords {
|
||||
end := wrapLength + offset
|
||||
// long words are wrapped one line at a time
|
||||
wrappedLine.WriteString(str[offset:end])
|
||||
wrappedLine.WriteString(newLineStr)
|
||||
offset += wrapLength
|
||||
} else {
|
||||
// long words aren't wrapped, just extended beyond limit
|
||||
end := wrapLength + offset
|
||||
spaceToWrapAt = strings.IndexRune(str[end:len(str)], ' ') + end
|
||||
if spaceToWrapAt >= 0 {
|
||||
wrappedLine.WriteString(str[offset:spaceToWrapAt])
|
||||
wrappedLine.WriteString(newLineStr)
|
||||
offset = spaceToWrapAt + 1
|
||||
} else {
|
||||
wrappedLine.WriteString(str[offset:len(str)])
|
||||
offset = inputLineLength
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
wrappedLine.WriteString(str[offset:len(str)])
|
||||
|
||||
return wrappedLine.String()
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Capitalize capitalizes all the delimiter separated words in a string. Only the first letter of each word is changed.
|
||||
To convert the rest of each word to lowercase at the same time, use CapitalizeFully(str string, delimiters ...rune).
|
||||
The delimiters represent a set of characters understood to separate words. The first string character
|
||||
and the first non-delimiter character after a delimiter will be capitalized. A "" input string returns "".
|
||||
Capitalization uses the Unicode title case, normally equivalent to upper case.
|
||||
|
||||
Parameters:
|
||||
str - the string to capitalize
|
||||
delimiters - set of characters to determine capitalization, exclusion of this parameter means whitespace would be delimeter
|
||||
|
||||
Returns:
|
||||
capitalized string
|
||||
*/
|
||||
func Capitalize (str string, delimiters ...rune) string {
|
||||
|
||||
var delimLen int
|
||||
|
||||
if delimiters == nil {
|
||||
delimLen = -1
|
||||
} else {
|
||||
delimLen = len(delimiters)
|
||||
}
|
||||
|
||||
if str == "" || delimLen == 0 {
|
||||
return str;
|
||||
}
|
||||
|
||||
buffer := []rune(str)
|
||||
capitalizeNext := true
|
||||
for i := 0; i < len(buffer); i++ {
|
||||
ch := buffer[i]
|
||||
if isDelimiter(ch, delimiters...) {
|
||||
capitalizeNext = true
|
||||
} else if capitalizeNext {
|
||||
buffer[i] = unicode.ToTitle(ch)
|
||||
capitalizeNext = false
|
||||
}
|
||||
}
|
||||
return string(buffer)
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
CapitalizeFully converts all the delimiter separated words in a string into capitalized words, that is each word is made up of a
|
||||
titlecase character and then a series of lowercase characters. The delimiters represent a set of characters understood
|
||||
to separate words. The first string character and the first non-delimiter character after a delimiter will be capitalized.
|
||||
Capitalization uses the Unicode title case, normally equivalent to upper case.
|
||||
|
||||
Parameters:
|
||||
str - the string to capitalize fully
|
||||
delimiters - set of characters to determine capitalization, exclusion of this parameter means whitespace would be delimeter
|
||||
|
||||
Returns:
|
||||
capitalized string
|
||||
*/
|
||||
func CapitalizeFully (str string, delimiters ...rune) string {
|
||||
|
||||
var delimLen int
|
||||
|
||||
if delimiters == nil {
|
||||
delimLen = -1
|
||||
} else {
|
||||
delimLen = len(delimiters)
|
||||
}
|
||||
|
||||
|
||||
if str == "" || delimLen == 0 {
|
||||
return str;
|
||||
}
|
||||
str = strings.ToLower(str)
|
||||
return Capitalize(str, delimiters...);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Uncapitalize uncapitalizes all the whitespace separated words in a string. Only the first letter of each word is changed.
|
||||
The delimiters represent a set of characters understood to separate words. The first string character and the first non-delimiter
|
||||
character after a delimiter will be uncapitalized. Whitespace is defined by unicode.IsSpace(char).
|
||||
|
||||
Parameters:
|
||||
str - the string to uncapitalize fully
|
||||
delimiters - set of characters to determine capitalization, exclusion of this parameter means whitespace would be delimeter
|
||||
|
||||
Returns:
|
||||
uncapitalized string
|
||||
*/
|
||||
func Uncapitalize (str string, delimiters ...rune) string {
|
||||
|
||||
var delimLen int
|
||||
|
||||
if delimiters == nil {
|
||||
delimLen = -1
|
||||
} else {
|
||||
delimLen = len(delimiters)
|
||||
}
|
||||
|
||||
if str == "" || delimLen == 0 {
|
||||
return str;
|
||||
}
|
||||
|
||||
buffer := []rune(str)
|
||||
uncapitalizeNext := true // TODO Always makes capitalize/un apply to first char.
|
||||
for i := 0; i < len(buffer); i++ {
|
||||
ch := buffer[i]
|
||||
if isDelimiter(ch, delimiters...) {
|
||||
uncapitalizeNext = true
|
||||
} else if uncapitalizeNext {
|
||||
buffer[i] = unicode.ToLower(ch)
|
||||
uncapitalizeNext = false
|
||||
}
|
||||
}
|
||||
return string(buffer)
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
SwapCase swaps the case of a string using a word based algorithm.
|
||||
|
||||
Conversion algorithm:
|
||||
|
||||
Upper case character converts to Lower case
|
||||
Title case character converts to Lower case
|
||||
Lower case character after Whitespace or at start converts to Title case
|
||||
Other Lower case character converts to Upper case
|
||||
Whitespace is defined by unicode.IsSpace(char).
|
||||
|
||||
Parameters:
|
||||
str - the string to swap case
|
||||
|
||||
Returns:
|
||||
the changed string
|
||||
*/
|
||||
func SwapCase(str string) string {
|
||||
if str == "" {
|
||||
return str
|
||||
}
|
||||
buffer := []rune(str)
|
||||
|
||||
whitespace := true
|
||||
|
||||
for i := 0; i < len(buffer); i++ {
|
||||
ch := buffer[i]
|
||||
if unicode.IsUpper(ch) {
|
||||
buffer[i] = unicode.ToLower(ch)
|
||||
whitespace = false
|
||||
} else if unicode.IsTitle(ch) {
|
||||
buffer[i] = unicode.ToLower(ch)
|
||||
whitespace = false
|
||||
} else if unicode.IsLower(ch) {
|
||||
if whitespace {
|
||||
buffer[i] = unicode.ToTitle(ch)
|
||||
whitespace = false
|
||||
} else {
|
||||
buffer[i] = unicode.ToUpper(ch)
|
||||
}
|
||||
} else {
|
||||
whitespace = unicode.IsSpace(ch)
|
||||
}
|
||||
}
|
||||
return string(buffer);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
Initials extracts the initial letters from each word in the string. The first letter of the string and all first
|
||||
letters after the defined delimiters are returned as a new string. Their case is not changed. If the delimiters
|
||||
parameter is excluded, then Whitespace is used. Whitespace is defined by unicode.IsSpacea(char). An empty delimiter array returns an empty string.
|
||||
|
||||
Parameters:
|
||||
str - the string to get initials from
|
||||
delimiters - set of characters to determine words, exclusion of this parameter means whitespace would be delimeter
|
||||
Returns:
|
||||
string of initial letters
|
||||
*/
|
||||
func Initials(str string, delimiters ...rune) string {
|
||||
if str == "" {
|
||||
return str
|
||||
}
|
||||
if delimiters != nil && len(delimiters) == 0 {
|
||||
return ""
|
||||
}
|
||||
strLen := len(str)
|
||||
var buf bytes.Buffer
|
||||
lastWasGap := true
|
||||
for i := 0; i < strLen; i++ {
|
||||
ch := rune(str[i])
|
||||
|
||||
if isDelimiter(ch, delimiters...) {
|
||||
lastWasGap = true
|
||||
} else if lastWasGap {
|
||||
buf.WriteRune(ch)
|
||||
lastWasGap = false
|
||||
}
|
||||
}
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
|
||||
|
||||
// private function (lower case func name)
|
||||
func isDelimiter(ch rune, delimiters ...rune) bool {
|
||||
if delimiters == nil {
|
||||
return unicode.IsSpace(ch)
|
||||
}
|
||||
for _, delimiter := range delimiters {
|
||||
if ch == delimiter {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
252
vendor/github.com/aokoli/goutils/wordutils_test.go
generated
vendored
Normal file
252
vendor/github.com/aokoli/goutils/wordutils_test.go
generated
vendored
Normal file
@@ -0,0 +1,252 @@
|
||||
package goutils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// ****************************** TESTS ********************************************
|
||||
|
||||
func TestWrapNormalWord(t *testing.T) {
|
||||
|
||||
in := "Bob Manuel Bob Manuel"
|
||||
out := "Bob Manuel\nBob Manuel"
|
||||
wrapLength := 10
|
||||
|
||||
if x := Wrap(in, wrapLength); x != out {
|
||||
t.Errorf("Wrap(%v) = %v, want %v", in, x, out)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestWrapCustomLongWordFalse(t *testing.T) {
|
||||
|
||||
in := "BobManuelBob Bob"
|
||||
out := "BobManuelBob<br\\>Bob"
|
||||
wrapLength := 10
|
||||
newLineStr := "<br\\>"
|
||||
wrapLongWords := false
|
||||
|
||||
if x := WrapCustom(in, wrapLength, newLineStr, wrapLongWords); x != out {
|
||||
t.Errorf("Wrap(%v) = %v, want %v", in, x, out)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestWrapCustomLongWordTrue(t *testing.T) {
|
||||
|
||||
in := "BobManuelBob Bob"
|
||||
out := "BobManuelB<br\\>ob Bob"
|
||||
wrapLength := 10
|
||||
newLineStr := "<br\\>"
|
||||
wrapLongWords := true
|
||||
|
||||
if x := WrapCustom(in, wrapLength, newLineStr, wrapLongWords); x != out {
|
||||
t.Errorf("WrapCustom(%v) = %v, want %v", in, x, out)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestCapitalize(t *testing.T) {
|
||||
|
||||
|
||||
// Test 1: Checks if function works with 1 parameter, and default whitespace delimiter
|
||||
in := "test is going.well.thank.you.for inquiring"
|
||||
out := "Test Is Going.well.thank.you.for Inquiring"
|
||||
|
||||
if x := Capitalize(in); x != out {
|
||||
t.Errorf("Capitalize(%v) = %v, want %v", in, x, out)
|
||||
}
|
||||
|
||||
|
||||
// Test 2: Checks if function works with both parameters, with param 2 containing whitespace and '.'
|
||||
out = "Test Is Going.Well.Thank.You.For Inquiring"
|
||||
delimiters := []rune{' ', '.'}
|
||||
|
||||
if x := Capitalize(in, delimiters...); x != out {
|
||||
t.Errorf("Capitalize(%v) = %v, want %v", in, x, out)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
func TestCapitalizeFully(t *testing.T) {
|
||||
|
||||
// Test 1
|
||||
in := "tEsT iS goiNG.wELL.tHaNk.yOU.for inqUIrING"
|
||||
out := "Test Is Going.well.thank.you.for Inquiring"
|
||||
|
||||
if x := CapitalizeFully(in); x != out {
|
||||
t.Errorf("CapitalizeFully(%v) = %v, want %v", in, x, out)
|
||||
}
|
||||
|
||||
|
||||
// Test 2
|
||||
out = "Test Is Going.Well.Thank.You.For Inquiring"
|
||||
delimiters := []rune{' ', '.'}
|
||||
|
||||
if x := CapitalizeFully(in, delimiters...); x != out {
|
||||
t.Errorf("CapitalizeFully(%v) = %v, want %v", in, x, out)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestUncapitalize(t *testing.T) {
|
||||
|
||||
// Test 1: Checks if function works with 1 parameter, and default whitespace delimiter
|
||||
in := "This Is A.Test"
|
||||
out := "this is a.Test"
|
||||
|
||||
if x := Uncapitalize(in); x != out {
|
||||
t.Errorf("Uncapitalize(%v) = %v, want %v", in, x, out)
|
||||
}
|
||||
|
||||
// Test 2: Checks if function works with both parameters, with param 2 containing whitespace and '.'
|
||||
out = "this is a.test"
|
||||
delimiters := []rune{' ', '.'}
|
||||
|
||||
if x := Uncapitalize(in, delimiters...); x != out {
|
||||
t.Errorf("Uncapitalize(%v) = %v, want %v", in, x, out)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestSwapCase(t *testing.T) {
|
||||
|
||||
in := "This Is A.Test"
|
||||
out := "tHIS iS a.tEST"
|
||||
|
||||
if x := SwapCase(in); x != out {
|
||||
t.Errorf("SwapCase(%v) = %v, want %v", in, x, out)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func TestInitials(t *testing.T) {
|
||||
|
||||
// Test 1
|
||||
in := "John Doe.Ray"
|
||||
out := "JD"
|
||||
|
||||
if x := Initials(in); x != out {
|
||||
t.Errorf("Initials(%v) = %v, want %v", in, x, out)
|
||||
}
|
||||
|
||||
|
||||
// Test 2
|
||||
out = "JDR"
|
||||
delimiters := []rune{' ','.'}
|
||||
|
||||
if x := Initials(in, delimiters...); x != out {
|
||||
t.Errorf("Initials(%v) = %v, want %v", in, x, out)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// ****************************** EXAMPLES ********************************************
|
||||
|
||||
func ExampleWrap() {
|
||||
|
||||
in := "Bob Manuel Bob Manuel"
|
||||
wrapLength := 10
|
||||
|
||||
fmt.Println (Wrap(in, wrapLength))
|
||||
// Output:
|
||||
// Bob Manuel
|
||||
// Bob Manuel
|
||||
}
|
||||
|
||||
|
||||
func ExampleWrapCustom_1() {
|
||||
|
||||
in := "BobManuelBob Bob"
|
||||
wrapLength := 10
|
||||
newLineStr := "<br\\>"
|
||||
wrapLongWords := false
|
||||
|
||||
fmt.Println (WrapCustom(in, wrapLength, newLineStr, wrapLongWords))
|
||||
// Output:
|
||||
// BobManuelBob<br\>Bob
|
||||
}
|
||||
|
||||
|
||||
func ExampleWrapCustom_2() {
|
||||
|
||||
in := "BobManuelBob Bob"
|
||||
wrapLength := 10
|
||||
newLineStr := "<br\\>"
|
||||
wrapLongWords := true
|
||||
|
||||
fmt.Println (WrapCustom(in, wrapLength, newLineStr, wrapLongWords))
|
||||
// Output:
|
||||
// BobManuelB<br\>ob Bob
|
||||
}
|
||||
|
||||
|
||||
|
||||
func ExampleCapitalize() {
|
||||
|
||||
in := "test is going.well.thank.you.for inquiring" // Compare input to CapitalizeFully example
|
||||
delimiters := []rune{' ', '.'}
|
||||
|
||||
fmt.Println (Capitalize(in))
|
||||
fmt.Println (Capitalize(in, delimiters...))
|
||||
// Output:
|
||||
// Test Is Going.well.thank.you.for Inquiring
|
||||
// Test Is Going.Well.Thank.You.For Inquiring
|
||||
}
|
||||
|
||||
|
||||
|
||||
func ExampleCapitalizeFully() {
|
||||
|
||||
|
||||
in := "tEsT iS goiNG.wELL.tHaNk.yOU.for inqUIrING" // Notice scattered capitalization
|
||||
delimiters := []rune{' ', '.'}
|
||||
|
||||
fmt.Println (CapitalizeFully(in))
|
||||
fmt.Println (CapitalizeFully(in, delimiters...))
|
||||
// Output:
|
||||
// Test Is Going.well.thank.you.for Inquiring
|
||||
// Test Is Going.Well.Thank.You.For Inquiring
|
||||
}
|
||||
|
||||
|
||||
func ExampleUncapitalize() {
|
||||
|
||||
in := "This Is A.Test"
|
||||
delimiters := []rune{' ', '.'}
|
||||
|
||||
fmt.Println (Uncapitalize(in))
|
||||
fmt.Println (Uncapitalize(in, delimiters...))
|
||||
// Output:
|
||||
// this is a.Test
|
||||
// this is a.test
|
||||
}
|
||||
|
||||
|
||||
func ExampleSwapCase() {
|
||||
|
||||
in := "This Is A.Test"
|
||||
fmt.Println (SwapCase(in))
|
||||
// Output:
|
||||
// tHIS iS a.tEST
|
||||
}
|
||||
|
||||
|
||||
func ExampleInitials() {
|
||||
|
||||
in := "John Doe.Ray"
|
||||
delimiters := []rune{' ','.'}
|
||||
|
||||
fmt.Println (Initials(in))
|
||||
fmt.Println (Initials(in, delimiters...))
|
||||
// Output:
|
||||
// JD
|
||||
// JDR
|
||||
}
|
||||
|
16
vendor/github.com/golang/protobuf/.gitignore
generated
vendored
Normal file
16
vendor/github.com/golang/protobuf/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
.DS_Store
|
||||
*.[568ao]
|
||||
*.ao
|
||||
*.so
|
||||
*.pyc
|
||||
._*
|
||||
.nfs.*
|
||||
[568a].out
|
||||
*~
|
||||
*.orig
|
||||
core
|
||||
_obj
|
||||
_test
|
||||
_testmain.go
|
||||
protoc-gen-go/testdata/multi/*.pb.go
|
||||
_conformance/_conformance
|
3
vendor/github.com/golang/protobuf/AUTHORS
generated
vendored
Normal file
3
vendor/github.com/golang/protobuf/AUTHORS
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
# This source code refers to The Go Authors for copyright purposes.
|
||||
# The master list of authors is in the main Go distribution,
|
||||
# visible at http://tip.golang.org/AUTHORS.
|
3
vendor/github.com/golang/protobuf/CONTRIBUTORS
generated
vendored
Normal file
3
vendor/github.com/golang/protobuf/CONTRIBUTORS
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
# This source code was written by the Go contributors.
|
||||
# The master list of contributors is in the main Go distribution,
|
||||
# visible at http://tip.golang.org/CONTRIBUTORS.
|
31
vendor/github.com/golang/protobuf/LICENSE
generated
vendored
Normal file
31
vendor/github.com/golang/protobuf/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
Go support for Protocol Buffers - Google's data interchange format
|
||||
|
||||
Copyright 2010 The Go Authors. All rights reserved.
|
||||
https://github.com/golang/protobuf
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
* Neither the name of Google Inc. nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
40
vendor/github.com/golang/protobuf/Make.protobuf
generated
vendored
Normal file
40
vendor/github.com/golang/protobuf/Make.protobuf
generated
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
# Go support for Protocol Buffers - Google's data interchange format
|
||||
#
|
||||
# Copyright 2010 The Go Authors. All rights reserved.
|
||||
# https://github.com/golang/protobuf
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are
|
||||
# met:
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# * Redistributions in binary form must reproduce the above
|
||||
# copyright notice, this list of conditions and the following disclaimer
|
||||
# in the documentation and/or other materials provided with the
|
||||
# distribution.
|
||||
# * Neither the name of Google Inc. nor the names of its
|
||||
# contributors may be used to endorse or promote products derived from
|
||||
# this software without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
# Includable Makefile to add a rule for generating .pb.go files from .proto files
|
||||
# (Google protocol buffer descriptions).
|
||||
# Typical use if myproto.proto is a file in package mypackage in this directory:
|
||||
#
|
||||
# include $(GOROOT)/src/pkg/github.com/golang/protobuf/Make.protobuf
|
||||
|
||||
%.pb.go: %.proto
|
||||
protoc --go_out=. $<
|
||||
|
55
vendor/github.com/golang/protobuf/Makefile
generated
vendored
Normal file
55
vendor/github.com/golang/protobuf/Makefile
generated
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
# Go support for Protocol Buffers - Google's data interchange format
|
||||
#
|
||||
# Copyright 2010 The Go Authors. All rights reserved.
|
||||
# https://github.com/golang/protobuf
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are
|
||||
# met:
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# * Redistributions in binary form must reproduce the above
|
||||
# copyright notice, this list of conditions and the following disclaimer
|
||||
# in the documentation and/or other materials provided with the
|
||||
# distribution.
|
||||
# * Neither the name of Google Inc. nor the names of its
|
||||
# contributors may be used to endorse or promote products derived from
|
||||
# this software without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
|
||||
all: install
|
||||
|
||||
install:
|
||||
go install ./proto ./jsonpb ./ptypes
|
||||
go install ./protoc-gen-go
|
||||
|
||||
test:
|
||||
go test ./proto ./jsonpb ./ptypes
|
||||
make -C protoc-gen-go/testdata test
|
||||
|
||||
clean:
|
||||
go clean ./...
|
||||
|
||||
nuke:
|
||||
go clean -i ./...
|
||||
|
||||
regenerate:
|
||||
make -C protoc-gen-go/descriptor regenerate
|
||||
make -C protoc-gen-go/plugin regenerate
|
||||
make -C protoc-gen-go/testdata regenerate
|
||||
make -C proto/testdata regenerate
|
||||
make -C jsonpb/jsonpb_test_proto regenerate
|
||||
make -C _conformance regenerate
|
241
vendor/github.com/golang/protobuf/README.md
generated
vendored
Normal file
241
vendor/github.com/golang/protobuf/README.md
generated
vendored
Normal file
@@ -0,0 +1,241 @@
|
||||
# Go support for Protocol Buffers
|
||||
|
||||
Google's data interchange format.
|
||||
Copyright 2010 The Go Authors.
|
||||
https://github.com/golang/protobuf
|
||||
|
||||
This package and the code it generates requires at least Go 1.4.
|
||||
|
||||
This software implements Go bindings for protocol buffers. For
|
||||
information about protocol buffers themselves, see
|
||||
https://developers.google.com/protocol-buffers/
|
||||
|
||||
## Installation ##
|
||||
|
||||
To use this software, you must:
|
||||
- Install the standard C++ implementation of protocol buffers from
|
||||
https://developers.google.com/protocol-buffers/
|
||||
- Of course, install the Go compiler and tools from
|
||||
https://golang.org/
|
||||
See
|
||||
https://golang.org/doc/install
|
||||
for details or, if you are using gccgo, follow the instructions at
|
||||
https://golang.org/doc/install/gccgo
|
||||
- Grab the code from the repository and install the proto package.
|
||||
The simplest way is to run `go get -u github.com/golang/protobuf/{proto,protoc-gen-go}`.
|
||||
The compiler plugin, protoc-gen-go, will be installed in $GOBIN,
|
||||
defaulting to $GOPATH/bin. It must be in your $PATH for the protocol
|
||||
compiler, protoc, to find it.
|
||||
|
||||
This software has two parts: a 'protocol compiler plugin' that
|
||||
generates Go source files that, once compiled, can access and manage
|
||||
protocol buffers; and a library that implements run-time support for
|
||||
encoding (marshaling), decoding (unmarshaling), and accessing protocol
|
||||
buffers.
|
||||
|
||||
There is support for gRPC in Go using protocol buffers.
|
||||
See the note at the bottom of this file for details.
|
||||
|
||||
There are no insertion points in the plugin.
|
||||
|
||||
|
||||
## Using protocol buffers with Go ##
|
||||
|
||||
Once the software is installed, there are two steps to using it.
|
||||
First you must compile the protocol buffer definitions and then import
|
||||
them, with the support library, into your program.
|
||||
|
||||
To compile the protocol buffer definition, run protoc with the --go_out
|
||||
parameter set to the directory you want to output the Go code to.
|
||||
|
||||
protoc --go_out=. *.proto
|
||||
|
||||
The generated files will be suffixed .pb.go. See the Test code below
|
||||
for an example using such a file.
|
||||
|
||||
|
||||
The package comment for the proto library contains text describing
|
||||
the interface provided in Go for protocol buffers. Here is an edited
|
||||
version.
|
||||
|
||||
==========
|
||||
|
||||
The proto package converts data structures to and from the
|
||||
wire format of protocol buffers. It works in concert with the
|
||||
Go source code generated for .proto files by the protocol compiler.
|
||||
|
||||
A summary of the properties of the protocol buffer interface
|
||||
for a protocol buffer variable v:
|
||||
|
||||
- Names are turned from camel_case to CamelCase for export.
|
||||
- There are no methods on v to set fields; just treat
|
||||
them as structure fields.
|
||||
- There are getters that return a field's value if set,
|
||||
and return the field's default value if unset.
|
||||
The getters work even if the receiver is a nil message.
|
||||
- The zero value for a struct is its correct initialization state.
|
||||
All desired fields must be set before marshaling.
|
||||
- A Reset() method will restore a protobuf struct to its zero state.
|
||||
- Non-repeated fields are pointers to the values; nil means unset.
|
||||
That is, optional or required field int32 f becomes F *int32.
|
||||
- Repeated fields are slices.
|
||||
- Helper functions are available to aid the setting of fields.
|
||||
Helpers for getting values are superseded by the
|
||||
GetFoo methods and their use is deprecated.
|
||||
msg.Foo = proto.String("hello") // set field
|
||||
- Constants are defined to hold the default values of all fields that
|
||||
have them. They have the form Default_StructName_FieldName.
|
||||
Because the getter methods handle defaulted values,
|
||||
direct use of these constants should be rare.
|
||||
- Enums are given type names and maps from names to values.
|
||||
Enum values are prefixed with the enum's type name. Enum types have
|
||||
a String method, and a Enum method to assist in message construction.
|
||||
- Nested groups and enums have type names prefixed with the name of
|
||||
the surrounding message type.
|
||||
- Extensions are given descriptor names that start with E_,
|
||||
followed by an underscore-delimited list of the nested messages
|
||||
that contain it (if any) followed by the CamelCased name of the
|
||||
extension field itself. HasExtension, ClearExtension, GetExtension
|
||||
and SetExtension are functions for manipulating extensions.
|
||||
- Oneof field sets are given a single field in their message,
|
||||
with distinguished wrapper types for each possible field value.
|
||||
- Marshal and Unmarshal are functions to encode and decode the wire format.
|
||||
|
||||
When the .proto file specifies `syntax="proto3"`, there are some differences:
|
||||
|
||||
- Non-repeated fields of non-message type are values instead of pointers.
|
||||
- Getters are only generated for message and oneof fields.
|
||||
- Enum types do not get an Enum method.
|
||||
|
||||
Consider file test.proto, containing
|
||||
|
||||
```proto
|
||||
package example;
|
||||
|
||||
enum FOO { X = 17; };
|
||||
|
||||
message Test {
|
||||
required string label = 1;
|
||||
optional int32 type = 2 [default=77];
|
||||
repeated int64 reps = 3;
|
||||
optional group OptionalGroup = 4 {
|
||||
required string RequiredField = 5;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
To create and play with a Test object from the example package,
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"path/to/example"
|
||||
)
|
||||
|
||||
func main() {
|
||||
test := &example.Test {
|
||||
Label: proto.String("hello"),
|
||||
Type: proto.Int32(17),
|
||||
Reps: []int64{1, 2, 3},
|
||||
Optionalgroup: &example.Test_OptionalGroup {
|
||||
RequiredField: proto.String("good bye"),
|
||||
},
|
||||
}
|
||||
data, err := proto.Marshal(test)
|
||||
if err != nil {
|
||||
log.Fatal("marshaling error: ", err)
|
||||
}
|
||||
newTest := &example.Test{}
|
||||
err = proto.Unmarshal(data, newTest)
|
||||
if err != nil {
|
||||
log.Fatal("unmarshaling error: ", err)
|
||||
}
|
||||
// Now test and newTest contain the same data.
|
||||
if test.GetLabel() != newTest.GetLabel() {
|
||||
log.Fatalf("data mismatch %q != %q", test.GetLabel(), newTest.GetLabel())
|
||||
}
|
||||
// etc.
|
||||
}
|
||||
```
|
||||
|
||||
## Parameters ##
|
||||
|
||||
To pass extra parameters to the plugin, use a comma-separated
|
||||
parameter list separated from the output directory by a colon:
|
||||
|
||||
|
||||
protoc --go_out=plugins=grpc,import_path=mypackage:. *.proto
|
||||
|
||||
|
||||
- `import_prefix=xxx` - a prefix that is added onto the beginning of
|
||||
all imports. Useful for things like generating protos in a
|
||||
subdirectory, or regenerating vendored protobufs in-place.
|
||||
- `import_path=foo/bar` - used as the package if no input files
|
||||
declare `go_package`. If it contains slashes, everything up to the
|
||||
rightmost slash is ignored.
|
||||
- `plugins=plugin1+plugin2` - specifies the list of sub-plugins to
|
||||
load. The only plugin in this repo is `grpc`.
|
||||
- `Mfoo/bar.proto=quux/shme` - declares that foo/bar.proto is
|
||||
associated with Go package quux/shme. This is subject to the
|
||||
import_prefix parameter.
|
||||
|
||||
## gRPC Support ##
|
||||
|
||||
If a proto file specifies RPC services, protoc-gen-go can be instructed to
|
||||
generate code compatible with gRPC (http://www.grpc.io/). To do this, pass
|
||||
the `plugins` parameter to protoc-gen-go; the usual way is to insert it into
|
||||
the --go_out argument to protoc:
|
||||
|
||||
protoc --go_out=plugins=grpc:. *.proto
|
||||
|
||||
## Compatibility ##
|
||||
|
||||
The library and the generated code are expected to be stable over time.
|
||||
However, we reserve the right to make breaking changes without notice for the
|
||||
following reasons:
|
||||
|
||||
- Security. A security issue in the specification or implementation may come to
|
||||
light whose resolution requires breaking compatibility. We reserve the right
|
||||
to address such security issues.
|
||||
- Unspecified behavior. There are some aspects of the Protocol Buffers
|
||||
specification that are undefined. Programs that depend on such unspecified
|
||||
behavior may break in future releases.
|
||||
- Specification errors or changes. If it becomes necessary to address an
|
||||
inconsistency, incompleteness, or change in the Protocol Buffers
|
||||
specification, resolving the issue could affect the meaning or legality of
|
||||
existing programs. We reserve the right to address such issues, including
|
||||
updating the implementations.
|
||||
- Bugs. If the library has a bug that violates the specification, a program
|
||||
that depends on the buggy behavior may break if the bug is fixed. We reserve
|
||||
the right to fix such bugs.
|
||||
- Adding methods or fields to generated structs. These may conflict with field
|
||||
names that already exist in a schema, causing applications to break. When the
|
||||
code generator encounters a field in the schema that would collide with a
|
||||
generated field or method name, the code generator will append an underscore
|
||||
to the generated field or method name.
|
||||
- Adding, removing, or changing methods or fields in generated structs that
|
||||
start with `XXX`. These parts of the generated code are exported out of
|
||||
necessity, but should not be considered part of the public API.
|
||||
- Adding, removing, or changing unexported symbols in generated code.
|
||||
|
||||
Any breaking changes outside of these will be announced 6 months in advance to
|
||||
protobuf@googlegroups.com.
|
||||
|
||||
You should, whenever possible, use generated code created by the `protoc-gen-go`
|
||||
tool built at the same commit as the `proto` package. The `proto` package
|
||||
declares package-level constants in the form `ProtoPackageIsVersionX`.
|
||||
Application code and generated code may depend on one of these constants to
|
||||
ensure that compilation will fail if the available version of the proto library
|
||||
is too old. Whenever we make a change to the generated code that requires newer
|
||||
library support, in the same commit we will increment the version number of the
|
||||
generated code and declare a new package-level constant whose name incorporates
|
||||
the latest version number. Removing a compatibility constant is considered a
|
||||
breaking change and would be subject to the announcement policy stated above.
|
||||
|
||||
The `protoc-gen-go/generator` package exposes a plugin interface,
|
||||
which is used by the gRPC code generation. This interface is not
|
||||
supported and is subject to incompatible changes without notice.
|
33
vendor/github.com/golang/protobuf/_conformance/Makefile
generated
vendored
Normal file
33
vendor/github.com/golang/protobuf/_conformance/Makefile
generated
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
# Go support for Protocol Buffers - Google's data interchange format
|
||||
#
|
||||
# Copyright 2016 The Go Authors. All rights reserved.
|
||||
# https://github.com/golang/protobuf
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are
|
||||
# met:
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# * Redistributions in binary form must reproduce the above
|
||||
# copyright notice, this list of conditions and the following disclaimer
|
||||
# in the documentation and/or other materials provided with the
|
||||
# distribution.
|
||||
# * Neither the name of Google Inc. nor the names of its
|
||||
# contributors may be used to endorse or promote products derived from
|
||||
# this software without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
regenerate:
|
||||
protoc --go_out=Mgoogle/protobuf/any.proto=github.com/golang/protobuf/ptypes/any,Mgoogle/protobuf/duration.proto=github.com/golang/protobuf/ptypes/duration,Mgoogle/protobuf/struct.proto=github.com/golang/protobuf/ptypes/struct,Mgoogle/protobuf/timestamp.proto=github.com/golang/protobuf/ptypes/timestamp,Mgoogle/protobuf/wrappers.proto=github.com/golang/protobuf/ptypes/wrappers,Mgoogle/protobuf/field_mask.proto=google.golang.org/genproto/protobuf:. conformance_proto/conformance.proto
|
161
vendor/github.com/golang/protobuf/_conformance/conformance.go
generated
vendored
Normal file
161
vendor/github.com/golang/protobuf/_conformance/conformance.go
generated
vendored
Normal file
@@ -0,0 +1,161 @@
|
||||
// Go support for Protocol Buffers - Google's data interchange format
|
||||
//
|
||||
// Copyright 2016 The Go Authors. All rights reserved.
|
||||
// https://github.com/golang/protobuf
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
// conformance implements the conformance test subprocess protocol as
|
||||
// documented in conformance.proto.
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
pb "github.com/golang/protobuf/_conformance/conformance_proto"
|
||||
"github.com/golang/protobuf/jsonpb"
|
||||
"github.com/golang/protobuf/proto"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var sizeBuf [4]byte
|
||||
inbuf := make([]byte, 0, 4096)
|
||||
outbuf := proto.NewBuffer(nil)
|
||||
for {
|
||||
if _, err := io.ReadFull(os.Stdin, sizeBuf[:]); err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "go conformance: read request:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
size := binary.LittleEndian.Uint32(sizeBuf[:])
|
||||
if int(size) > cap(inbuf) {
|
||||
inbuf = make([]byte, size)
|
||||
}
|
||||
inbuf = inbuf[:size]
|
||||
if _, err := io.ReadFull(os.Stdin, inbuf); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "go conformance: read request:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
req := new(pb.ConformanceRequest)
|
||||
if err := proto.Unmarshal(inbuf, req); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "go conformance: parse request:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
res := handle(req)
|
||||
|
||||
if err := outbuf.Marshal(res); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "go conformance: marshal response:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
binary.LittleEndian.PutUint32(sizeBuf[:], uint32(len(outbuf.Bytes())))
|
||||
if _, err := os.Stdout.Write(sizeBuf[:]); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "go conformance: write response:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if _, err := os.Stdout.Write(outbuf.Bytes()); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "go conformance: write response:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
outbuf.Reset()
|
||||
}
|
||||
}
|
||||
|
||||
var jsonMarshaler = jsonpb.Marshaler{
|
||||
OrigName: true,
|
||||
}
|
||||
|
||||
func handle(req *pb.ConformanceRequest) *pb.ConformanceResponse {
|
||||
var err error
|
||||
var msg pb.TestAllTypes
|
||||
switch p := req.Payload.(type) {
|
||||
case *pb.ConformanceRequest_ProtobufPayload:
|
||||
err = proto.Unmarshal(p.ProtobufPayload, &msg)
|
||||
case *pb.ConformanceRequest_JsonPayload:
|
||||
err = jsonpb.UnmarshalString(p.JsonPayload, &msg)
|
||||
if err != nil && err.Error() == "unmarshaling Any not supported yet" {
|
||||
return &pb.ConformanceResponse{
|
||||
Result: &pb.ConformanceResponse_Skipped{
|
||||
Skipped: err.Error(),
|
||||
},
|
||||
}
|
||||
}
|
||||
default:
|
||||
return &pb.ConformanceResponse{
|
||||
Result: &pb.ConformanceResponse_RuntimeError{
|
||||
RuntimeError: "unknown request payload type",
|
||||
},
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return &pb.ConformanceResponse{
|
||||
Result: &pb.ConformanceResponse_ParseError{
|
||||
ParseError: err.Error(),
|
||||
},
|
||||
}
|
||||
}
|
||||
switch req.RequestedOutputFormat {
|
||||
case pb.WireFormat_PROTOBUF:
|
||||
p, err := proto.Marshal(&msg)
|
||||
if err != nil {
|
||||
return &pb.ConformanceResponse{
|
||||
Result: &pb.ConformanceResponse_SerializeError{
|
||||
SerializeError: err.Error(),
|
||||
},
|
||||
}
|
||||
}
|
||||
return &pb.ConformanceResponse{
|
||||
Result: &pb.ConformanceResponse_ProtobufPayload{
|
||||
ProtobufPayload: p,
|
||||
},
|
||||
}
|
||||
case pb.WireFormat_JSON:
|
||||
p, err := jsonMarshaler.MarshalToString(&msg)
|
||||
if err != nil {
|
||||
return &pb.ConformanceResponse{
|
||||
Result: &pb.ConformanceResponse_SerializeError{
|
||||
SerializeError: err.Error(),
|
||||
},
|
||||
}
|
||||
}
|
||||
return &pb.ConformanceResponse{
|
||||
Result: &pb.ConformanceResponse_JsonPayload{
|
||||
JsonPayload: p,
|
||||
},
|
||||
}
|
||||
default:
|
||||
return &pb.ConformanceResponse{
|
||||
Result: &pb.ConformanceResponse_RuntimeError{
|
||||
RuntimeError: "unknown output format",
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
1886
vendor/github.com/golang/protobuf/_conformance/conformance_proto/conformance.pb.go
generated
vendored
Normal file
1886
vendor/github.com/golang/protobuf/_conformance/conformance_proto/conformance.pb.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
285
vendor/github.com/golang/protobuf/_conformance/conformance_proto/conformance.proto
generated
vendored
Normal file
285
vendor/github.com/golang/protobuf/_conformance/conformance_proto/conformance.proto
generated
vendored
Normal file
@@ -0,0 +1,285 @@
|
||||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2008 Google Inc. All rights reserved.
|
||||
// https://developers.google.com/protocol-buffers/
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
syntax = "proto3";
|
||||
package conformance;
|
||||
option java_package = "com.google.protobuf.conformance";
|
||||
|
||||
import "google/protobuf/any.proto";
|
||||
import "google/protobuf/duration.proto";
|
||||
import "google/protobuf/field_mask.proto";
|
||||
import "google/protobuf/struct.proto";
|
||||
import "google/protobuf/timestamp.proto";
|
||||
import "google/protobuf/wrappers.proto";
|
||||
|
||||
// This defines the conformance testing protocol. This protocol exists between
|
||||
// the conformance test suite itself and the code being tested. For each test,
|
||||
// the suite will send a ConformanceRequest message and expect a
|
||||
// ConformanceResponse message.
|
||||
//
|
||||
// You can either run the tests in two different ways:
|
||||
//
|
||||
// 1. in-process (using the interface in conformance_test.h).
|
||||
//
|
||||
// 2. as a sub-process communicating over a pipe. Information about how to
|
||||
// do this is in conformance_test_runner.cc.
|
||||
//
|
||||
// Pros/cons of the two approaches:
|
||||
//
|
||||
// - running as a sub-process is much simpler for languages other than C/C++.
|
||||
//
|
||||
// - running as a sub-process may be more tricky in unusual environments like
|
||||
// iOS apps, where fork/stdin/stdout are not available.
|
||||
|
||||
enum WireFormat {
|
||||
UNSPECIFIED = 0;
|
||||
PROTOBUF = 1;
|
||||
JSON = 2;
|
||||
}
|
||||
|
||||
// Represents a single test case's input. The testee should:
|
||||
//
|
||||
// 1. parse this proto (which should always succeed)
|
||||
// 2. parse the protobuf or JSON payload in "payload" (which may fail)
|
||||
// 3. if the parse succeeded, serialize the message in the requested format.
|
||||
message ConformanceRequest {
|
||||
// The payload (whether protobuf of JSON) is always for a TestAllTypes proto
|
||||
// (see below).
|
||||
oneof payload {
|
||||
bytes protobuf_payload = 1;
|
||||
string json_payload = 2;
|
||||
}
|
||||
|
||||
// Which format should the testee serialize its message to?
|
||||
WireFormat requested_output_format = 3;
|
||||
}
|
||||
|
||||
// Represents a single test case's output.
|
||||
message ConformanceResponse {
|
||||
oneof result {
|
||||
// This string should be set to indicate parsing failed. The string can
|
||||
// provide more information about the parse error if it is available.
|
||||
//
|
||||
// Setting this string does not necessarily mean the testee failed the
|
||||
// test. Some of the test cases are intentionally invalid input.
|
||||
string parse_error = 1;
|
||||
|
||||
// If the input was successfully parsed but errors occurred when
|
||||
// serializing it to the requested output format, set the error message in
|
||||
// this field.
|
||||
string serialize_error = 6;
|
||||
|
||||
// This should be set if some other error occurred. This will always
|
||||
// indicate that the test failed. The string can provide more information
|
||||
// about the failure.
|
||||
string runtime_error = 2;
|
||||
|
||||
// If the input was successfully parsed and the requested output was
|
||||
// protobuf, serialize it to protobuf and set it in this field.
|
||||
bytes protobuf_payload = 3;
|
||||
|
||||
// If the input was successfully parsed and the requested output was JSON,
|
||||
// serialize to JSON and set it in this field.
|
||||
string json_payload = 4;
|
||||
|
||||
// For when the testee skipped the test, likely because a certain feature
|
||||
// wasn't supported, like JSON input/output.
|
||||
string skipped = 5;
|
||||
}
|
||||
}
|
||||
|
||||
// This proto includes every type of field in both singular and repeated
|
||||
// forms.
|
||||
message TestAllTypes {
|
||||
message NestedMessage {
|
||||
int32 a = 1;
|
||||
TestAllTypes corecursive = 2;
|
||||
}
|
||||
|
||||
enum NestedEnum {
|
||||
FOO = 0;
|
||||
BAR = 1;
|
||||
BAZ = 2;
|
||||
NEG = -1; // Intentionally negative.
|
||||
}
|
||||
|
||||
// Singular
|
||||
int32 optional_int32 = 1;
|
||||
int64 optional_int64 = 2;
|
||||
uint32 optional_uint32 = 3;
|
||||
uint64 optional_uint64 = 4;
|
||||
sint32 optional_sint32 = 5;
|
||||
sint64 optional_sint64 = 6;
|
||||
fixed32 optional_fixed32 = 7;
|
||||
fixed64 optional_fixed64 = 8;
|
||||
sfixed32 optional_sfixed32 = 9;
|
||||
sfixed64 optional_sfixed64 = 10;
|
||||
float optional_float = 11;
|
||||
double optional_double = 12;
|
||||
bool optional_bool = 13;
|
||||
string optional_string = 14;
|
||||
bytes optional_bytes = 15;
|
||||
|
||||
NestedMessage optional_nested_message = 18;
|
||||
ForeignMessage optional_foreign_message = 19;
|
||||
|
||||
NestedEnum optional_nested_enum = 21;
|
||||
ForeignEnum optional_foreign_enum = 22;
|
||||
|
||||
string optional_string_piece = 24 [ctype=STRING_PIECE];
|
||||
string optional_cord = 25 [ctype=CORD];
|
||||
|
||||
TestAllTypes recursive_message = 27;
|
||||
|
||||
// Repeated
|
||||
repeated int32 repeated_int32 = 31;
|
||||
repeated int64 repeated_int64 = 32;
|
||||
repeated uint32 repeated_uint32 = 33;
|
||||
repeated uint64 repeated_uint64 = 34;
|
||||
repeated sint32 repeated_sint32 = 35;
|
||||
repeated sint64 repeated_sint64 = 36;
|
||||
repeated fixed32 repeated_fixed32 = 37;
|
||||
repeated fixed64 repeated_fixed64 = 38;
|
||||
repeated sfixed32 repeated_sfixed32 = 39;
|
||||
repeated sfixed64 repeated_sfixed64 = 40;
|
||||
repeated float repeated_float = 41;
|
||||
repeated double repeated_double = 42;
|
||||
repeated bool repeated_bool = 43;
|
||||
repeated string repeated_string = 44;
|
||||
repeated bytes repeated_bytes = 45;
|
||||
|
||||
repeated NestedMessage repeated_nested_message = 48;
|
||||
repeated ForeignMessage repeated_foreign_message = 49;
|
||||
|
||||
repeated NestedEnum repeated_nested_enum = 51;
|
||||
repeated ForeignEnum repeated_foreign_enum = 52;
|
||||
|
||||
repeated string repeated_string_piece = 54 [ctype=STRING_PIECE];
|
||||
repeated string repeated_cord = 55 [ctype=CORD];
|
||||
|
||||
// Map
|
||||
map < int32, int32> map_int32_int32 = 56;
|
||||
map < int64, int64> map_int64_int64 = 57;
|
||||
map < uint32, uint32> map_uint32_uint32 = 58;
|
||||
map < uint64, uint64> map_uint64_uint64 = 59;
|
||||
map < sint32, sint32> map_sint32_sint32 = 60;
|
||||
map < sint64, sint64> map_sint64_sint64 = 61;
|
||||
map < fixed32, fixed32> map_fixed32_fixed32 = 62;
|
||||
map < fixed64, fixed64> map_fixed64_fixed64 = 63;
|
||||
map <sfixed32, sfixed32> map_sfixed32_sfixed32 = 64;
|
||||
map <sfixed64, sfixed64> map_sfixed64_sfixed64 = 65;
|
||||
map < int32, float> map_int32_float = 66;
|
||||
map < int32, double> map_int32_double = 67;
|
||||
map < bool, bool> map_bool_bool = 68;
|
||||
map < string, string> map_string_string = 69;
|
||||
map < string, bytes> map_string_bytes = 70;
|
||||
map < string, NestedMessage> map_string_nested_message = 71;
|
||||
map < string, ForeignMessage> map_string_foreign_message = 72;
|
||||
map < string, NestedEnum> map_string_nested_enum = 73;
|
||||
map < string, ForeignEnum> map_string_foreign_enum = 74;
|
||||
|
||||
oneof oneof_field {
|
||||
uint32 oneof_uint32 = 111;
|
||||
NestedMessage oneof_nested_message = 112;
|
||||
string oneof_string = 113;
|
||||
bytes oneof_bytes = 114;
|
||||
bool oneof_bool = 115;
|
||||
uint64 oneof_uint64 = 116;
|
||||
float oneof_float = 117;
|
||||
double oneof_double = 118;
|
||||
NestedEnum oneof_enum = 119;
|
||||
}
|
||||
|
||||
// Well-known types
|
||||
google.protobuf.BoolValue optional_bool_wrapper = 201;
|
||||
google.protobuf.Int32Value optional_int32_wrapper = 202;
|
||||
google.protobuf.Int64Value optional_int64_wrapper = 203;
|
||||
google.protobuf.UInt32Value optional_uint32_wrapper = 204;
|
||||
google.protobuf.UInt64Value optional_uint64_wrapper = 205;
|
||||
google.protobuf.FloatValue optional_float_wrapper = 206;
|
||||
google.protobuf.DoubleValue optional_double_wrapper = 207;
|
||||
google.protobuf.StringValue optional_string_wrapper = 208;
|
||||
google.protobuf.BytesValue optional_bytes_wrapper = 209;
|
||||
|
||||
repeated google.protobuf.BoolValue repeated_bool_wrapper = 211;
|
||||
repeated google.protobuf.Int32Value repeated_int32_wrapper = 212;
|
||||
repeated google.protobuf.Int64Value repeated_int64_wrapper = 213;
|
||||
repeated google.protobuf.UInt32Value repeated_uint32_wrapper = 214;
|
||||
repeated google.protobuf.UInt64Value repeated_uint64_wrapper = 215;
|
||||
repeated google.protobuf.FloatValue repeated_float_wrapper = 216;
|
||||
repeated google.protobuf.DoubleValue repeated_double_wrapper = 217;
|
||||
repeated google.protobuf.StringValue repeated_string_wrapper = 218;
|
||||
repeated google.protobuf.BytesValue repeated_bytes_wrapper = 219;
|
||||
|
||||
google.protobuf.Duration optional_duration = 301;
|
||||
google.protobuf.Timestamp optional_timestamp = 302;
|
||||
google.protobuf.FieldMask optional_field_mask = 303;
|
||||
google.protobuf.Struct optional_struct = 304;
|
||||
google.protobuf.Any optional_any = 305;
|
||||
google.protobuf.Value optional_value = 306;
|
||||
|
||||
repeated google.protobuf.Duration repeated_duration = 311;
|
||||
repeated google.protobuf.Timestamp repeated_timestamp = 312;
|
||||
repeated google.protobuf.FieldMask repeated_fieldmask = 313;
|
||||
repeated google.protobuf.Struct repeated_struct = 324;
|
||||
repeated google.protobuf.Any repeated_any = 315;
|
||||
repeated google.protobuf.Value repeated_value = 316;
|
||||
|
||||
// Test field-name-to-JSON-name convention.
|
||||
// (protobuf says names can be any valid C/C++ identifier.)
|
||||
int32 fieldname1 = 401;
|
||||
int32 field_name2 = 402;
|
||||
int32 _field_name3 = 403;
|
||||
int32 field__name4_ = 404;
|
||||
int32 field0name5 = 405;
|
||||
int32 field_0_name6 = 406;
|
||||
int32 fieldName7 = 407;
|
||||
int32 FieldName8 = 408;
|
||||
int32 field_Name9 = 409;
|
||||
int32 Field_Name10 = 410;
|
||||
int32 FIELD_NAME11 = 411;
|
||||
int32 FIELD_name12 = 412;
|
||||
int32 __field_name13 = 413;
|
||||
int32 __Field_name14 = 414;
|
||||
int32 field__name15 = 415;
|
||||
int32 field__Name16 = 416;
|
||||
int32 field_name17__ = 417;
|
||||
int32 Field_name18__ = 418;
|
||||
}
|
||||
|
||||
message ForeignMessage {
|
||||
int32 c = 1;
|
||||
}
|
||||
|
||||
enum ForeignEnum {
|
||||
FOREIGN_FOO = 0;
|
||||
FOREIGN_BAR = 1;
|
||||
FOREIGN_BAZ = 2;
|
||||
}
|
93
vendor/github.com/golang/protobuf/descriptor/descriptor.go
generated
vendored
Normal file
93
vendor/github.com/golang/protobuf/descriptor/descriptor.go
generated
vendored
Normal file
@@ -0,0 +1,93 @@
|
||||
// Go support for Protocol Buffers - Google's data interchange format
|
||||
//
|
||||
// Copyright 2016 The Go Authors. All rights reserved.
|
||||
// https://github.com/golang/protobuf
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
// Package descriptor provides functions for obtaining protocol buffer
|
||||
// descriptors for generated Go types.
|
||||
//
|
||||
// These functions cannot go in package proto because they depend on the
|
||||
// generated protobuf descriptor messages, which themselves depend on proto.
|
||||
package descriptor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
protobuf "google.golang.org/genproto/protobuf"
|
||||
)
|
||||
|
||||
// extractFile extracts a FileDescriptorProto from a gzip'd buffer.
|
||||
func extractFile(gz []byte) (*protobuf.FileDescriptorProto, error) {
|
||||
r, err := gzip.NewReader(bytes.NewReader(gz))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to open gzip reader: %v", err)
|
||||
}
|
||||
defer r.Close()
|
||||
|
||||
b, err := ioutil.ReadAll(r)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to uncompress descriptor: %v", err)
|
||||
}
|
||||
|
||||
fd := new(protobuf.FileDescriptorProto)
|
||||
if err := proto.Unmarshal(b, fd); err != nil {
|
||||
return nil, fmt.Errorf("malformed FileDescriptorProto: %v", err)
|
||||
}
|
||||
|
||||
return fd, nil
|
||||
}
|
||||
|
||||
// Message is a proto.Message with a method to return its descriptor.
|
||||
//
|
||||
// Message types generated by the protocol compiler always satisfy
|
||||
// the Message interface.
|
||||
type Message interface {
|
||||
proto.Message
|
||||
Descriptor() ([]byte, []int)
|
||||
}
|
||||
|
||||
// ForMessage returns a FileDescriptorProto and a DescriptorProto from within it
|
||||
// describing the given message.
|
||||
func ForMessage(msg Message) (fd *protobuf.FileDescriptorProto, md *protobuf.DescriptorProto) {
|
||||
gz, path := msg.Descriptor()
|
||||
fd, err := extractFile(gz)
|
||||
if err != nil {
|
||||
panic(fmt.Sprintf("invalid FileDescriptorProto for %T: %v", msg, err))
|
||||
}
|
||||
|
||||
md = fd.MessageType[path[0]]
|
||||
for _, i := range path[1:] {
|
||||
md = md.NestedType[i]
|
||||
}
|
||||
return fd, md
|
||||
}
|
32
vendor/github.com/golang/protobuf/descriptor/descriptor_test.go
generated
vendored
Normal file
32
vendor/github.com/golang/protobuf/descriptor/descriptor_test.go
generated
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
package descriptor_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/protobuf/descriptor"
|
||||
tpb "github.com/golang/protobuf/proto/testdata"
|
||||
protobuf "google.golang.org/genproto/protobuf"
|
||||
)
|
||||
|
||||
func TestMessage(t *testing.T) {
|
||||
var msg *protobuf.DescriptorProto
|
||||
fd, md := descriptor.ForMessage(msg)
|
||||
if pkg, want := fd.GetPackage(), "google.protobuf"; pkg != want {
|
||||
t.Errorf("descriptor.ForMessage(%T).GetPackage() = %q; want %q", msg, pkg, want)
|
||||
}
|
||||
if name, want := md.GetName(), "DescriptorProto"; name != want {
|
||||
t.Fatalf("descriptor.ForMessage(%T).GetName() = %q; want %q", msg, name, want)
|
||||
}
|
||||
}
|
||||
|
||||
func Example_Options() {
|
||||
var msg *tpb.MyMessageSet
|
||||
_, md := descriptor.ForMessage(msg)
|
||||
if md.GetOptions().GetMessageSetWireFormat() {
|
||||
fmt.Printf("%v uses option message_set_wire_format.\n", md.GetName())
|
||||
}
|
||||
|
||||
// Output:
|
||||
// MyMessageSet uses option message_set_wire_format.
|
||||
}
|
843
vendor/github.com/golang/protobuf/jsonpb/jsonpb.go
generated
vendored
Normal file
843
vendor/github.com/golang/protobuf/jsonpb/jsonpb.go
generated
vendored
Normal file
@@ -0,0 +1,843 @@
|
||||
// Go support for Protocol Buffers - Google's data interchange format
|
||||
//
|
||||
// Copyright 2015 The Go Authors. All rights reserved.
|
||||
// https://github.com/golang/protobuf
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
/*
|
||||
Package jsonpb provides marshaling and unmarshaling between protocol buffers and JSON.
|
||||
It follows the specification at https://developers.google.com/protocol-buffers/docs/proto3#json.
|
||||
|
||||
This package produces a different output than the standard "encoding/json" package,
|
||||
which does not operate correctly on protocol buffers.
|
||||
*/
|
||||
package jsonpb
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
)
|
||||
|
||||
// Marshaler is a configurable object for converting between
|
||||
// protocol buffer objects and a JSON representation for them.
|
||||
type Marshaler struct {
|
||||
// Whether to render enum values as integers, as opposed to string values.
|
||||
EnumsAsInts bool
|
||||
|
||||
// Whether to render fields with zero values.
|
||||
EmitDefaults bool
|
||||
|
||||
// A string to indent each level by. The presence of this field will
|
||||
// also cause a space to appear between the field separator and
|
||||
// value, and for newlines to be appear between fields and array
|
||||
// elements.
|
||||
Indent string
|
||||
|
||||
// Whether to use the original (.proto) name for fields.
|
||||
OrigName bool
|
||||
}
|
||||
|
||||
// Marshal marshals a protocol buffer into JSON.
|
||||
func (m *Marshaler) Marshal(out io.Writer, pb proto.Message) error {
|
||||
writer := &errWriter{writer: out}
|
||||
return m.marshalObject(writer, pb, "", "")
|
||||
}
|
||||
|
||||
// MarshalToString converts a protocol buffer object to JSON string.
|
||||
func (m *Marshaler) MarshalToString(pb proto.Message) (string, error) {
|
||||
var buf bytes.Buffer
|
||||
if err := m.Marshal(&buf, pb); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
type int32Slice []int32
|
||||
|
||||
// For sorting extensions ids to ensure stable output.
|
||||
func (s int32Slice) Len() int { return len(s) }
|
||||
func (s int32Slice) Less(i, j int) bool { return s[i] < s[j] }
|
||||
func (s int32Slice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
|
||||
type wkt interface {
|
||||
XXX_WellKnownType() string
|
||||
}
|
||||
|
||||
// marshalObject writes a struct to the Writer.
|
||||
func (m *Marshaler) marshalObject(out *errWriter, v proto.Message, indent, typeURL string) error {
|
||||
s := reflect.ValueOf(v).Elem()
|
||||
|
||||
// Handle well-known types.
|
||||
if wkt, ok := v.(wkt); ok {
|
||||
switch wkt.XXX_WellKnownType() {
|
||||
case "DoubleValue", "FloatValue", "Int64Value", "UInt64Value",
|
||||
"Int32Value", "UInt32Value", "BoolValue", "StringValue", "BytesValue":
|
||||
// "Wrappers use the same representation in JSON
|
||||
// as the wrapped primitive type, ..."
|
||||
sprop := proto.GetProperties(s.Type())
|
||||
return m.marshalValue(out, sprop.Prop[0], s.Field(0), indent)
|
||||
case "Any":
|
||||
// Any is a bit more involved.
|
||||
return m.marshalAny(out, v, indent)
|
||||
case "Duration":
|
||||
// "Generated output always contains 3, 6, or 9 fractional digits,
|
||||
// depending on required precision."
|
||||
s, ns := s.Field(0).Int(), s.Field(1).Int()
|
||||
d := time.Duration(s)*time.Second + time.Duration(ns)*time.Nanosecond
|
||||
x := fmt.Sprintf("%.9f", d.Seconds())
|
||||
x = strings.TrimSuffix(x, "000")
|
||||
x = strings.TrimSuffix(x, "000")
|
||||
out.write(`"`)
|
||||
out.write(x)
|
||||
out.write(`s"`)
|
||||
return out.err
|
||||
case "Struct":
|
||||
// Let marshalValue handle the `fields` map.
|
||||
// TODO: pass the correct Properties if needed.
|
||||
return m.marshalValue(out, &proto.Properties{}, s.Field(0), indent)
|
||||
case "Timestamp":
|
||||
// "RFC 3339, where generated output will always be Z-normalized
|
||||
// and uses 3, 6 or 9 fractional digits."
|
||||
s, ns := s.Field(0).Int(), s.Field(1).Int()
|
||||
t := time.Unix(s, ns).UTC()
|
||||
// time.RFC3339Nano isn't exactly right (we need to get 3/6/9 fractional digits).
|
||||
x := t.Format("2006-01-02T15:04:05.000000000")
|
||||
x = strings.TrimSuffix(x, "000")
|
||||
x = strings.TrimSuffix(x, "000")
|
||||
out.write(`"`)
|
||||
out.write(x)
|
||||
out.write(`Z"`)
|
||||
return out.err
|
||||
case "Value":
|
||||
// Value has a single oneof.
|
||||
kind := s.Field(0)
|
||||
if kind.IsNil() {
|
||||
// "absence of any variant indicates an error"
|
||||
return errors.New("nil Value")
|
||||
}
|
||||
// oneof -> *T -> T -> T.F
|
||||
x := kind.Elem().Elem().Field(0)
|
||||
// TODO: pass the correct Properties if needed.
|
||||
return m.marshalValue(out, &proto.Properties{}, x, indent)
|
||||
}
|
||||
}
|
||||
|
||||
out.write("{")
|
||||
if m.Indent != "" {
|
||||
out.write("\n")
|
||||
}
|
||||
|
||||
firstField := true
|
||||
|
||||
if typeURL != "" {
|
||||
if err := m.marshalTypeURL(out, indent, typeURL); err != nil {
|
||||
return err
|
||||
}
|
||||
firstField = false
|
||||
}
|
||||
|
||||
for i := 0; i < s.NumField(); i++ {
|
||||
value := s.Field(i)
|
||||
valueField := s.Type().Field(i)
|
||||
if strings.HasPrefix(valueField.Name, "XXX_") {
|
||||
continue
|
||||
}
|
||||
|
||||
// IsNil will panic on most value kinds.
|
||||
switch value.Kind() {
|
||||
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
|
||||
if value.IsNil() {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if !m.EmitDefaults {
|
||||
switch value.Kind() {
|
||||
case reflect.Bool:
|
||||
if !value.Bool() {
|
||||
continue
|
||||
}
|
||||
case reflect.Int32, reflect.Int64:
|
||||
if value.Int() == 0 {
|
||||
continue
|
||||
}
|
||||
case reflect.Uint32, reflect.Uint64:
|
||||
if value.Uint() == 0 {
|
||||
continue
|
||||
}
|
||||
case reflect.Float32, reflect.Float64:
|
||||
if value.Float() == 0 {
|
||||
continue
|
||||
}
|
||||
case reflect.String:
|
||||
if value.Len() == 0 {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Oneof fields need special handling.
|
||||
if valueField.Tag.Get("protobuf_oneof") != "" {
|
||||
// value is an interface containing &T{real_value}.
|
||||
sv := value.Elem().Elem() // interface -> *T -> T
|
||||
value = sv.Field(0)
|
||||
valueField = sv.Type().Field(0)
|
||||
}
|
||||
prop := jsonProperties(valueField, m.OrigName)
|
||||
if !firstField {
|
||||
m.writeSep(out)
|
||||
}
|
||||
if err := m.marshalField(out, prop, value, indent); err != nil {
|
||||
return err
|
||||
}
|
||||
firstField = false
|
||||
}
|
||||
|
||||
// Handle proto2 extensions.
|
||||
if ep, ok := v.(proto.Message); ok {
|
||||
extensions := proto.RegisteredExtensions(v)
|
||||
// Sort extensions for stable output.
|
||||
ids := make([]int32, 0, len(extensions))
|
||||
for id, desc := range extensions {
|
||||
if !proto.HasExtension(ep, desc) {
|
||||
continue
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
sort.Sort(int32Slice(ids))
|
||||
for _, id := range ids {
|
||||
desc := extensions[id]
|
||||
if desc == nil {
|
||||
// unknown extension
|
||||
continue
|
||||
}
|
||||
ext, extErr := proto.GetExtension(ep, desc)
|
||||
if extErr != nil {
|
||||
return extErr
|
||||
}
|
||||
value := reflect.ValueOf(ext)
|
||||
var prop proto.Properties
|
||||
prop.Parse(desc.Tag)
|
||||
prop.JSONName = fmt.Sprintf("[%s]", desc.Name)
|
||||
if !firstField {
|
||||
m.writeSep(out)
|
||||
}
|
||||
if err := m.marshalField(out, &prop, value, indent); err != nil {
|
||||
return err
|
||||
}
|
||||
firstField = false
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if m.Indent != "" {
|
||||
out.write("\n")
|
||||
out.write(indent)
|
||||
}
|
||||
out.write("}")
|
||||
return out.err
|
||||
}
|
||||
|
||||
func (m *Marshaler) writeSep(out *errWriter) {
|
||||
if m.Indent != "" {
|
||||
out.write(",\n")
|
||||
} else {
|
||||
out.write(",")
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Marshaler) marshalAny(out *errWriter, any proto.Message, indent string) error {
|
||||
// "If the Any contains a value that has a special JSON mapping,
|
||||
// it will be converted as follows: {"@type": xxx, "value": yyy}.
|
||||
// Otherwise, the value will be converted into a JSON object,
|
||||
// and the "@type" field will be inserted to indicate the actual data type."
|
||||
v := reflect.ValueOf(any).Elem()
|
||||
turl := v.Field(0).String()
|
||||
val := v.Field(1).Bytes()
|
||||
|
||||
// Only the part of type_url after the last slash is relevant.
|
||||
mname := turl
|
||||
if slash := strings.LastIndex(mname, "/"); slash >= 0 {
|
||||
mname = mname[slash+1:]
|
||||
}
|
||||
mt := proto.MessageType(mname)
|
||||
if mt == nil {
|
||||
return fmt.Errorf("unknown message type %q", mname)
|
||||
}
|
||||
msg := reflect.New(mt.Elem()).Interface().(proto.Message)
|
||||
if err := proto.Unmarshal(val, msg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, ok := msg.(wkt); ok {
|
||||
out.write("{")
|
||||
if m.Indent != "" {
|
||||
out.write("\n")
|
||||
}
|
||||
if err := m.marshalTypeURL(out, indent, turl); err != nil {
|
||||
return err
|
||||
}
|
||||
m.writeSep(out)
|
||||
if m.Indent != "" {
|
||||
out.write(indent)
|
||||
out.write(m.Indent)
|
||||
out.write(`"value": `)
|
||||
} else {
|
||||
out.write(`"value":`)
|
||||
}
|
||||
if err := m.marshalObject(out, msg, indent+m.Indent, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
if m.Indent != "" {
|
||||
out.write("\n")
|
||||
out.write(indent)
|
||||
}
|
||||
out.write("}")
|
||||
return out.err
|
||||
}
|
||||
|
||||
return m.marshalObject(out, msg, indent, turl)
|
||||
}
|
||||
|
||||
func (m *Marshaler) marshalTypeURL(out *errWriter, indent, typeURL string) error {
|
||||
if m.Indent != "" {
|
||||
out.write(indent)
|
||||
out.write(m.Indent)
|
||||
}
|
||||
out.write(`"@type":`)
|
||||
if m.Indent != "" {
|
||||
out.write(" ")
|
||||
}
|
||||
b, err := json.Marshal(typeURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
out.write(string(b))
|
||||
return out.err
|
||||
}
|
||||
|
||||
// marshalField writes field description and value to the Writer.
|
||||
func (m *Marshaler) marshalField(out *errWriter, prop *proto.Properties, v reflect.Value, indent string) error {
|
||||
if m.Indent != "" {
|
||||
out.write(indent)
|
||||
out.write(m.Indent)
|
||||
}
|
||||
out.write(`"`)
|
||||
out.write(prop.JSONName)
|
||||
out.write(`":`)
|
||||
if m.Indent != "" {
|
||||
out.write(" ")
|
||||
}
|
||||
if err := m.marshalValue(out, prop, v, indent); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// marshalValue writes the value to the Writer.
|
||||
func (m *Marshaler) marshalValue(out *errWriter, prop *proto.Properties, v reflect.Value, indent string) error {
|
||||
|
||||
var err error
|
||||
v = reflect.Indirect(v)
|
||||
|
||||
// Handle repeated elements.
|
||||
if v.Kind() == reflect.Slice && v.Type().Elem().Kind() != reflect.Uint8 {
|
||||
out.write("[")
|
||||
comma := ""
|
||||
for i := 0; i < v.Len(); i++ {
|
||||
sliceVal := v.Index(i)
|
||||
out.write(comma)
|
||||
if m.Indent != "" {
|
||||
out.write("\n")
|
||||
out.write(indent)
|
||||
out.write(m.Indent)
|
||||
out.write(m.Indent)
|
||||
}
|
||||
if err := m.marshalValue(out, prop, sliceVal, indent+m.Indent); err != nil {
|
||||
return err
|
||||
}
|
||||
comma = ","
|
||||
}
|
||||
if m.Indent != "" {
|
||||
out.write("\n")
|
||||
out.write(indent)
|
||||
out.write(m.Indent)
|
||||
}
|
||||
out.write("]")
|
||||
return out.err
|
||||
}
|
||||
|
||||
// Handle well-known types.
|
||||
// Most are handled up in marshalObject (because 99% are messages).
|
||||
type wkt interface {
|
||||
XXX_WellKnownType() string
|
||||
}
|
||||
if wkt, ok := v.Interface().(wkt); ok {
|
||||
switch wkt.XXX_WellKnownType() {
|
||||
case "NullValue":
|
||||
out.write("null")
|
||||
return out.err
|
||||
}
|
||||
}
|
||||
|
||||
// Handle enumerations.
|
||||
if !m.EnumsAsInts && prop.Enum != "" {
|
||||
// Unknown enum values will are stringified by the proto library as their
|
||||
// value. Such values should _not_ be quoted or they will be interpreted
|
||||
// as an enum string instead of their value.
|
||||
enumStr := v.Interface().(fmt.Stringer).String()
|
||||
var valStr string
|
||||
if v.Kind() == reflect.Ptr {
|
||||
valStr = strconv.Itoa(int(v.Elem().Int()))
|
||||
} else {
|
||||
valStr = strconv.Itoa(int(v.Int()))
|
||||
}
|
||||
isKnownEnum := enumStr != valStr
|
||||
if isKnownEnum {
|
||||
out.write(`"`)
|
||||
}
|
||||
out.write(enumStr)
|
||||
if isKnownEnum {
|
||||
out.write(`"`)
|
||||
}
|
||||
return out.err
|
||||
}
|
||||
|
||||
// Handle nested messages.
|
||||
if v.Kind() == reflect.Struct {
|
||||
return m.marshalObject(out, v.Addr().Interface().(proto.Message), indent+m.Indent, "")
|
||||
}
|
||||
|
||||
// Handle maps.
|
||||
// Since Go randomizes map iteration, we sort keys for stable output.
|
||||
if v.Kind() == reflect.Map {
|
||||
out.write(`{`)
|
||||
keys := v.MapKeys()
|
||||
sort.Sort(mapKeys(keys))
|
||||
for i, k := range keys {
|
||||
if i > 0 {
|
||||
out.write(`,`)
|
||||
}
|
||||
if m.Indent != "" {
|
||||
out.write("\n")
|
||||
out.write(indent)
|
||||
out.write(m.Indent)
|
||||
out.write(m.Indent)
|
||||
}
|
||||
|
||||
b, err := json.Marshal(k.Interface())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s := string(b)
|
||||
|
||||
// If the JSON is not a string value, encode it again to make it one.
|
||||
if !strings.HasPrefix(s, `"`) {
|
||||
b, err := json.Marshal(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s = string(b)
|
||||
}
|
||||
|
||||
out.write(s)
|
||||
out.write(`:`)
|
||||
if m.Indent != "" {
|
||||
out.write(` `)
|
||||
}
|
||||
|
||||
if err := m.marshalValue(out, prop, v.MapIndex(k), indent+m.Indent); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if m.Indent != "" {
|
||||
out.write("\n")
|
||||
out.write(indent)
|
||||
out.write(m.Indent)
|
||||
}
|
||||
out.write(`}`)
|
||||
return out.err
|
||||
}
|
||||
|
||||
// Default handling defers to the encoding/json library.
|
||||
b, err := json.Marshal(v.Interface())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
needToQuote := string(b[0]) != `"` && (v.Kind() == reflect.Int64 || v.Kind() == reflect.Uint64)
|
||||
if needToQuote {
|
||||
out.write(`"`)
|
||||
}
|
||||
out.write(string(b))
|
||||
if needToQuote {
|
||||
out.write(`"`)
|
||||
}
|
||||
return out.err
|
||||
}
|
||||
|
||||
// Unmarshaler is a configurable object for converting from a JSON
|
||||
// representation to a protocol buffer object.
|
||||
type Unmarshaler struct {
|
||||
// Whether to allow messages to contain unknown fields, as opposed to
|
||||
// failing to unmarshal.
|
||||
AllowUnknownFields bool
|
||||
}
|
||||
|
||||
// UnmarshalNext unmarshals the next protocol buffer from a JSON object stream.
|
||||
// This function is lenient and will decode any options permutations of the
|
||||
// related Marshaler.
|
||||
func (u *Unmarshaler) UnmarshalNext(dec *json.Decoder, pb proto.Message) error {
|
||||
inputValue := json.RawMessage{}
|
||||
if err := dec.Decode(&inputValue); err != nil {
|
||||
return err
|
||||
}
|
||||
return u.unmarshalValue(reflect.ValueOf(pb).Elem(), inputValue, nil)
|
||||
}
|
||||
|
||||
// Unmarshal unmarshals a JSON object stream into a protocol
|
||||
// buffer. This function is lenient and will decode any options
|
||||
// permutations of the related Marshaler.
|
||||
func (u *Unmarshaler) Unmarshal(r io.Reader, pb proto.Message) error {
|
||||
dec := json.NewDecoder(r)
|
||||
return u.UnmarshalNext(dec, pb)
|
||||
}
|
||||
|
||||
// UnmarshalNext unmarshals the next protocol buffer from a JSON object stream.
|
||||
// This function is lenient and will decode any options permutations of the
|
||||
// related Marshaler.
|
||||
func UnmarshalNext(dec *json.Decoder, pb proto.Message) error {
|
||||
return new(Unmarshaler).UnmarshalNext(dec, pb)
|
||||
}
|
||||
|
||||
// Unmarshal unmarshals a JSON object stream into a protocol
|
||||
// buffer. This function is lenient and will decode any options
|
||||
// permutations of the related Marshaler.
|
||||
func Unmarshal(r io.Reader, pb proto.Message) error {
|
||||
return new(Unmarshaler).Unmarshal(r, pb)
|
||||
}
|
||||
|
||||
// UnmarshalString will populate the fields of a protocol buffer based
|
||||
// on a JSON string. This function is lenient and will decode any options
|
||||
// permutations of the related Marshaler.
|
||||
func UnmarshalString(str string, pb proto.Message) error {
|
||||
return new(Unmarshaler).Unmarshal(strings.NewReader(str), pb)
|
||||
}
|
||||
|
||||
// unmarshalValue converts/copies a value into the target.
|
||||
// prop may be nil.
|
||||
func (u *Unmarshaler) unmarshalValue(target reflect.Value, inputValue json.RawMessage, prop *proto.Properties) error {
|
||||
targetType := target.Type()
|
||||
|
||||
// Allocate memory for pointer fields.
|
||||
if targetType.Kind() == reflect.Ptr {
|
||||
target.Set(reflect.New(targetType.Elem()))
|
||||
return u.unmarshalValue(target.Elem(), inputValue, prop)
|
||||
}
|
||||
|
||||
// Handle well-known types.
|
||||
type wkt interface {
|
||||
XXX_WellKnownType() string
|
||||
}
|
||||
if wkt, ok := target.Addr().Interface().(wkt); ok {
|
||||
switch wkt.XXX_WellKnownType() {
|
||||
case "DoubleValue", "FloatValue", "Int64Value", "UInt64Value",
|
||||
"Int32Value", "UInt32Value", "BoolValue", "StringValue", "BytesValue":
|
||||
// "Wrappers use the same representation in JSON
|
||||
// as the wrapped primitive type, except that null is allowed."
|
||||
// encoding/json will turn JSON `null` into Go `nil`,
|
||||
// so we don't have to do any extra work.
|
||||
return u.unmarshalValue(target.Field(0), inputValue, prop)
|
||||
case "Any":
|
||||
return fmt.Errorf("unmarshaling Any not supported yet")
|
||||
case "Duration":
|
||||
ivStr := string(inputValue)
|
||||
if ivStr == "null" {
|
||||
target.Field(0).SetInt(0)
|
||||
target.Field(1).SetInt(0)
|
||||
return nil
|
||||
}
|
||||
|
||||
unq, err := strconv.Unquote(ivStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
d, err := time.ParseDuration(unq)
|
||||
if err != nil {
|
||||
return fmt.Errorf("bad Duration: %v", err)
|
||||
}
|
||||
ns := d.Nanoseconds()
|
||||
s := ns / 1e9
|
||||
ns %= 1e9
|
||||
target.Field(0).SetInt(s)
|
||||
target.Field(1).SetInt(ns)
|
||||
return nil
|
||||
case "Timestamp":
|
||||
ivStr := string(inputValue)
|
||||
if ivStr == "null" {
|
||||
target.Field(0).SetInt(0)
|
||||
target.Field(1).SetInt(0)
|
||||
return nil
|
||||
}
|
||||
|
||||
unq, err := strconv.Unquote(ivStr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
t, err := time.Parse(time.RFC3339Nano, unq)
|
||||
if err != nil {
|
||||
return fmt.Errorf("bad Timestamp: %v", err)
|
||||
}
|
||||
target.Field(0).SetInt(int64(t.Unix()))
|
||||
target.Field(1).SetInt(int64(t.Nanosecond()))
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Handle enums, which have an underlying type of int32,
|
||||
// and may appear as strings.
|
||||
// The case of an enum appearing as a number is handled
|
||||
// at the bottom of this function.
|
||||
if inputValue[0] == '"' && prop != nil && prop.Enum != "" {
|
||||
vmap := proto.EnumValueMap(prop.Enum)
|
||||
// Don't need to do unquoting; valid enum names
|
||||
// are from a limited character set.
|
||||
s := inputValue[1 : len(inputValue)-1]
|
||||
n, ok := vmap[string(s)]
|
||||
if !ok {
|
||||
return fmt.Errorf("unknown value %q for enum %s", s, prop.Enum)
|
||||
}
|
||||
if target.Kind() == reflect.Ptr { // proto2
|
||||
target.Set(reflect.New(targetType.Elem()))
|
||||
target = target.Elem()
|
||||
}
|
||||
target.SetInt(int64(n))
|
||||
return nil
|
||||
}
|
||||
|
||||
// Handle nested messages.
|
||||
if targetType.Kind() == reflect.Struct {
|
||||
var jsonFields map[string]json.RawMessage
|
||||
if err := json.Unmarshal(inputValue, &jsonFields); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
consumeField := func(prop *proto.Properties) (json.RawMessage, bool) {
|
||||
// Be liberal in what names we accept; both orig_name and camelName are okay.
|
||||
fieldNames := acceptedJSONFieldNames(prop)
|
||||
|
||||
vOrig, okOrig := jsonFields[fieldNames.orig]
|
||||
vCamel, okCamel := jsonFields[fieldNames.camel]
|
||||
if !okOrig && !okCamel {
|
||||
return nil, false
|
||||
}
|
||||
// If, for some reason, both are present in the data, favour the camelName.
|
||||
var raw json.RawMessage
|
||||
if okOrig {
|
||||
raw = vOrig
|
||||
delete(jsonFields, fieldNames.orig)
|
||||
}
|
||||
if okCamel {
|
||||
raw = vCamel
|
||||
delete(jsonFields, fieldNames.camel)
|
||||
}
|
||||
return raw, true
|
||||
}
|
||||
|
||||
sprops := proto.GetProperties(targetType)
|
||||
for i := 0; i < target.NumField(); i++ {
|
||||
ft := target.Type().Field(i)
|
||||
if strings.HasPrefix(ft.Name, "XXX_") {
|
||||
continue
|
||||
}
|
||||
|
||||
valueForField, ok := consumeField(sprops.Prop[i])
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if err := u.unmarshalValue(target.Field(i), valueForField, sprops.Prop[i]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// Check for any oneof fields.
|
||||
if len(jsonFields) > 0 {
|
||||
for _, oop := range sprops.OneofTypes {
|
||||
raw, ok := consumeField(oop.Prop)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
nv := reflect.New(oop.Type.Elem())
|
||||
target.Field(oop.Field).Set(nv)
|
||||
if err := u.unmarshalValue(nv.Elem().Field(0), raw, oop.Prop); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
if !u.AllowUnknownFields && len(jsonFields) > 0 {
|
||||
// Pick any field to be the scapegoat.
|
||||
var f string
|
||||
for fname := range jsonFields {
|
||||
f = fname
|
||||
break
|
||||
}
|
||||
return fmt.Errorf("unknown field %q in %v", f, targetType)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Handle arrays (which aren't encoded bytes)
|
||||
if targetType.Kind() == reflect.Slice && targetType.Elem().Kind() != reflect.Uint8 {
|
||||
var slc []json.RawMessage
|
||||
if err := json.Unmarshal(inputValue, &slc); err != nil {
|
||||
return err
|
||||
}
|
||||
len := len(slc)
|
||||
target.Set(reflect.MakeSlice(targetType, len, len))
|
||||
for i := 0; i < len; i++ {
|
||||
if err := u.unmarshalValue(target.Index(i), slc[i], prop); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Handle maps (whose keys are always strings)
|
||||
if targetType.Kind() == reflect.Map {
|
||||
var mp map[string]json.RawMessage
|
||||
if err := json.Unmarshal(inputValue, &mp); err != nil {
|
||||
return err
|
||||
}
|
||||
target.Set(reflect.MakeMap(targetType))
|
||||
var keyprop, valprop *proto.Properties
|
||||
if prop != nil {
|
||||
// These could still be nil if the protobuf metadata is broken somehow.
|
||||
// TODO: This won't work because the fields are unexported.
|
||||
// We should probably just reparse them.
|
||||
//keyprop, valprop = prop.mkeyprop, prop.mvalprop
|
||||
}
|
||||
for ks, raw := range mp {
|
||||
// Unmarshal map key. The core json library already decoded the key into a
|
||||
// string, so we handle that specially. Other types were quoted post-serialization.
|
||||
var k reflect.Value
|
||||
if targetType.Key().Kind() == reflect.String {
|
||||
k = reflect.ValueOf(ks)
|
||||
} else {
|
||||
k = reflect.New(targetType.Key()).Elem()
|
||||
if err := u.unmarshalValue(k, json.RawMessage(ks), keyprop); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Unmarshal map value.
|
||||
v := reflect.New(targetType.Elem()).Elem()
|
||||
if err := u.unmarshalValue(v, raw, valprop); err != nil {
|
||||
return err
|
||||
}
|
||||
target.SetMapIndex(k, v)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// 64-bit integers can be encoded as strings. In this case we drop
|
||||
// the quotes and proceed as normal.
|
||||
isNum := targetType.Kind() == reflect.Int64 || targetType.Kind() == reflect.Uint64
|
||||
if isNum && strings.HasPrefix(string(inputValue), `"`) {
|
||||
inputValue = inputValue[1 : len(inputValue)-1]
|
||||
}
|
||||
|
||||
// Use the encoding/json for parsing other value types.
|
||||
return json.Unmarshal(inputValue, target.Addr().Interface())
|
||||
}
|
||||
|
||||
// jsonProperties returns parsed proto.Properties for the field and corrects JSONName attribute.
|
||||
func jsonProperties(f reflect.StructField, origName bool) *proto.Properties {
|
||||
var prop proto.Properties
|
||||
prop.Init(f.Type, f.Name, f.Tag.Get("protobuf"), &f)
|
||||
if origName || prop.JSONName == "" {
|
||||
prop.JSONName = prop.OrigName
|
||||
}
|
||||
return &prop
|
||||
}
|
||||
|
||||
type fieldNames struct {
|
||||
orig, camel string
|
||||
}
|
||||
|
||||
func acceptedJSONFieldNames(prop *proto.Properties) fieldNames {
|
||||
opts := fieldNames{orig: prop.OrigName, camel: prop.OrigName}
|
||||
if prop.JSONName != "" {
|
||||
opts.camel = prop.JSONName
|
||||
}
|
||||
return opts
|
||||
}
|
||||
|
||||
// Writer wrapper inspired by https://blog.golang.org/errors-are-values
|
||||
type errWriter struct {
|
||||
writer io.Writer
|
||||
err error
|
||||
}
|
||||
|
||||
func (w *errWriter) write(str string) {
|
||||
if w.err != nil {
|
||||
return
|
||||
}
|
||||
_, w.err = w.writer.Write([]byte(str))
|
||||
}
|
||||
|
||||
// Map fields may have key types of non-float scalars, strings and enums.
|
||||
// The easiest way to sort them in some deterministic order is to use fmt.
|
||||
// If this turns out to be inefficient we can always consider other options,
|
||||
// such as doing a Schwartzian transform.
|
||||
//
|
||||
// Numeric keys are sorted in numeric order per
|
||||
// https://developers.google.com/protocol-buffers/docs/proto#maps.
|
||||
type mapKeys []reflect.Value
|
||||
|
||||
func (s mapKeys) Len() int { return len(s) }
|
||||
func (s mapKeys) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
||||
func (s mapKeys) Less(i, j int) bool {
|
||||
if k := s[i].Kind(); k == s[j].Kind() {
|
||||
switch k {
|
||||
case reflect.Int32, reflect.Int64:
|
||||
return s[i].Int() < s[j].Int()
|
||||
case reflect.Uint32, reflect.Uint64:
|
||||
return s[i].Uint() < s[j].Uint()
|
||||
}
|
||||
}
|
||||
return fmt.Sprint(s[i].Interface()) < fmt.Sprint(s[j].Interface())
|
||||
}
|
563
vendor/github.com/golang/protobuf/jsonpb/jsonpb_test.go
generated
vendored
Normal file
563
vendor/github.com/golang/protobuf/jsonpb/jsonpb_test.go
generated
vendored
Normal file
@@ -0,0 +1,563 @@
|
||||
// Go support for Protocol Buffers - Google's data interchange format
|
||||
//
|
||||
// Copyright 2015 The Go Authors. All rights reserved.
|
||||
// https://github.com/golang/protobuf
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package jsonpb
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
|
||||
pb "github.com/golang/protobuf/jsonpb/jsonpb_test_proto"
|
||||
proto3pb "github.com/golang/protobuf/proto/proto3_proto"
|
||||
anypb "github.com/golang/protobuf/ptypes/any"
|
||||
durpb "github.com/golang/protobuf/ptypes/duration"
|
||||
stpb "github.com/golang/protobuf/ptypes/struct"
|
||||
tspb "github.com/golang/protobuf/ptypes/timestamp"
|
||||
wpb "github.com/golang/protobuf/ptypes/wrappers"
|
||||
)
|
||||
|
||||
var (
|
||||
marshaler = Marshaler{}
|
||||
|
||||
marshalerAllOptions = Marshaler{
|
||||
Indent: " ",
|
||||
}
|
||||
|
||||
simpleObject = &pb.Simple{
|
||||
OInt32: proto.Int32(-32),
|
||||
OInt64: proto.Int64(-6400000000),
|
||||
OUint32: proto.Uint32(32),
|
||||
OUint64: proto.Uint64(6400000000),
|
||||
OSint32: proto.Int32(-13),
|
||||
OSint64: proto.Int64(-2600000000),
|
||||
OFloat: proto.Float32(3.14),
|
||||
ODouble: proto.Float64(6.02214179e23),
|
||||
OBool: proto.Bool(true),
|
||||
OString: proto.String("hello \"there\""),
|
||||
OBytes: []byte("beep boop"),
|
||||
}
|
||||
|
||||
simpleObjectJSON = `{` +
|
||||
`"oBool":true,` +
|
||||
`"oInt32":-32,` +
|
||||
`"oInt64":"-6400000000",` +
|
||||
`"oUint32":32,` +
|
||||
`"oUint64":"6400000000",` +
|
||||
`"oSint32":-13,` +
|
||||
`"oSint64":"-2600000000",` +
|
||||
`"oFloat":3.14,` +
|
||||
`"oDouble":6.02214179e+23,` +
|
||||
`"oString":"hello \"there\"",` +
|
||||
`"oBytes":"YmVlcCBib29w"` +
|
||||
`}`
|
||||
|
||||
simpleObjectPrettyJSON = `{
|
||||
"oBool": true,
|
||||
"oInt32": -32,
|
||||
"oInt64": "-6400000000",
|
||||
"oUint32": 32,
|
||||
"oUint64": "6400000000",
|
||||
"oSint32": -13,
|
||||
"oSint64": "-2600000000",
|
||||
"oFloat": 3.14,
|
||||
"oDouble": 6.02214179e+23,
|
||||
"oString": "hello \"there\"",
|
||||
"oBytes": "YmVlcCBib29w"
|
||||
}`
|
||||
|
||||
repeatsObject = &pb.Repeats{
|
||||
RBool: []bool{true, false, true},
|
||||
RInt32: []int32{-3, -4, -5},
|
||||
RInt64: []int64{-123456789, -987654321},
|
||||
RUint32: []uint32{1, 2, 3},
|
||||
RUint64: []uint64{6789012345, 3456789012},
|
||||
RSint32: []int32{-1, -2, -3},
|
||||
RSint64: []int64{-6789012345, -3456789012},
|
||||
RFloat: []float32{3.14, 6.28},
|
||||
RDouble: []float64{299792458 * 1e20, 6.62606957e-34},
|
||||
RString: []string{"happy", "days"},
|
||||
RBytes: [][]byte{[]byte("skittles"), []byte("m&m's")},
|
||||
}
|
||||
|
||||
repeatsObjectJSON = `{` +
|
||||
`"rBool":[true,false,true],` +
|
||||
`"rInt32":[-3,-4,-5],` +
|
||||
`"rInt64":["-123456789","-987654321"],` +
|
||||
`"rUint32":[1,2,3],` +
|
||||
`"rUint64":["6789012345","3456789012"],` +
|
||||
`"rSint32":[-1,-2,-3],` +
|
||||
`"rSint64":["-6789012345","-3456789012"],` +
|
||||
`"rFloat":[3.14,6.28],` +
|
||||
`"rDouble":[2.99792458e+28,6.62606957e-34],` +
|
||||
`"rString":["happy","days"],` +
|
||||
`"rBytes":["c2tpdHRsZXM=","bSZtJ3M="]` +
|
||||
`}`
|
||||
|
||||
repeatsObjectPrettyJSON = `{
|
||||
"rBool": [
|
||||
true,
|
||||
false,
|
||||
true
|
||||
],
|
||||
"rInt32": [
|
||||
-3,
|
||||
-4,
|
||||
-5
|
||||
],
|
||||
"rInt64": [
|
||||
"-123456789",
|
||||
"-987654321"
|
||||
],
|
||||
"rUint32": [
|
||||
1,
|
||||
2,
|
||||
3
|
||||
],
|
||||
"rUint64": [
|
||||
"6789012345",
|
||||
"3456789012"
|
||||
],
|
||||
"rSint32": [
|
||||
-1,
|
||||
-2,
|
||||
-3
|
||||
],
|
||||
"rSint64": [
|
||||
"-6789012345",
|
||||
"-3456789012"
|
||||
],
|
||||
"rFloat": [
|
||||
3.14,
|
||||
6.28
|
||||
],
|
||||
"rDouble": [
|
||||
2.99792458e+28,
|
||||
6.62606957e-34
|
||||
],
|
||||
"rString": [
|
||||
"happy",
|
||||
"days"
|
||||
],
|
||||
"rBytes": [
|
||||
"c2tpdHRsZXM=",
|
||||
"bSZtJ3M="
|
||||
]
|
||||
}`
|
||||
|
||||
innerSimple = &pb.Simple{OInt32: proto.Int32(-32)}
|
||||
innerSimple2 = &pb.Simple{OInt64: proto.Int64(25)}
|
||||
innerRepeats = &pb.Repeats{RString: []string{"roses", "red"}}
|
||||
innerRepeats2 = &pb.Repeats{RString: []string{"violets", "blue"}}
|
||||
complexObject = &pb.Widget{
|
||||
Color: pb.Widget_GREEN.Enum(),
|
||||
RColor: []pb.Widget_Color{pb.Widget_RED, pb.Widget_GREEN, pb.Widget_BLUE},
|
||||
Simple: innerSimple,
|
||||
RSimple: []*pb.Simple{innerSimple, innerSimple2},
|
||||
Repeats: innerRepeats,
|
||||
RRepeats: []*pb.Repeats{innerRepeats, innerRepeats2},
|
||||
}
|
||||
|
||||
complexObjectJSON = `{"color":"GREEN",` +
|
||||
`"rColor":["RED","GREEN","BLUE"],` +
|
||||
`"simple":{"oInt32":-32},` +
|
||||
`"rSimple":[{"oInt32":-32},{"oInt64":"25"}],` +
|
||||
`"repeats":{"rString":["roses","red"]},` +
|
||||
`"rRepeats":[{"rString":["roses","red"]},{"rString":["violets","blue"]}]` +
|
||||
`}`
|
||||
|
||||
complexObjectPrettyJSON = `{
|
||||
"color": "GREEN",
|
||||
"rColor": [
|
||||
"RED",
|
||||
"GREEN",
|
||||
"BLUE"
|
||||
],
|
||||
"simple": {
|
||||
"oInt32": -32
|
||||
},
|
||||
"rSimple": [
|
||||
{
|
||||
"oInt32": -32
|
||||
},
|
||||
{
|
||||
"oInt64": "25"
|
||||
}
|
||||
],
|
||||
"repeats": {
|
||||
"rString": [
|
||||
"roses",
|
||||
"red"
|
||||
]
|
||||
},
|
||||
"rRepeats": [
|
||||
{
|
||||
"rString": [
|
||||
"roses",
|
||||
"red"
|
||||
]
|
||||
},
|
||||
{
|
||||
"rString": [
|
||||
"violets",
|
||||
"blue"
|
||||
]
|
||||
}
|
||||
]
|
||||
}`
|
||||
|
||||
colorPrettyJSON = `{
|
||||
"color": 2
|
||||
}`
|
||||
|
||||
colorListPrettyJSON = `{
|
||||
"color": 1000,
|
||||
"rColor": [
|
||||
"RED"
|
||||
]
|
||||
}`
|
||||
|
||||
nummyPrettyJSON = `{
|
||||
"nummy": {
|
||||
"1": 2,
|
||||
"3": 4
|
||||
}
|
||||
}`
|
||||
|
||||
objjyPrettyJSON = `{
|
||||
"objjy": {
|
||||
"1": {
|
||||
"dub": 1
|
||||
}
|
||||
}
|
||||
}`
|
||||
realNumber = &pb.Real{Value: proto.Float64(3.14159265359)}
|
||||
realNumberName = "Pi"
|
||||
complexNumber = &pb.Complex{Imaginary: proto.Float64(0.5772156649)}
|
||||
realNumberJSON = `{` +
|
||||
`"value":3.14159265359,` +
|
||||
`"[jsonpb.Complex.real_extension]":{"imaginary":0.5772156649},` +
|
||||
`"[jsonpb.name]":"Pi"` +
|
||||
`}`
|
||||
|
||||
anySimple = &pb.KnownTypes{
|
||||
An: &anypb.Any{
|
||||
TypeUrl: "something.example.com/jsonpb.Simple",
|
||||
Value: []byte{
|
||||
// &pb.Simple{OBool:true}
|
||||
1 << 3, 1,
|
||||
},
|
||||
},
|
||||
}
|
||||
anySimpleJSON = `{"an":{"@type":"something.example.com/jsonpb.Simple","oBool":true}}`
|
||||
anySimplePrettyJSON = `{
|
||||
"an": {
|
||||
"@type": "something.example.com/jsonpb.Simple",
|
||||
"oBool": true
|
||||
}
|
||||
}`
|
||||
|
||||
anyWellKnown = &pb.KnownTypes{
|
||||
An: &anypb.Any{
|
||||
TypeUrl: "type.googleapis.com/google.protobuf.Duration",
|
||||
Value: []byte{
|
||||
// &durpb.Duration{Seconds: 1, Nanos: 212000000 }
|
||||
1 << 3, 1, // seconds
|
||||
2 << 3, 0x80, 0xba, 0x8b, 0x65, // nanos
|
||||
},
|
||||
},
|
||||
}
|
||||
anyWellKnownJSON = `{"an":{"@type":"type.googleapis.com/google.protobuf.Duration","value":"1.212s"}}`
|
||||
anyWellKnownPrettyJSON = `{
|
||||
"an": {
|
||||
"@type": "type.googleapis.com/google.protobuf.Duration",
|
||||
"value": "1.212s"
|
||||
}
|
||||
}`
|
||||
)
|
||||
|
||||
func init() {
|
||||
if err := proto.SetExtension(realNumber, pb.E_Name, &realNumberName); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := proto.SetExtension(realNumber, pb.E_Complex_RealExtension, complexNumber); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
var marshalingTests = []struct {
|
||||
desc string
|
||||
marshaler Marshaler
|
||||
pb proto.Message
|
||||
json string
|
||||
}{
|
||||
{"simple flat object", marshaler, simpleObject, simpleObjectJSON},
|
||||
{"simple pretty object", marshalerAllOptions, simpleObject, simpleObjectPrettyJSON},
|
||||
{"repeated fields flat object", marshaler, repeatsObject, repeatsObjectJSON},
|
||||
{"repeated fields pretty object", marshalerAllOptions, repeatsObject, repeatsObjectPrettyJSON},
|
||||
{"nested message/enum flat object", marshaler, complexObject, complexObjectJSON},
|
||||
{"nested message/enum pretty object", marshalerAllOptions, complexObject, complexObjectPrettyJSON},
|
||||
{"enum-string flat object", Marshaler{},
|
||||
&pb.Widget{Color: pb.Widget_BLUE.Enum()}, `{"color":"BLUE"}`},
|
||||
{"enum-value pretty object", Marshaler{EnumsAsInts: true, Indent: " "},
|
||||
&pb.Widget{Color: pb.Widget_BLUE.Enum()}, colorPrettyJSON},
|
||||
{"unknown enum value object", marshalerAllOptions,
|
||||
&pb.Widget{Color: pb.Widget_Color(1000).Enum(), RColor: []pb.Widget_Color{pb.Widget_RED}}, colorListPrettyJSON},
|
||||
{"repeated proto3 enum", Marshaler{},
|
||||
&proto3pb.Message{RFunny: []proto3pb.Message_Humour{
|
||||
proto3pb.Message_PUNS,
|
||||
proto3pb.Message_SLAPSTICK,
|
||||
}},
|
||||
`{"rFunny":["PUNS","SLAPSTICK"]}`},
|
||||
{"repeated proto3 enum as int", Marshaler{EnumsAsInts: true},
|
||||
&proto3pb.Message{RFunny: []proto3pb.Message_Humour{
|
||||
proto3pb.Message_PUNS,
|
||||
proto3pb.Message_SLAPSTICK,
|
||||
}},
|
||||
`{"rFunny":[1,2]}`},
|
||||
{"empty value", marshaler, &pb.Simple3{}, `{}`},
|
||||
{"empty value emitted", Marshaler{EmitDefaults: true}, &pb.Simple3{}, `{"dub":0}`},
|
||||
{"map<int64, int32>", marshaler, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}, `{"nummy":{"1":2,"3":4}}`},
|
||||
{"map<int64, int32>", marshalerAllOptions, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}, nummyPrettyJSON},
|
||||
{"map<string, string>", marshaler,
|
||||
&pb.Mappy{Strry: map[string]string{`"one"`: "two", "three": "four"}},
|
||||
`{"strry":{"\"one\"":"two","three":"four"}}`},
|
||||
{"map<int32, Object>", marshaler,
|
||||
&pb.Mappy{Objjy: map[int32]*pb.Simple3{1: &pb.Simple3{Dub: 1}}}, `{"objjy":{"1":{"dub":1}}}`},
|
||||
{"map<int32, Object>", marshalerAllOptions,
|
||||
&pb.Mappy{Objjy: map[int32]*pb.Simple3{1: &pb.Simple3{Dub: 1}}}, objjyPrettyJSON},
|
||||
{"map<int64, string>", marshaler, &pb.Mappy{Buggy: map[int64]string{1234: "yup"}},
|
||||
`{"buggy":{"1234":"yup"}}`},
|
||||
{"map<bool, bool>", marshaler, &pb.Mappy{Booly: map[bool]bool{false: true}}, `{"booly":{"false":true}}`},
|
||||
// TODO: This is broken.
|
||||
//{"map<string, enum>", marshaler, &pb.Mappy{Enumy: map[string]pb.Numeral{"XIV": pb.Numeral_ROMAN}}, `{"enumy":{"XIV":"ROMAN"}`},
|
||||
{"map<string, enum as int>", Marshaler{EnumsAsInts: true}, &pb.Mappy{Enumy: map[string]pb.Numeral{"XIV": pb.Numeral_ROMAN}}, `{"enumy":{"XIV":2}}`},
|
||||
{"map<int32, bool>", marshaler, &pb.Mappy{S32Booly: map[int32]bool{1: true, 3: false, 10: true, 12: false}}, `{"s32booly":{"1":true,"3":false,"10":true,"12":false}}`},
|
||||
{"map<int64, bool>", marshaler, &pb.Mappy{S64Booly: map[int64]bool{1: true, 3: false, 10: true, 12: false}}, `{"s64booly":{"1":true,"3":false,"10":true,"12":false}}`},
|
||||
{"map<uint32, bool>", marshaler, &pb.Mappy{U32Booly: map[uint32]bool{1: true, 3: false, 10: true, 12: false}}, `{"u32booly":{"1":true,"3":false,"10":true,"12":false}}`},
|
||||
{"map<uint64, bool>", marshaler, &pb.Mappy{U64Booly: map[uint64]bool{1: true, 3: false, 10: true, 12: false}}, `{"u64booly":{"1":true,"3":false,"10":true,"12":false}}`},
|
||||
{"proto2 map<int64, string>", marshaler, &pb.Maps{MInt64Str: map[int64]string{213: "cat"}},
|
||||
`{"mInt64Str":{"213":"cat"}}`},
|
||||
{"proto2 map<bool, Object>", marshaler,
|
||||
&pb.Maps{MBoolSimple: map[bool]*pb.Simple{true: &pb.Simple{OInt32: proto.Int32(1)}}},
|
||||
`{"mBoolSimple":{"true":{"oInt32":1}}}`},
|
||||
{"oneof, not set", marshaler, &pb.MsgWithOneof{}, `{}`},
|
||||
{"oneof, set", marshaler, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Title{"Grand Poobah"}}, `{"title":"Grand Poobah"}`},
|
||||
{"force orig_name", Marshaler{OrigName: true}, &pb.Simple{OInt32: proto.Int32(4)},
|
||||
`{"o_int32":4}`},
|
||||
{"proto2 extension", marshaler, realNumber, realNumberJSON},
|
||||
{"Any with message", marshaler, anySimple, anySimpleJSON},
|
||||
{"Any with message and indent", marshalerAllOptions, anySimple, anySimplePrettyJSON},
|
||||
{"Any with WKT", marshaler, anyWellKnown, anyWellKnownJSON},
|
||||
{"Any with WKT and indent", marshalerAllOptions, anyWellKnown, anyWellKnownPrettyJSON},
|
||||
{"Duration", marshaler, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: 3}}, `{"dur":"3.000s"}`},
|
||||
{"Struct", marshaler, &pb.KnownTypes{St: &stpb.Struct{
|
||||
Fields: map[string]*stpb.Value{
|
||||
"one": &stpb.Value{Kind: &stpb.Value_StringValue{"loneliest number"}},
|
||||
"two": &stpb.Value{Kind: &stpb.Value_NullValue{stpb.NullValue_NULL_VALUE}},
|
||||
},
|
||||
}}, `{"st":{"one":"loneliest number","two":null}}`},
|
||||
{"Timestamp", marshaler, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: 14e8, Nanos: 21e6}}, `{"ts":"2014-05-13T16:53:20.021Z"}`},
|
||||
|
||||
{"DoubleValue", marshaler, &pb.KnownTypes{Dbl: &wpb.DoubleValue{Value: 1.2}}, `{"dbl":1.2}`},
|
||||
{"FloatValue", marshaler, &pb.KnownTypes{Flt: &wpb.FloatValue{Value: 1.2}}, `{"flt":1.2}`},
|
||||
{"Int64Value", marshaler, &pb.KnownTypes{I64: &wpb.Int64Value{Value: -3}}, `{"i64":"-3"}`},
|
||||
{"UInt64Value", marshaler, &pb.KnownTypes{U64: &wpb.UInt64Value{Value: 3}}, `{"u64":"3"}`},
|
||||
{"Int32Value", marshaler, &pb.KnownTypes{I32: &wpb.Int32Value{Value: -4}}, `{"i32":-4}`},
|
||||
{"UInt32Value", marshaler, &pb.KnownTypes{U32: &wpb.UInt32Value{Value: 4}}, `{"u32":4}`},
|
||||
{"BoolValue", marshaler, &pb.KnownTypes{Bool: &wpb.BoolValue{Value: true}}, `{"bool":true}`},
|
||||
{"StringValue", marshaler, &pb.KnownTypes{Str: &wpb.StringValue{Value: "plush"}}, `{"str":"plush"}`},
|
||||
{"BytesValue", marshaler, &pb.KnownTypes{Bytes: &wpb.BytesValue{Value: []byte("wow")}}, `{"bytes":"d293"}`},
|
||||
}
|
||||
|
||||
func TestMarshaling(t *testing.T) {
|
||||
for _, tt := range marshalingTests {
|
||||
json, err := tt.marshaler.MarshalToString(tt.pb)
|
||||
if err != nil {
|
||||
t.Errorf("%s: marshaling error: %v", tt.desc, err)
|
||||
} else if tt.json != json {
|
||||
t.Errorf("%s: got [%v] want [%v]", tt.desc, json, tt.json)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var unmarshalingTests = []struct {
|
||||
desc string
|
||||
unmarshaler Unmarshaler
|
||||
json string
|
||||
pb proto.Message
|
||||
}{
|
||||
{"simple flat object", Unmarshaler{}, simpleObjectJSON, simpleObject},
|
||||
{"simple pretty object", Unmarshaler{}, simpleObjectPrettyJSON, simpleObject},
|
||||
{"repeated fields flat object", Unmarshaler{}, repeatsObjectJSON, repeatsObject},
|
||||
{"repeated fields pretty object", Unmarshaler{}, repeatsObjectPrettyJSON, repeatsObject},
|
||||
{"nested message/enum flat object", Unmarshaler{}, complexObjectJSON, complexObject},
|
||||
{"nested message/enum pretty object", Unmarshaler{}, complexObjectPrettyJSON, complexObject},
|
||||
{"enum-string object", Unmarshaler{}, `{"color":"BLUE"}`, &pb.Widget{Color: pb.Widget_BLUE.Enum()}},
|
||||
{"enum-value object", Unmarshaler{}, "{\n \"color\": 2\n}", &pb.Widget{Color: pb.Widget_BLUE.Enum()}},
|
||||
{"unknown field with allowed option", Unmarshaler{AllowUnknownFields: true}, `{"unknown": "foo"}`, new(pb.Simple)},
|
||||
{"proto3 enum string", Unmarshaler{}, `{"hilarity":"PUNS"}`, &proto3pb.Message{Hilarity: proto3pb.Message_PUNS}},
|
||||
{"proto3 enum value", Unmarshaler{}, `{"hilarity":1}`, &proto3pb.Message{Hilarity: proto3pb.Message_PUNS}},
|
||||
{"unknown enum value object",
|
||||
Unmarshaler{},
|
||||
"{\n \"color\": 1000,\n \"r_color\": [\n \"RED\"\n ]\n}",
|
||||
&pb.Widget{Color: pb.Widget_Color(1000).Enum(), RColor: []pb.Widget_Color{pb.Widget_RED}}},
|
||||
{"repeated proto3 enum", Unmarshaler{}, `{"rFunny":["PUNS","SLAPSTICK"]}`,
|
||||
&proto3pb.Message{RFunny: []proto3pb.Message_Humour{
|
||||
proto3pb.Message_PUNS,
|
||||
proto3pb.Message_SLAPSTICK,
|
||||
}}},
|
||||
{"repeated proto3 enum as int", Unmarshaler{}, `{"rFunny":[1,2]}`,
|
||||
&proto3pb.Message{RFunny: []proto3pb.Message_Humour{
|
||||
proto3pb.Message_PUNS,
|
||||
proto3pb.Message_SLAPSTICK,
|
||||
}}},
|
||||
{"repeated proto3 enum as mix of strings and ints", Unmarshaler{}, `{"rFunny":["PUNS",2]}`,
|
||||
&proto3pb.Message{RFunny: []proto3pb.Message_Humour{
|
||||
proto3pb.Message_PUNS,
|
||||
proto3pb.Message_SLAPSTICK,
|
||||
}}},
|
||||
{"unquoted int64 object", Unmarshaler{}, `{"oInt64":-314}`, &pb.Simple{OInt64: proto.Int64(-314)}},
|
||||
{"unquoted uint64 object", Unmarshaler{}, `{"oUint64":123}`, &pb.Simple{OUint64: proto.Uint64(123)}},
|
||||
{"map<int64, int32>", Unmarshaler{}, `{"nummy":{"1":2,"3":4}}`, &pb.Mappy{Nummy: map[int64]int32{1: 2, 3: 4}}},
|
||||
{"map<string, string>", Unmarshaler{}, `{"strry":{"\"one\"":"two","three":"four"}}`, &pb.Mappy{Strry: map[string]string{`"one"`: "two", "three": "four"}}},
|
||||
{"map<int32, Object>", Unmarshaler{}, `{"objjy":{"1":{"dub":1}}}`, &pb.Mappy{Objjy: map[int32]*pb.Simple3{1: &pb.Simple3{Dub: 1}}}},
|
||||
// TODO: This is broken.
|
||||
//{"map<string, enum>", Unmarshaler{}, `{"enumy":{"XIV":"ROMAN"}`, &pb.Mappy{Enumy: map[string]pb.Numeral{"XIV": pb.Numeral_ROMAN}}},
|
||||
{"map<string, enum as int>", Unmarshaler{}, `{"enumy":{"XIV":2}}`, &pb.Mappy{Enumy: map[string]pb.Numeral{"XIV": pb.Numeral_ROMAN}}},
|
||||
{"oneof", Unmarshaler{}, `{"salary":31000}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Salary{31000}}},
|
||||
{"oneof spec name", Unmarshaler{}, `{"Country":"Australia"}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Country{"Australia"}}},
|
||||
{"oneof orig_name", Unmarshaler{}, `{"Country":"Australia"}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_Country{"Australia"}}},
|
||||
{"oneof spec name2", Unmarshaler{}, `{"homeAddress":"Australia"}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_HomeAddress{"Australia"}}},
|
||||
{"oneof orig_name2", Unmarshaler{}, `{"home_address":"Australia"}`, &pb.MsgWithOneof{Union: &pb.MsgWithOneof_HomeAddress{"Australia"}}},
|
||||
{"orig_name input", Unmarshaler{}, `{"o_bool":true}`, &pb.Simple{OBool: proto.Bool(true)}},
|
||||
{"camelName input", Unmarshaler{}, `{"oBool":true}`, &pb.Simple{OBool: proto.Bool(true)}},
|
||||
|
||||
{"Duration", Unmarshaler{}, `{"dur":"3.000s"}`, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: 3}}},
|
||||
{"null Duration", Unmarshaler{}, `{"dur":null}`, &pb.KnownTypes{Dur: &durpb.Duration{Seconds: 0}}},
|
||||
{"Timestamp", Unmarshaler{}, `{"ts":"2014-05-13T16:53:20.021Z"}`, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: 14e8, Nanos: 21e6}}},
|
||||
{"PreEpochTimestamp", Unmarshaler{}, `{"ts":"1969-12-31T23:59:58.999999995Z"}`, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: -2, Nanos: 999999995}}},
|
||||
{"ZeroTimeTimestamp", Unmarshaler{}, `{"ts":"0001-01-01T00:00:00Z"}`, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: -62135596800, Nanos: 0}}},
|
||||
{"null Timestamp", Unmarshaler{}, `{"ts":null}`, &pb.KnownTypes{Ts: &tspb.Timestamp{Seconds: 0, Nanos: 0}}},
|
||||
|
||||
{"DoubleValue", Unmarshaler{}, `{"dbl":1.2}`, &pb.KnownTypes{Dbl: &wpb.DoubleValue{Value: 1.2}}},
|
||||
{"FloatValue", Unmarshaler{}, `{"flt":1.2}`, &pb.KnownTypes{Flt: &wpb.FloatValue{Value: 1.2}}},
|
||||
{"Int64Value", Unmarshaler{}, `{"i64":"-3"}`, &pb.KnownTypes{I64: &wpb.Int64Value{Value: -3}}},
|
||||
{"UInt64Value", Unmarshaler{}, `{"u64":"3"}`, &pb.KnownTypes{U64: &wpb.UInt64Value{Value: 3}}},
|
||||
{"Int32Value", Unmarshaler{}, `{"i32":-4}`, &pb.KnownTypes{I32: &wpb.Int32Value{Value: -4}}},
|
||||
{"UInt32Value", Unmarshaler{}, `{"u32":4}`, &pb.KnownTypes{U32: &wpb.UInt32Value{Value: 4}}},
|
||||
{"BoolValue", Unmarshaler{}, `{"bool":true}`, &pb.KnownTypes{Bool: &wpb.BoolValue{Value: true}}},
|
||||
{"StringValue", Unmarshaler{}, `{"str":"plush"}`, &pb.KnownTypes{Str: &wpb.StringValue{Value: "plush"}}},
|
||||
{"BytesValue", Unmarshaler{}, `{"bytes":"d293"}`, &pb.KnownTypes{Bytes: &wpb.BytesValue{Value: []byte("wow")}}},
|
||||
// `null` is also a permissible value. Let's just test one.
|
||||
{"null DoubleValue", Unmarshaler{}, `{"dbl":null}`, &pb.KnownTypes{Dbl: &wpb.DoubleValue{}}},
|
||||
}
|
||||
|
||||
func TestUnmarshaling(t *testing.T) {
|
||||
for _, tt := range unmarshalingTests {
|
||||
// Make a new instance of the type of our expected object.
|
||||
p := reflect.New(reflect.TypeOf(tt.pb).Elem()).Interface().(proto.Message)
|
||||
|
||||
err := tt.unmarshaler.Unmarshal(strings.NewReader(tt.json), p)
|
||||
if err != nil {
|
||||
t.Errorf("%s: %v", tt.desc, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// For easier diffs, compare text strings of the protos.
|
||||
exp := proto.MarshalTextString(tt.pb)
|
||||
act := proto.MarshalTextString(p)
|
||||
if string(exp) != string(act) {
|
||||
t.Errorf("%s: got [%s] want [%s]", tt.desc, act, exp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalNext(t *testing.T) {
|
||||
// We only need to check against a few, not all of them.
|
||||
tests := unmarshalingTests[:5]
|
||||
|
||||
// Create a buffer with many concatenated JSON objects.
|
||||
var b bytes.Buffer
|
||||
for _, tt := range tests {
|
||||
b.WriteString(tt.json)
|
||||
}
|
||||
|
||||
dec := json.NewDecoder(&b)
|
||||
for _, tt := range tests {
|
||||
// Make a new instance of the type of our expected object.
|
||||
p := reflect.New(reflect.TypeOf(tt.pb).Elem()).Interface().(proto.Message)
|
||||
|
||||
err := tt.unmarshaler.UnmarshalNext(dec, p)
|
||||
if err != nil {
|
||||
t.Errorf("%s: %v", tt.desc, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// For easier diffs, compare text strings of the protos.
|
||||
exp := proto.MarshalTextString(tt.pb)
|
||||
act := proto.MarshalTextString(p)
|
||||
if string(exp) != string(act) {
|
||||
t.Errorf("%s: got [%s] want [%s]", tt.desc, act, exp)
|
||||
}
|
||||
}
|
||||
|
||||
p := &pb.Simple{}
|
||||
err := new(Unmarshaler).UnmarshalNext(dec, p)
|
||||
if err != io.EOF {
|
||||
t.Errorf("eof: got %v, expected io.EOF", err)
|
||||
}
|
||||
}
|
||||
|
||||
var unmarshalingShouldError = []struct {
|
||||
desc string
|
||||
in string
|
||||
pb proto.Message
|
||||
}{
|
||||
{"a value", "666", new(pb.Simple)},
|
||||
{"gibberish", "{adskja123;l23=-=", new(pb.Simple)},
|
||||
{"unknown field", `{"unknown": "foo"}`, new(pb.Simple)},
|
||||
{"unknown enum name", `{"hilarity":"DAVE"}`, new(proto3pb.Message)},
|
||||
}
|
||||
|
||||
func TestUnmarshalingBadInput(t *testing.T) {
|
||||
for _, tt := range unmarshalingShouldError {
|
||||
err := UnmarshalString(tt.in, tt.pb)
|
||||
if err == nil {
|
||||
t.Errorf("an error was expected when parsing %q instead of an object", tt.desc)
|
||||
}
|
||||
}
|
||||
}
|
33
vendor/github.com/golang/protobuf/jsonpb/jsonpb_test_proto/Makefile
generated
vendored
Normal file
33
vendor/github.com/golang/protobuf/jsonpb/jsonpb_test_proto/Makefile
generated
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
# Go support for Protocol Buffers - Google's data interchange format
|
||||
#
|
||||
# Copyright 2015 The Go Authors. All rights reserved.
|
||||
# https://github.com/golang/protobuf
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are
|
||||
# met:
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# * Redistributions in binary form must reproduce the above
|
||||
# copyright notice, this list of conditions and the following disclaimer
|
||||
# in the documentation and/or other materials provided with the
|
||||
# distribution.
|
||||
# * Neither the name of Google Inc. nor the names of its
|
||||
# contributors may be used to endorse or promote products derived from
|
||||
# this software without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
regenerate:
|
||||
protoc --go_out=Mgoogle/protobuf/any.proto=github.com/golang/protobuf/ptypes/any,Mgoogle/protobuf/duration.proto=github.com/golang/protobuf/ptypes/duration,Mgoogle/protobuf/struct.proto=github.com/golang/protobuf/ptypes/struct,Mgoogle/protobuf/timestamp.proto=github.com/golang/protobuf/ptypes/timestamp,Mgoogle/protobuf/wrappers.proto=github.com/golang/protobuf/ptypes/wrappers:. *.proto
|
207
vendor/github.com/golang/protobuf/jsonpb/jsonpb_test_proto/more_test_objects.pb.go
generated
vendored
Normal file
207
vendor/github.com/golang/protobuf/jsonpb/jsonpb_test_proto/more_test_objects.pb.go
generated
vendored
Normal file
@@ -0,0 +1,207 @@
|
||||
// Code generated by protoc-gen-go.
|
||||
// source: more_test_objects.proto
|
||||
// DO NOT EDIT!
|
||||
|
||||
/*
|
||||
Package jsonpb is a generated protocol buffer package.
|
||||
|
||||
It is generated from these files:
|
||||
more_test_objects.proto
|
||||
test_objects.proto
|
||||
|
||||
It has these top-level messages:
|
||||
Simple3
|
||||
Mappy
|
||||
Simple
|
||||
Repeats
|
||||
Widget
|
||||
Maps
|
||||
MsgWithOneof
|
||||
Real
|
||||
Complex
|
||||
KnownTypes
|
||||
*/
|
||||
package jsonpb
|
||||
|
||||
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 Numeral int32
|
||||
|
||||
const (
|
||||
Numeral_UNKNOWN Numeral = 0
|
||||
Numeral_ARABIC Numeral = 1
|
||||
Numeral_ROMAN Numeral = 2
|
||||
)
|
||||
|
||||
var Numeral_name = map[int32]string{
|
||||
0: "UNKNOWN",
|
||||
1: "ARABIC",
|
||||
2: "ROMAN",
|
||||
}
|
||||
var Numeral_value = map[string]int32{
|
||||
"UNKNOWN": 0,
|
||||
"ARABIC": 1,
|
||||
"ROMAN": 2,
|
||||
}
|
||||
|
||||
func (x Numeral) String() string {
|
||||
return proto.EnumName(Numeral_name, int32(x))
|
||||
}
|
||||
func (Numeral) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
|
||||
|
||||
type Simple3 struct {
|
||||
Dub float64 `protobuf:"fixed64,1,opt,name=dub" json:"dub,omitempty"`
|
||||
}
|
||||
|
||||
func (m *Simple3) Reset() { *m = Simple3{} }
|
||||
func (m *Simple3) String() string { return proto.CompactTextString(m) }
|
||||
func (*Simple3) ProtoMessage() {}
|
||||
func (*Simple3) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
|
||||
|
||||
func (m *Simple3) GetDub() float64 {
|
||||
if m != nil {
|
||||
return m.Dub
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
type Mappy struct {
|
||||
Nummy map[int64]int32 `protobuf:"bytes,1,rep,name=nummy" json:"nummy,omitempty" protobuf_key:"varint,1,opt,name=key" protobuf_val:"varint,2,opt,name=value"`
|
||||
Strry map[string]string `protobuf:"bytes,2,rep,name=strry" json:"strry,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"`
|
||||
Objjy map[int32]*Simple3 `protobuf:"bytes,3,rep,name=objjy" json:"objjy,omitempty" protobuf_key:"varint,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"`
|
||||
Buggy map[int64]string `protobuf:"bytes,4,rep,name=buggy" json:"buggy,omitempty" protobuf_key:"varint,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"`
|
||||
Booly map[bool]bool `protobuf:"bytes,5,rep,name=booly" json:"booly,omitempty" protobuf_key:"varint,1,opt,name=key" protobuf_val:"varint,2,opt,name=value"`
|
||||
Enumy map[string]Numeral `protobuf:"bytes,6,rep,name=enumy" json:"enumy,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"varint,2,opt,name=value,enum=jsonpb.Numeral"`
|
||||
S32Booly map[int32]bool `protobuf:"bytes,7,rep,name=s32booly" json:"s32booly,omitempty" protobuf_key:"varint,1,opt,name=key" protobuf_val:"varint,2,opt,name=value"`
|
||||
S64Booly map[int64]bool `protobuf:"bytes,8,rep,name=s64booly" json:"s64booly,omitempty" protobuf_key:"varint,1,opt,name=key" protobuf_val:"varint,2,opt,name=value"`
|
||||
U32Booly map[uint32]bool `protobuf:"bytes,9,rep,name=u32booly" json:"u32booly,omitempty" protobuf_key:"varint,1,opt,name=key" protobuf_val:"varint,2,opt,name=value"`
|
||||
U64Booly map[uint64]bool `protobuf:"bytes,10,rep,name=u64booly" json:"u64booly,omitempty" protobuf_key:"varint,1,opt,name=key" protobuf_val:"varint,2,opt,name=value"`
|
||||
}
|
||||
|
||||
func (m *Mappy) Reset() { *m = Mappy{} }
|
||||
func (m *Mappy) String() string { return proto.CompactTextString(m) }
|
||||
func (*Mappy) ProtoMessage() {}
|
||||
func (*Mappy) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
|
||||
|
||||
func (m *Mappy) GetNummy() map[int64]int32 {
|
||||
if m != nil {
|
||||
return m.Nummy
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Mappy) GetStrry() map[string]string {
|
||||
if m != nil {
|
||||
return m.Strry
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Mappy) GetObjjy() map[int32]*Simple3 {
|
||||
if m != nil {
|
||||
return m.Objjy
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Mappy) GetBuggy() map[int64]string {
|
||||
if m != nil {
|
||||
return m.Buggy
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Mappy) GetBooly() map[bool]bool {
|
||||
if m != nil {
|
||||
return m.Booly
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Mappy) GetEnumy() map[string]Numeral {
|
||||
if m != nil {
|
||||
return m.Enumy
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Mappy) GetS32Booly() map[int32]bool {
|
||||
if m != nil {
|
||||
return m.S32Booly
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Mappy) GetS64Booly() map[int64]bool {
|
||||
if m != nil {
|
||||
return m.S64Booly
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Mappy) GetU32Booly() map[uint32]bool {
|
||||
if m != nil {
|
||||
return m.U32Booly
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Mappy) GetU64Booly() map[uint64]bool {
|
||||
if m != nil {
|
||||
return m.U64Booly
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
proto.RegisterType((*Simple3)(nil), "jsonpb.Simple3")
|
||||
proto.RegisterType((*Mappy)(nil), "jsonpb.Mappy")
|
||||
proto.RegisterEnum("jsonpb.Numeral", Numeral_name, Numeral_value)
|
||||
}
|
||||
|
||||
func init() { proto.RegisterFile("more_test_objects.proto", fileDescriptor0) }
|
||||
|
||||
var fileDescriptor0 = []byte{
|
||||
// 444 bytes of a gzipped FileDescriptorProto
|
||||
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x8c, 0x94, 0xc1, 0x6b, 0xdb, 0x30,
|
||||
0x14, 0x87, 0xe7, 0xa4, 0x4e, 0xec, 0x17, 0xba, 0x19, 0x31, 0x98, 0x58, 0x2f, 0xa1, 0x30, 0x08,
|
||||
0x83, 0xf9, 0x90, 0x8c, 0xad, 0x6c, 0xa7, 0x74, 0xf4, 0x50, 0x46, 0x1d, 0x70, 0x09, 0x3b, 0x96,
|
||||
0x78, 0x13, 0x65, 0x9e, 0x6d, 0x19, 0xdb, 0x1a, 0xe8, 0x8f, 0x1f, 0x8c, 0x27, 0xcb, 0xb5, 0x6c,
|
||||
0x14, 0xd2, 0x9b, 0xcc, 0xef, 0xfb, 0xf2, 0x9e, 0xf4, 0x1e, 0x81, 0x37, 0x39, 0xaf, 0xd8, 0x43,
|
||||
0xc3, 0xea, 0xe6, 0x81, 0x27, 0x29, 0xfb, 0xd9, 0xd4, 0x61, 0x59, 0xf1, 0x86, 0x93, 0x59, 0x5a,
|
||||
0xf3, 0xa2, 0x4c, 0x2e, 0x2f, 0x60, 0x7e, 0xff, 0x3b, 0x2f, 0x33, 0xb6, 0x21, 0x01, 0x4c, 0x7f,
|
||||
0x89, 0x84, 0x3a, 0x4b, 0x67, 0xe5, 0xc4, 0x78, 0xbc, 0xfc, 0xe7, 0x81, 0x7b, 0x77, 0x28, 0x4b,
|
||||
0x49, 0x42, 0x70, 0x0b, 0x91, 0xe7, 0x92, 0x3a, 0xcb, 0xe9, 0x6a, 0xb1, 0xa6, 0x61, 0xab, 0x87,
|
||||
0x2a, 0x0d, 0x23, 0x8c, 0x6e, 0x8a, 0xa6, 0x92, 0x71, 0x8b, 0x21, 0x5f, 0x37, 0x55, 0x25, 0xe9,
|
||||
0xc4, 0xc6, 0xdf, 0x63, 0xa4, 0x79, 0x85, 0x21, 0xcf, 0x93, 0x34, 0x95, 0x74, 0x6a, 0xe3, 0x77,
|
||||
0x18, 0x69, 0x5e, 0x61, 0xc8, 0x27, 0xe2, 0xf1, 0x51, 0xd2, 0x33, 0x1b, 0x7f, 0x8d, 0x91, 0xe6,
|
||||
0x15, 0xa6, 0x78, 0xce, 0x33, 0x49, 0x5d, 0x2b, 0x8f, 0x51, 0xc7, 0xe3, 0x19, 0x79, 0x56, 0x88,
|
||||
0x5c, 0xd2, 0x99, 0x8d, 0xbf, 0xc1, 0x48, 0xf3, 0x0a, 0x23, 0x9f, 0xc1, 0xab, 0x37, 0xeb, 0xb6,
|
||||
0xc4, 0x5c, 0x29, 0x17, 0xa3, 0x2b, 0xeb, 0xb4, 0xb5, 0x9e, 0x60, 0x25, 0x7e, 0xfa, 0xd8, 0x8a,
|
||||
0x9e, 0x55, 0xd4, 0x69, 0x27, 0xea, 0x4f, 0x14, 0x45, 0x57, 0xd1, 0xb7, 0x89, 0xfb, 0x61, 0x45,
|
||||
0x61, 0x54, 0x14, 0x5d, 0x45, 0xb0, 0x8a, 0xc3, 0x8a, 0x1d, 0xfc, 0xf6, 0x0a, 0xa0, 0x1f, 0x34,
|
||||
0x6e, 0xcb, 0x1f, 0x26, 0xd5, 0xb6, 0x4c, 0x63, 0x3c, 0x92, 0xd7, 0xe0, 0xfe, 0x3d, 0x64, 0x82,
|
||||
0xd1, 0xc9, 0xd2, 0x59, 0xb9, 0x71, 0xfb, 0xf1, 0x65, 0x72, 0xe5, 0xa0, 0xd9, 0x8f, 0xdc, 0x34,
|
||||
0x7d, 0x8b, 0xe9, 0x9b, 0xe6, 0x2d, 0x40, 0x3f, 0x7c, 0xd3, 0x74, 0x5b, 0xf3, 0x9d, 0x69, 0x2e,
|
||||
0xd6, 0xaf, 0xba, 0x9b, 0xe8, 0x9d, 0x1e, 0x35, 0xd1, 0xef, 0xc5, 0xa9, 0xf6, 0xfd, 0xb1, 0xf9,
|
||||
0xf4, 0x20, 0xa6, 0xe9, 0x59, 0x4c, 0x6f, 0xd4, 0x7e, 0xbf, 0x2b, 0x96, 0x8b, 0x0f, 0xda, 0x7f,
|
||||
0xd9, 0xb7, 0x1f, 0x89, 0x9c, 0x55, 0x87, 0xcc, 0xfc, 0xa9, 0xaf, 0x70, 0x3e, 0xd8, 0x21, 0xcb,
|
||||
0x63, 0x1c, 0xef, 0x03, 0x65, 0x73, 0xaa, 0xa7, 0xae, 0x3f, 0x96, 0xf7, 0xc7, 0x2a, 0x9f, 0x3f,
|
||||
0x47, 0x3e, 0x56, 0xf9, 0xec, 0x84, 0xfc, 0xfe, 0x03, 0xcc, 0xf5, 0x4b, 0x90, 0x05, 0xcc, 0xf7,
|
||||
0xd1, 0xf7, 0x68, 0xf7, 0x23, 0x0a, 0x5e, 0x10, 0x80, 0xd9, 0x36, 0xde, 0x5e, 0xdf, 0x7e, 0x0b,
|
||||
0x1c, 0xe2, 0x83, 0x1b, 0xef, 0xee, 0xb6, 0x51, 0x30, 0x49, 0x66, 0xea, 0xaf, 0x6d, 0xf3, 0x3f,
|
||||
0x00, 0x00, 0xff, 0xff, 0xa2, 0x4b, 0xe1, 0x77, 0xf5, 0x04, 0x00, 0x00,
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user