mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-14 18:53:35 +00:00
added vendors
This commit is contained in:
3948
vendor/github.com/gogo/protobuf/protoc-gen-gogo/generator/generator.go
generated
vendored
Normal file
3948
vendor/github.com/gogo/protobuf/protoc-gen-gogo/generator/generator.go
generated
vendored
Normal file
File diff suppressed because it is too large
Load Diff
460
vendor/github.com/gogo/protobuf/protoc-gen-gogo/generator/helper.go
generated
vendored
Normal file
460
vendor/github.com/gogo/protobuf/protoc-gen-gogo/generator/helper.go
generated
vendored
Normal file
@ -0,0 +1,460 @@
|
||||
// Protocol Buffers for Go with Gadgets
|
||||
//
|
||||
// Copyright (c) 2013, The GoGo Authors. All rights reserved.
|
||||
// http://github.com/gogo/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.
|
||||
//
|
||||
// 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 generator
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"go/parser"
|
||||
"go/printer"
|
||||
"go/token"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/gogo/protobuf/gogoproto"
|
||||
"github.com/gogo/protobuf/proto"
|
||||
descriptor "github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
|
||||
plugin "github.com/gogo/protobuf/protoc-gen-gogo/plugin"
|
||||
)
|
||||
|
||||
func (d *FileDescriptor) Messages() []*Descriptor {
|
||||
return d.desc
|
||||
}
|
||||
|
||||
func (d *FileDescriptor) Enums() []*EnumDescriptor {
|
||||
return d.enum
|
||||
}
|
||||
|
||||
func (d *Descriptor) IsGroup() bool {
|
||||
return d.group
|
||||
}
|
||||
|
||||
func (g *Generator) IsGroup(field *descriptor.FieldDescriptorProto) bool {
|
||||
if d, ok := g.typeNameToObject[field.GetTypeName()].(*Descriptor); ok {
|
||||
return d.IsGroup()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (g *Generator) TypeNameByObject(typeName string) Object {
|
||||
o, ok := g.typeNameToObject[typeName]
|
||||
if !ok {
|
||||
g.Fail("can't find object with type", typeName)
|
||||
}
|
||||
return o
|
||||
}
|
||||
|
||||
func (g *Generator) OneOfTypeName(message *Descriptor, field *descriptor.FieldDescriptorProto) string {
|
||||
typeName := message.TypeName()
|
||||
ccTypeName := CamelCaseSlice(typeName)
|
||||
fieldName := g.GetOneOfFieldName(message, field)
|
||||
tname := ccTypeName + "_" + fieldName
|
||||
// It is possible for this to collide with a message or enum
|
||||
// nested in this message. Check for collisions.
|
||||
ok := true
|
||||
for _, desc := range message.nested {
|
||||
if strings.Join(desc.TypeName(), "_") == tname {
|
||||
ok = false
|
||||
break
|
||||
}
|
||||
}
|
||||
for _, enum := range message.enums {
|
||||
if strings.Join(enum.TypeName(), "_") == tname {
|
||||
ok = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
tname += "_"
|
||||
}
|
||||
return tname
|
||||
}
|
||||
|
||||
type PluginImports interface {
|
||||
NewImport(pkg string) Single
|
||||
GenerateImports(file *FileDescriptor)
|
||||
}
|
||||
|
||||
type pluginImports struct {
|
||||
generator *Generator
|
||||
singles []Single
|
||||
}
|
||||
|
||||
func NewPluginImports(generator *Generator) *pluginImports {
|
||||
return &pluginImports{generator, make([]Single, 0)}
|
||||
}
|
||||
|
||||
func (this *pluginImports) NewImport(pkg string) Single {
|
||||
imp := newImportedPackage(this.generator.ImportPrefix, pkg)
|
||||
this.singles = append(this.singles, imp)
|
||||
return imp
|
||||
}
|
||||
|
||||
func (this *pluginImports) GenerateImports(file *FileDescriptor) {
|
||||
for _, s := range this.singles {
|
||||
if s.IsUsed() {
|
||||
this.generator.PrintImport(GoPackageName(s.Name()), GoImportPath(s.Location()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type Single interface {
|
||||
Use() string
|
||||
IsUsed() bool
|
||||
Name() string
|
||||
Location() string
|
||||
}
|
||||
|
||||
type importedPackage struct {
|
||||
used bool
|
||||
pkg string
|
||||
name string
|
||||
importPrefix string
|
||||
}
|
||||
|
||||
func newImportedPackage(importPrefix string, pkg string) *importedPackage {
|
||||
return &importedPackage{
|
||||
pkg: pkg,
|
||||
importPrefix: importPrefix,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *importedPackage) Use() string {
|
||||
if !this.used {
|
||||
this.name = string(cleanPackageName(this.pkg))
|
||||
this.used = true
|
||||
}
|
||||
return this.name
|
||||
}
|
||||
|
||||
func (this *importedPackage) IsUsed() bool {
|
||||
return this.used
|
||||
}
|
||||
|
||||
func (this *importedPackage) Name() string {
|
||||
return this.name
|
||||
}
|
||||
|
||||
func (this *importedPackage) Location() string {
|
||||
return this.importPrefix + this.pkg
|
||||
}
|
||||
|
||||
func (g *Generator) GetFieldName(message *Descriptor, field *descriptor.FieldDescriptorProto) string {
|
||||
goTyp, _ := g.GoType(message, field)
|
||||
fieldname := CamelCase(*field.Name)
|
||||
if gogoproto.IsCustomName(field) {
|
||||
fieldname = gogoproto.GetCustomName(field)
|
||||
}
|
||||
if gogoproto.IsEmbed(field) {
|
||||
fieldname = EmbedFieldName(goTyp)
|
||||
}
|
||||
if field.OneofIndex != nil {
|
||||
fieldname = message.OneofDecl[int(*field.OneofIndex)].GetName()
|
||||
fieldname = CamelCase(fieldname)
|
||||
}
|
||||
for _, f := range methodNames {
|
||||
if f == fieldname {
|
||||
return fieldname + "_"
|
||||
}
|
||||
}
|
||||
if !gogoproto.IsProtoSizer(message.file.FileDescriptorProto, message.DescriptorProto) {
|
||||
if fieldname == "Size" {
|
||||
return fieldname + "_"
|
||||
}
|
||||
}
|
||||
return fieldname
|
||||
}
|
||||
|
||||
func (g *Generator) GetOneOfFieldName(message *Descriptor, field *descriptor.FieldDescriptorProto) string {
|
||||
goTyp, _ := g.GoType(message, field)
|
||||
fieldname := CamelCase(*field.Name)
|
||||
if gogoproto.IsCustomName(field) {
|
||||
fieldname = gogoproto.GetCustomName(field)
|
||||
}
|
||||
if gogoproto.IsEmbed(field) {
|
||||
fieldname = EmbedFieldName(goTyp)
|
||||
}
|
||||
for _, f := range methodNames {
|
||||
if f == fieldname {
|
||||
return fieldname + "_"
|
||||
}
|
||||
}
|
||||
if !gogoproto.IsProtoSizer(message.file.FileDescriptorProto, message.DescriptorProto) {
|
||||
if fieldname == "Size" {
|
||||
return fieldname + "_"
|
||||
}
|
||||
}
|
||||
return fieldname
|
||||
}
|
||||
|
||||
func (g *Generator) IsMap(field *descriptor.FieldDescriptorProto) bool {
|
||||
if !field.IsMessage() {
|
||||
return false
|
||||
}
|
||||
byName := g.ObjectNamed(field.GetTypeName())
|
||||
desc, ok := byName.(*Descriptor)
|
||||
if byName == nil || !ok || !desc.GetOptions().GetMapEntry() {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (g *Generator) GetMapKeyField(field, keyField *descriptor.FieldDescriptorProto) *descriptor.FieldDescriptorProto {
|
||||
if !gogoproto.IsCastKey(field) {
|
||||
return keyField
|
||||
}
|
||||
keyField = proto.Clone(keyField).(*descriptor.FieldDescriptorProto)
|
||||
if keyField.Options == nil {
|
||||
keyField.Options = &descriptor.FieldOptions{}
|
||||
}
|
||||
keyType := gogoproto.GetCastKey(field)
|
||||
if err := proto.SetExtension(keyField.Options, gogoproto.E_Casttype, &keyType); err != nil {
|
||||
g.Fail(err.Error())
|
||||
}
|
||||
return keyField
|
||||
}
|
||||
|
||||
func (g *Generator) GetMapValueField(field, valField *descriptor.FieldDescriptorProto) *descriptor.FieldDescriptorProto {
|
||||
if gogoproto.IsCustomType(field) && gogoproto.IsCastValue(field) {
|
||||
g.Fail("cannot have a customtype and casttype: ", field.String())
|
||||
}
|
||||
valField = proto.Clone(valField).(*descriptor.FieldDescriptorProto)
|
||||
if valField.Options == nil {
|
||||
valField.Options = &descriptor.FieldOptions{}
|
||||
}
|
||||
|
||||
stdtime := gogoproto.IsStdTime(field)
|
||||
if stdtime {
|
||||
if err := proto.SetExtension(valField.Options, gogoproto.E_Stdtime, &stdtime); err != nil {
|
||||
g.Fail(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
stddur := gogoproto.IsStdDuration(field)
|
||||
if stddur {
|
||||
if err := proto.SetExtension(valField.Options, gogoproto.E_Stdduration, &stddur); err != nil {
|
||||
g.Fail(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
wktptr := gogoproto.IsWktPtr(field)
|
||||
if wktptr {
|
||||
if err := proto.SetExtension(valField.Options, gogoproto.E_Wktpointer, &wktptr); err != nil {
|
||||
g.Fail(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
if valType := gogoproto.GetCastValue(field); len(valType) > 0 {
|
||||
if err := proto.SetExtension(valField.Options, gogoproto.E_Casttype, &valType); err != nil {
|
||||
g.Fail(err.Error())
|
||||
}
|
||||
}
|
||||
if valType := gogoproto.GetCustomType(field); len(valType) > 0 {
|
||||
if err := proto.SetExtension(valField.Options, gogoproto.E_Customtype, &valType); err != nil {
|
||||
g.Fail(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
nullable := gogoproto.IsNullable(field)
|
||||
if err := proto.SetExtension(valField.Options, gogoproto.E_Nullable, &nullable); err != nil {
|
||||
g.Fail(err.Error())
|
||||
}
|
||||
return valField
|
||||
}
|
||||
|
||||
// GoMapValueTypes returns the map value Go type and the alias map value Go type (for casting), taking into
|
||||
// account whether the map is nullable or the value is a message.
|
||||
func GoMapValueTypes(mapField, valueField *descriptor.FieldDescriptorProto, goValueType, goValueAliasType string) (nullable bool, outGoType string, outGoAliasType string) {
|
||||
nullable = gogoproto.IsNullable(mapField) && (valueField.IsMessage() || gogoproto.IsCustomType(mapField))
|
||||
if nullable {
|
||||
// ensure the non-aliased Go value type is a pointer for consistency
|
||||
if strings.HasPrefix(goValueType, "*") {
|
||||
outGoType = goValueType
|
||||
} else {
|
||||
outGoType = "*" + goValueType
|
||||
}
|
||||
outGoAliasType = goValueAliasType
|
||||
} else {
|
||||
outGoType = strings.Replace(goValueType, "*", "", 1)
|
||||
outGoAliasType = strings.Replace(goValueAliasType, "*", "", 1)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func GoTypeToName(goTyp string) string {
|
||||
return strings.Replace(strings.Replace(goTyp, "*", "", -1), "[]", "", -1)
|
||||
}
|
||||
|
||||
func EmbedFieldName(goTyp string) string {
|
||||
goTyp = GoTypeToName(goTyp)
|
||||
goTyps := strings.Split(goTyp, ".")
|
||||
if len(goTyps) == 1 {
|
||||
return goTyp
|
||||
}
|
||||
if len(goTyps) == 2 {
|
||||
return goTyps[1]
|
||||
}
|
||||
panic("unreachable")
|
||||
}
|
||||
|
||||
func (g *Generator) GeneratePlugin(p Plugin) {
|
||||
plugins = []Plugin{p}
|
||||
p.Init(g)
|
||||
// Generate the output. The generator runs for every file, even the files
|
||||
// that we don't generate output for, so that we can collate the full list
|
||||
// of exported symbols to support public imports.
|
||||
genFileMap := make(map[*FileDescriptor]bool, len(g.genFiles))
|
||||
for _, file := range g.genFiles {
|
||||
genFileMap[file] = true
|
||||
}
|
||||
for _, file := range g.allFiles {
|
||||
g.Reset()
|
||||
g.writeOutput = genFileMap[file]
|
||||
g.generatePlugin(file, p)
|
||||
if !g.writeOutput {
|
||||
continue
|
||||
}
|
||||
g.Response.File = append(g.Response.File, &plugin.CodeGeneratorResponse_File{
|
||||
Name: proto.String(file.goFileName(g.pathType)),
|
||||
Content: proto.String(g.String()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Generator) SetFile(filename string) {
|
||||
g.file = g.fileByName(filename)
|
||||
}
|
||||
|
||||
func (g *Generator) generatePlugin(file *FileDescriptor, p Plugin) {
|
||||
g.writtenImports = make(map[string]bool)
|
||||
g.usedPackages = make(map[GoImportPath]bool)
|
||||
g.packageNames = make(map[GoImportPath]GoPackageName)
|
||||
g.usedPackageNames = make(map[GoPackageName]bool)
|
||||
g.file = file
|
||||
|
||||
// Run the plugins before the imports so we know which imports are necessary.
|
||||
p.Generate(file)
|
||||
|
||||
// Generate header and imports last, though they appear first in the output.
|
||||
rem := g.Buffer
|
||||
g.Buffer = new(bytes.Buffer)
|
||||
g.generateHeader()
|
||||
p.GenerateImports(g.file)
|
||||
g.generateImports()
|
||||
if !g.writeOutput {
|
||||
return
|
||||
}
|
||||
g.Write(rem.Bytes())
|
||||
|
||||
// Reformat generated code.
|
||||
contents := string(g.Buffer.Bytes())
|
||||
fset := token.NewFileSet()
|
||||
ast, err := parser.ParseFile(fset, "", g, parser.ParseComments)
|
||||
if err != nil {
|
||||
g.Fail("bad Go source code was generated:", contents, err.Error())
|
||||
return
|
||||
}
|
||||
g.Reset()
|
||||
err = (&printer.Config{Mode: printer.TabIndent | printer.UseSpaces, Tabwidth: 8}).Fprint(g, fset, ast)
|
||||
if err != nil {
|
||||
g.Fail("generated Go source code could not be reformatted:", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func GetCustomType(field *descriptor.FieldDescriptorProto) (packageName string, typ string, err error) {
|
||||
return getCustomType(field)
|
||||
}
|
||||
|
||||
func getCustomType(field *descriptor.FieldDescriptorProto) (packageName string, typ string, err error) {
|
||||
if field.Options != nil {
|
||||
var v interface{}
|
||||
v, err = proto.GetExtension(field.Options, gogoproto.E_Customtype)
|
||||
if err == nil && v.(*string) != nil {
|
||||
ctype := *(v.(*string))
|
||||
packageName, typ = splitCPackageType(ctype)
|
||||
return packageName, typ, nil
|
||||
}
|
||||
}
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
func splitCPackageType(ctype string) (packageName string, typ string) {
|
||||
ss := strings.Split(ctype, ".")
|
||||
if len(ss) == 1 {
|
||||
return "", ctype
|
||||
}
|
||||
packageName = strings.Join(ss[0:len(ss)-1], ".")
|
||||
typeName := ss[len(ss)-1]
|
||||
importStr := strings.Map(badToUnderscore, packageName)
|
||||
typ = importStr + "." + typeName
|
||||
return packageName, typ
|
||||
}
|
||||
|
||||
func getCastType(field *descriptor.FieldDescriptorProto) (packageName string, typ string, err error) {
|
||||
if field.Options != nil {
|
||||
var v interface{}
|
||||
v, err = proto.GetExtension(field.Options, gogoproto.E_Casttype)
|
||||
if err == nil && v.(*string) != nil {
|
||||
ctype := *(v.(*string))
|
||||
packageName, typ = splitCPackageType(ctype)
|
||||
return packageName, typ, nil
|
||||
}
|
||||
}
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
func FileName(file *FileDescriptor) string {
|
||||
fname := path.Base(file.FileDescriptorProto.GetName())
|
||||
fname = strings.Replace(fname, ".proto", "", -1)
|
||||
fname = strings.Replace(fname, "-", "_", -1)
|
||||
fname = strings.Replace(fname, ".", "_", -1)
|
||||
return CamelCase(fname)
|
||||
}
|
||||
|
||||
func (g *Generator) AllFiles() *descriptor.FileDescriptorSet {
|
||||
set := &descriptor.FileDescriptorSet{}
|
||||
set.File = make([]*descriptor.FileDescriptorProto, len(g.allFiles))
|
||||
for i := range g.allFiles {
|
||||
set.File[i] = g.allFiles[i].FileDescriptorProto
|
||||
}
|
||||
return set
|
||||
}
|
||||
|
||||
func (d *Descriptor) Path() string {
|
||||
return d.path
|
||||
}
|
||||
|
||||
func (g *Generator) useTypes() string {
|
||||
pkg := strings.Map(badToUnderscore, "github.com/gogo/protobuf/types")
|
||||
g.customImports = append(g.customImports, "github.com/gogo/protobuf/types")
|
||||
return pkg
|
||||
}
|
||||
|
||||
func (d *FileDescriptor) GoPackageName() string {
|
||||
return string(d.packageName)
|
||||
}
|
117
vendor/github.com/gogo/protobuf/protoc-gen-gogo/generator/internal/remap/remap.go
generated
vendored
Normal file
117
vendor/github.com/gogo/protobuf/protoc-gen-gogo/generator/internal/remap/remap.go
generated
vendored
Normal file
@ -0,0 +1,117 @@
|
||||
// Go support for Protocol Buffers - Google's data interchange format
|
||||
//
|
||||
// Copyright 2017 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 remap handles tracking the locations of Go tokens in a source text
|
||||
across a rewrite by the Go formatter.
|
||||
*/
|
||||
package remap
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/scanner"
|
||||
"go/token"
|
||||
)
|
||||
|
||||
// A Location represents a span of byte offsets in the source text.
|
||||
type Location struct {
|
||||
Pos, End int // End is exclusive
|
||||
}
|
||||
|
||||
// A Map represents a mapping between token locations in an input source text
|
||||
// and locations in the correspnding output text.
|
||||
type Map map[Location]Location
|
||||
|
||||
// Find reports whether the specified span is recorded by m, and if so returns
|
||||
// the new location it was mapped to. If the input span was not found, the
|
||||
// returned location is the same as the input.
|
||||
func (m Map) Find(pos, end int) (Location, bool) {
|
||||
key := Location{
|
||||
Pos: pos,
|
||||
End: end,
|
||||
}
|
||||
if loc, ok := m[key]; ok {
|
||||
return loc, true
|
||||
}
|
||||
return key, false
|
||||
}
|
||||
|
||||
func (m Map) add(opos, oend, npos, nend int) {
|
||||
m[Location{Pos: opos, End: oend}] = Location{Pos: npos, End: nend}
|
||||
}
|
||||
|
||||
// Compute constructs a location mapping from input to output. An error is
|
||||
// reported if any of the tokens of output cannot be mapped.
|
||||
func Compute(input, output []byte) (Map, error) {
|
||||
itok := tokenize(input)
|
||||
otok := tokenize(output)
|
||||
if len(itok) != len(otok) {
|
||||
return nil, fmt.Errorf("wrong number of tokens, %d ≠ %d", len(itok), len(otok))
|
||||
}
|
||||
m := make(Map)
|
||||
for i, ti := range itok {
|
||||
to := otok[i]
|
||||
if ti.Token != to.Token {
|
||||
return nil, fmt.Errorf("token %d type mismatch: %s ≠ %s", i+1, ti, to)
|
||||
}
|
||||
m.add(ti.pos, ti.end, to.pos, to.end)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// tokinfo records the span and type of a source token.
|
||||
type tokinfo struct {
|
||||
pos, end int
|
||||
token.Token
|
||||
}
|
||||
|
||||
func tokenize(src []byte) []tokinfo {
|
||||
fs := token.NewFileSet()
|
||||
var s scanner.Scanner
|
||||
s.Init(fs.AddFile("src", fs.Base(), len(src)), src, nil, scanner.ScanComments)
|
||||
var info []tokinfo
|
||||
for {
|
||||
pos, next, lit := s.Scan()
|
||||
switch next {
|
||||
case token.SEMICOLON:
|
||||
continue
|
||||
}
|
||||
info = append(info, tokinfo{
|
||||
pos: int(pos - 1),
|
||||
end: int(pos + token.Pos(len(lit)) - 1),
|
||||
Token: next,
|
||||
})
|
||||
if next == token.EOF {
|
||||
break
|
||||
}
|
||||
}
|
||||
return info
|
||||
}
|
82
vendor/github.com/gogo/protobuf/protoc-gen-gogo/generator/internal/remap/remap_test.go
generated
vendored
Normal file
82
vendor/github.com/gogo/protobuf/protoc-gen-gogo/generator/internal/remap/remap_test.go
generated
vendored
Normal file
@ -0,0 +1,82 @@
|
||||
// Go support for Protocol Buffers - Google's data interchange format
|
||||
//
|
||||
// Copyright 2017 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 remap
|
||||
|
||||
import (
|
||||
"go/format"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestErrors(t *testing.T) {
|
||||
tests := []struct {
|
||||
in, out string
|
||||
}{
|
||||
{"", "x"},
|
||||
{"x", ""},
|
||||
{"var x int = 5\n", "var x = 5\n"},
|
||||
{"these are \"one\" thing", "those are 'another' thing"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
m, err := Compute([]byte(test.in), []byte(test.out))
|
||||
if err != nil {
|
||||
t.Logf("Got expected error: %v", err)
|
||||
continue
|
||||
}
|
||||
t.Errorf("Compute(%q, %q): got %+v, wanted error", test.in, test.out, m)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatching(t *testing.T) {
|
||||
// The input is a source text that will be rearranged by the formatter.
|
||||
const input = `package foo
|
||||
var s int
|
||||
func main(){}
|
||||
`
|
||||
|
||||
output, err := format.Source([]byte(input))
|
||||
if err != nil {
|
||||
t.Fatalf("Formatting failed: %v", err)
|
||||
}
|
||||
m, err := Compute([]byte(input), output)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected error: %v", err)
|
||||
}
|
||||
|
||||
// Verify that the mapped locations have the same text.
|
||||
for key, val := range m {
|
||||
want := input[key.Pos:key.End]
|
||||
got := string(output[val.Pos:val.End])
|
||||
if got != want {
|
||||
t.Errorf("Token at %d:%d: got %q, want %q", key.Pos, key.End, got, want)
|
||||
}
|
||||
}
|
||||
}
|
115
vendor/github.com/gogo/protobuf/protoc-gen-gogo/generator/name_test.go
generated
vendored
Normal file
115
vendor/github.com/gogo/protobuf/protoc-gen-gogo/generator/name_test.go
generated
vendored
Normal file
@ -0,0 +1,115 @@
|
||||
// Go support for Protocol Buffers - Google's data interchange format
|
||||
//
|
||||
// Copyright 2013 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 generator
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
|
||||
)
|
||||
|
||||
func TestCamelCase(t *testing.T) {
|
||||
tests := []struct {
|
||||
in, want string
|
||||
}{
|
||||
{"one", "One"},
|
||||
{"one_two", "OneTwo"},
|
||||
{"_my_field_name_2", "XMyFieldName_2"},
|
||||
{"Something_Capped", "Something_Capped"},
|
||||
{"my_Name", "My_Name"},
|
||||
{"OneTwo", "OneTwo"},
|
||||
{"_", "X"},
|
||||
{"_a_", "XA_"},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
if got := CamelCase(tc.in); got != tc.want {
|
||||
t.Errorf("CamelCase(%q) = %q, want %q", tc.in, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoPackageOption(t *testing.T) {
|
||||
tests := []struct {
|
||||
in string
|
||||
impPath GoImportPath
|
||||
pkg GoPackageName
|
||||
ok bool
|
||||
}{
|
||||
{"", "", "", false},
|
||||
{"foo", "", "foo", true},
|
||||
{"github.com/golang/bar", "github.com/golang/bar", "bar", true},
|
||||
{"github.com/golang/bar;baz", "github.com/golang/bar", "baz", true},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
d := &FileDescriptor{
|
||||
FileDescriptorProto: &descriptor.FileDescriptorProto{
|
||||
Options: &descriptor.FileOptions{
|
||||
GoPackage: &tc.in,
|
||||
},
|
||||
},
|
||||
}
|
||||
impPath, pkg, ok := d.goPackageOption()
|
||||
if impPath != tc.impPath || pkg != tc.pkg || ok != tc.ok {
|
||||
t.Errorf("go_package = %q => (%q, %q, %t), want (%q, %q, %t)", tc.in,
|
||||
impPath, pkg, ok, tc.impPath, tc.pkg, tc.ok)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnescape(t *testing.T) {
|
||||
tests := []struct {
|
||||
in string
|
||||
out string
|
||||
}{
|
||||
// successful cases, including all kinds of escapes
|
||||
{"", ""},
|
||||
{"foo bar baz frob nitz", "foo bar baz frob nitz"},
|
||||
{`\000\001\002\003\004\005\006\007`, string([]byte{0, 1, 2, 3, 4, 5, 6, 7})},
|
||||
{`\a\b\f\n\r\t\v\\\?\'\"`, string([]byte{'\a', '\b', '\f', '\n', '\r', '\t', '\v', '\\', '?', '\'', '"'})},
|
||||
{`\x10\x20\x30\x40\x50\x60\x70\x80`, string([]byte{16, 32, 48, 64, 80, 96, 112, 128})},
|
||||
// variable length octal escapes
|
||||
{`\0\018\222\377\3\04\005\6\07`, string([]byte{0, 1, '8', 0222, 255, 3, 4, 5, 6, 7})},
|
||||
// malformed escape sequences left as is
|
||||
{"foo \\g bar", "foo \\g bar"},
|
||||
{"foo \\xg0 bar", "foo \\xg0 bar"},
|
||||
{"\\", "\\"},
|
||||
{"\\x", "\\x"},
|
||||
{"\\xf", "\\xf"},
|
||||
{"\\777", "\\777"}, // overflows byte
|
||||
}
|
||||
for _, tc := range tests {
|
||||
s := unescape(tc.in)
|
||||
if s != tc.out {
|
||||
t.Errorf("doUnescape(%q) = %q; should have been %q", tc.in, s, tc.out)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user