vendor: Update vendoring for the exec client and server implementations
Signed-off-by: Jacek J. Łakis <jacek.lakis@intel.com> Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
This commit is contained in:
parent
d25b88583f
commit
bf51655a7b
2124 changed files with 809703 additions and 5 deletions
58
vendor/cloud.google.com/go/internal/atomiccache/atomiccache.go
generated
vendored
Normal file
58
vendor/cloud.google.com/go/internal/atomiccache/atomiccache.go
generated
vendored
Normal file
|
@ -0,0 +1,58 @@
|
|||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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 atomiccache provides a map-based cache that supports very fast
|
||||
// reads.
|
||||
package atomiccache
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
type mapType map[interface{}]interface{}
|
||||
|
||||
// Cache is a map-based cache that supports fast reads via use of atomics.
|
||||
// Writes are slow, requiring a copy of the entire cache.
|
||||
// The zero Cache is an empty cache, ready for use.
|
||||
type Cache struct {
|
||||
val atomic.Value // mapType
|
||||
mu sync.Mutex // used only by writers
|
||||
}
|
||||
|
||||
// Get returns the value of the cache at key. If there is no value,
|
||||
// getter is called to provide one, and the cache is updated.
|
||||
// The getter function may be called concurrently. It should be pure,
|
||||
// returning the same value for every call.
|
||||
func (c *Cache) Get(key interface{}, getter func() interface{}) interface{} {
|
||||
mp, _ := c.val.Load().(mapType)
|
||||
if v, ok := mp[key]; ok {
|
||||
return v
|
||||
}
|
||||
|
||||
// Compute value without lock.
|
||||
// Might duplicate effort but won't hold other computations back.
|
||||
newV := getter()
|
||||
|
||||
c.mu.Lock()
|
||||
mp, _ = c.val.Load().(mapType)
|
||||
newM := make(mapType, len(mp)+1)
|
||||
for k, v := range mp {
|
||||
newM[k] = v
|
||||
}
|
||||
newM[key] = newV
|
||||
c.val.Store(newM)
|
||||
c.mu.Unlock()
|
||||
return newV
|
||||
}
|
46
vendor/cloud.google.com/go/internal/atomiccache/atomiccache_test.go
generated
vendored
Normal file
46
vendor/cloud.google.com/go/internal/atomiccache/atomiccache_test.go
generated
vendored
Normal file
|
@ -0,0 +1,46 @@
|
|||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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 atomiccache
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGet(t *testing.T) {
|
||||
var c Cache
|
||||
called := false
|
||||
get := func(k interface{}) interface{} {
|
||||
return c.Get(k, func() interface{} {
|
||||
called = true
|
||||
return fmt.Sprintf("v%d", k)
|
||||
})
|
||||
}
|
||||
got := get(1)
|
||||
if want := "v1"; got != want {
|
||||
t.Errorf("got %v, want %v", got, want)
|
||||
}
|
||||
if !called {
|
||||
t.Error("getter not called, expected a call")
|
||||
}
|
||||
called = false
|
||||
got = get(1)
|
||||
if want := "v1"; got != want {
|
||||
t.Errorf("got %v, want %v", got, want)
|
||||
}
|
||||
if called {
|
||||
t.Error("getter unexpectedly called")
|
||||
}
|
||||
}
|
64
vendor/cloud.google.com/go/internal/cloud.go
generated
vendored
Normal file
64
vendor/cloud.google.com/go/internal/cloud.go
generated
vendored
Normal file
|
@ -0,0 +1,64 @@
|
|||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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 internal provides support for the cloud packages.
|
||||
//
|
||||
// Users should not import this package directly.
|
||||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const userAgent = "gcloud-golang/0.1"
|
||||
|
||||
// Transport is an http.RoundTripper that appends Google Cloud client's
|
||||
// user-agent to the original request's user-agent header.
|
||||
type Transport struct {
|
||||
// TODO(bradfitz): delete internal.Transport. It's too wrappy for what it does.
|
||||
// Do User-Agent some other way.
|
||||
|
||||
// Base is the actual http.RoundTripper
|
||||
// requests will use. It must not be nil.
|
||||
Base http.RoundTripper
|
||||
}
|
||||
|
||||
// RoundTrip appends a user-agent to the existing user-agent
|
||||
// header and delegates the request to the base http.RoundTripper.
|
||||
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
req = cloneRequest(req)
|
||||
ua := req.Header.Get("User-Agent")
|
||||
if ua == "" {
|
||||
ua = userAgent
|
||||
} else {
|
||||
ua = fmt.Sprintf("%s %s", ua, userAgent)
|
||||
}
|
||||
req.Header.Set("User-Agent", ua)
|
||||
return t.Base.RoundTrip(req)
|
||||
}
|
||||
|
||||
// cloneRequest returns a clone of the provided *http.Request.
|
||||
// The clone is a shallow copy of the struct and its Header map.
|
||||
func cloneRequest(r *http.Request) *http.Request {
|
||||
// shallow copy of the struct
|
||||
r2 := new(http.Request)
|
||||
*r2 = *r
|
||||
// deep copy of the Header
|
||||
r2.Header = make(http.Header)
|
||||
for k, s := range r.Header {
|
||||
r2.Header[k] = s
|
||||
}
|
||||
return r2
|
||||
}
|
444
vendor/cloud.google.com/go/internal/fields/fields.go
generated
vendored
Normal file
444
vendor/cloud.google.com/go/internal/fields/fields.go
generated
vendored
Normal file
|
@ -0,0 +1,444 @@
|
|||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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 fields provides a view of the fields of a struct that follows the Go
|
||||
// rules, amended to consider tags and case insensitivity.
|
||||
//
|
||||
// Usage
|
||||
//
|
||||
// First define a function that interprets tags:
|
||||
//
|
||||
// func parseTag(st reflect.StructTag) (name string, keep bool, other interface{}, err error) { ... }
|
||||
//
|
||||
// The function's return values describe whether to ignore the field
|
||||
// completely or provide an alternate name, as well as other data from the
|
||||
// parse that is stored to avoid re-parsing.
|
||||
//
|
||||
// Then define a function to validate the type:
|
||||
//
|
||||
// func validate(t reflect.Type) error { ... }
|
||||
//
|
||||
// Then, if necessary, define a function to specify leaf types - types
|
||||
// which should be considered one field and not be recursed into:
|
||||
//
|
||||
// func isLeafType(t reflect.Type) bool { ... }
|
||||
//
|
||||
// eg:
|
||||
//
|
||||
// func isLeafType(t reflect.Type) bool {
|
||||
// return t == reflect.TypeOf(time.Time{})
|
||||
// }
|
||||
//
|
||||
// Next, construct a Cache, passing your functions. As its name suggests, a
|
||||
// Cache remembers validation and field information for a type, so subsequent
|
||||
// calls with the same type are very fast.
|
||||
//
|
||||
// cache := fields.NewCache(parseTag, validate, isLeafType)
|
||||
//
|
||||
// To get the fields of a struct type as determined by the above rules, call
|
||||
// the Fields method:
|
||||
//
|
||||
// fields, err := cache.Fields(reflect.TypeOf(MyStruct{}))
|
||||
//
|
||||
// The return value can be treated as a slice of Fields.
|
||||
//
|
||||
// Given a string, such as a key or column name obtained during unmarshalling,
|
||||
// call Match on the list of fields to find a field whose name is the best
|
||||
// match:
|
||||
//
|
||||
// field := fields.Match(name)
|
||||
//
|
||||
// Match looks for an exact match first, then falls back to a case-insensitive
|
||||
// comparison.
|
||||
package fields
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"reflect"
|
||||
"sort"
|
||||
|
||||
"cloud.google.com/go/internal/atomiccache"
|
||||
)
|
||||
|
||||
// A Field records information about a struct field.
|
||||
type Field struct {
|
||||
Name string // effective field name
|
||||
NameFromTag bool // did Name come from a tag?
|
||||
Type reflect.Type // field type
|
||||
Index []int // index sequence, for reflect.Value.FieldByIndex
|
||||
ParsedTag interface{} // third return value of the parseTag function
|
||||
|
||||
nameBytes []byte
|
||||
equalFold func(s, t []byte) bool
|
||||
}
|
||||
|
||||
type ParseTagFunc func(reflect.StructTag) (name string, keep bool, other interface{}, err error)
|
||||
|
||||
type ValidateFunc func(reflect.Type) error
|
||||
|
||||
type LeafTypesFunc func(reflect.Type) bool
|
||||
|
||||
// A Cache records information about the fields of struct types.
|
||||
//
|
||||
// A Cache is safe for use by multiple goroutines.
|
||||
type Cache struct {
|
||||
parseTag ParseTagFunc
|
||||
validate ValidateFunc
|
||||
leafTypes LeafTypesFunc
|
||||
cache atomiccache.Cache // from reflect.Type to cacheValue
|
||||
}
|
||||
|
||||
// NewCache constructs a Cache.
|
||||
//
|
||||
// Its first argument should be a function that accepts
|
||||
// a struct tag and returns four values: an alternative name for the field
|
||||
// extracted from the tag, a boolean saying whether to keep the field or ignore
|
||||
// it, additional data that is stored with the field information to avoid
|
||||
// having to parse the tag again, and an error.
|
||||
//
|
||||
// Its second argument should be a function that accepts a reflect.Type and
|
||||
// returns an error if the struct type is invalid in any way. For example, it
|
||||
// may check that all of the struct field tags are valid, or that all fields
|
||||
// are of an appropriate type.
|
||||
func NewCache(parseTag ParseTagFunc, validate ValidateFunc, leafTypes LeafTypesFunc) *Cache {
|
||||
if parseTag == nil {
|
||||
parseTag = func(reflect.StructTag) (string, bool, interface{}, error) {
|
||||
return "", true, nil, nil
|
||||
}
|
||||
}
|
||||
if validate == nil {
|
||||
validate = func(reflect.Type) error {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if leafTypes == nil {
|
||||
leafTypes = func(reflect.Type) bool {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return &Cache{
|
||||
parseTag: parseTag,
|
||||
validate: validate,
|
||||
leafTypes: leafTypes,
|
||||
}
|
||||
}
|
||||
|
||||
// A fieldScan represents an item on the fieldByNameFunc scan work list.
|
||||
type fieldScan struct {
|
||||
typ reflect.Type
|
||||
index []int
|
||||
}
|
||||
|
||||
// Fields returns all the exported fields of t, which must be a struct type. It
|
||||
// follows the standard Go rules for embedded fields, modified by the presence
|
||||
// of tags. The result is sorted lexicographically by index.
|
||||
//
|
||||
// These rules apply in the absence of tags:
|
||||
// Anonymous struct fields are treated as if their inner exported fields were
|
||||
// fields in the outer struct (embedding). The result includes all fields that
|
||||
// aren't shadowed by fields at higher level of embedding. If more than one
|
||||
// field with the same name exists at the same level of embedding, it is
|
||||
// excluded. An anonymous field that is not of struct type is treated as having
|
||||
// its type as its name.
|
||||
//
|
||||
// Tags modify these rules as follows:
|
||||
// A field's tag is used as its name.
|
||||
// An anonymous struct field with a name given in its tag is treated as
|
||||
// a field having that name, rather than an embedded struct (the struct's
|
||||
// fields will not be returned).
|
||||
// If more than one field with the same name exists at the same level of embedding,
|
||||
// but exactly one of them is tagged, then the tagged field is reported and the others
|
||||
// are ignored.
|
||||
func (c *Cache) Fields(t reflect.Type) (List, error) {
|
||||
if t.Kind() != reflect.Struct {
|
||||
panic("fields: Fields of non-struct type")
|
||||
}
|
||||
return c.cachedTypeFields(t)
|
||||
}
|
||||
|
||||
// A List is a list of Fields.
|
||||
type List []Field
|
||||
|
||||
// Match returns the field in the list whose name best matches the supplied
|
||||
// name, nor nil if no field does. If there is a field with the exact name, it
|
||||
// is returned. Otherwise the first field (sorted by index) whose name matches
|
||||
// case-insensitively is returned.
|
||||
func (l List) Match(name string) *Field {
|
||||
return l.MatchBytes([]byte(name))
|
||||
}
|
||||
|
||||
// MatchBytes is identical to Match, except that the argument is a byte slice.
|
||||
func (l List) MatchBytes(name []byte) *Field {
|
||||
var f *Field
|
||||
for i := range l {
|
||||
ff := &l[i]
|
||||
if bytes.Equal(ff.nameBytes, name) {
|
||||
return ff
|
||||
}
|
||||
if f == nil && ff.equalFold(ff.nameBytes, name) {
|
||||
f = ff
|
||||
}
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
type cacheValue struct {
|
||||
fields List
|
||||
err error
|
||||
}
|
||||
|
||||
// cachedTypeFields is like typeFields but uses a cache to avoid repeated work.
|
||||
// This code has been copied and modified from
|
||||
// https://go.googlesource.com/go/+/go1.7.3/src/encoding/json/encode.go.
|
||||
func (c *Cache) cachedTypeFields(t reflect.Type) (List, error) {
|
||||
cv := c.cache.Get(t, func() interface{} {
|
||||
if err := c.validate(t); err != nil {
|
||||
return cacheValue{nil, err}
|
||||
}
|
||||
f, err := c.typeFields(t)
|
||||
return cacheValue{List(f), err}
|
||||
}).(cacheValue)
|
||||
return cv.fields, cv.err
|
||||
}
|
||||
|
||||
func (c *Cache) typeFields(t reflect.Type) ([]Field, error) {
|
||||
fields, err := c.listFields(t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sort.Sort(byName(fields))
|
||||
// Delete all fields that are hidden by the Go rules for embedded fields.
|
||||
|
||||
// The fields are sorted in primary order of name, secondary order of field
|
||||
// index length. So the first field with a given name is the dominant one.
|
||||
var out []Field
|
||||
for advance, i := 0, 0; i < len(fields); i += advance {
|
||||
// One iteration per name.
|
||||
// Find the sequence of fields with the name of this first field.
|
||||
fi := fields[i]
|
||||
name := fi.Name
|
||||
for advance = 1; i+advance < len(fields); advance++ {
|
||||
fj := fields[i+advance]
|
||||
if fj.Name != name {
|
||||
break
|
||||
}
|
||||
}
|
||||
// Find the dominant field, if any, out of all fields that have the same name.
|
||||
dominant, ok := dominantField(fields[i : i+advance])
|
||||
if ok {
|
||||
out = append(out, dominant)
|
||||
}
|
||||
}
|
||||
sort.Sort(byIndex(out))
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *Cache) listFields(t reflect.Type) ([]Field, error) {
|
||||
// This uses the same condition that the Go language does: there must be a unique instance
|
||||
// of the match at a given depth level. If there are multiple instances of a match at the
|
||||
// same depth, they annihilate each other and inhibit any possible match at a lower level.
|
||||
// The algorithm is breadth first search, one depth level at a time.
|
||||
|
||||
// The current and next slices are work queues:
|
||||
// current lists the fields to visit on this depth level,
|
||||
// and next lists the fields on the next lower level.
|
||||
current := []fieldScan{}
|
||||
next := []fieldScan{{typ: t}}
|
||||
|
||||
// nextCount records the number of times an embedded type has been
|
||||
// encountered and considered for queueing in the 'next' slice.
|
||||
// We only queue the first one, but we increment the count on each.
|
||||
// If a struct type T can be reached more than once at a given depth level,
|
||||
// then it annihilates itself and need not be considered at all when we
|
||||
// process that next depth level.
|
||||
var nextCount map[reflect.Type]int
|
||||
|
||||
// visited records the structs that have been considered already.
|
||||
// Embedded pointer fields can create cycles in the graph of
|
||||
// reachable embedded types; visited avoids following those cycles.
|
||||
// It also avoids duplicated effort: if we didn't find the field in an
|
||||
// embedded type T at level 2, we won't find it in one at level 4 either.
|
||||
visited := map[reflect.Type]bool{}
|
||||
|
||||
var fields []Field // Fields found.
|
||||
|
||||
for len(next) > 0 {
|
||||
current, next = next, current[:0]
|
||||
count := nextCount
|
||||
nextCount = nil
|
||||
|
||||
// Process all the fields at this depth, now listed in 'current'.
|
||||
// The loop queues embedded fields found in 'next', for processing during the next
|
||||
// iteration. The multiplicity of the 'current' field counts is recorded
|
||||
// in 'count'; the multiplicity of the 'next' field counts is recorded in 'nextCount'.
|
||||
for _, scan := range current {
|
||||
t := scan.typ
|
||||
if visited[t] {
|
||||
// We've looked through this type before, at a higher level.
|
||||
// That higher level would shadow the lower level we're now at,
|
||||
// so this one can't be useful to us. Ignore it.
|
||||
continue
|
||||
}
|
||||
visited[t] = true
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
f := t.Field(i)
|
||||
|
||||
exported := (f.PkgPath == "")
|
||||
|
||||
// If a named field is unexported, ignore it. An anonymous
|
||||
// unexported field is processed, because it may contain
|
||||
// exported fields, which are visible.
|
||||
if !exported && !f.Anonymous {
|
||||
continue
|
||||
}
|
||||
|
||||
// Examine the tag.
|
||||
tagName, keep, other, err := c.parseTag(f.Tag)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !keep {
|
||||
continue
|
||||
}
|
||||
if c.leafTypes(f.Type) {
|
||||
fields = append(fields, newField(f, tagName, other, scan.index, i))
|
||||
continue
|
||||
}
|
||||
|
||||
var ntyp reflect.Type
|
||||
if f.Anonymous {
|
||||
// Anonymous field of type T or *T.
|
||||
ntyp = f.Type
|
||||
if ntyp.Kind() == reflect.Ptr {
|
||||
ntyp = ntyp.Elem()
|
||||
}
|
||||
}
|
||||
|
||||
// Record fields with a tag name, non-anonymous fields, or
|
||||
// anonymous non-struct fields.
|
||||
if tagName != "" || ntyp == nil || ntyp.Kind() != reflect.Struct {
|
||||
if !exported {
|
||||
continue
|
||||
}
|
||||
fields = append(fields, newField(f, tagName, other, scan.index, i))
|
||||
if count[t] > 1 {
|
||||
// If there were multiple instances, add a second,
|
||||
// so that the annihilation code will see a duplicate.
|
||||
fields = append(fields, fields[len(fields)-1])
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// Queue embedded struct fields for processing with next level,
|
||||
// but only if the embedded types haven't already been queued.
|
||||
if nextCount[ntyp] > 0 {
|
||||
nextCount[ntyp] = 2 // exact multiple doesn't matter
|
||||
continue
|
||||
}
|
||||
if nextCount == nil {
|
||||
nextCount = map[reflect.Type]int{}
|
||||
}
|
||||
nextCount[ntyp] = 1
|
||||
if count[t] > 1 {
|
||||
nextCount[ntyp] = 2 // exact multiple doesn't matter
|
||||
}
|
||||
var index []int
|
||||
index = append(index, scan.index...)
|
||||
index = append(index, i)
|
||||
next = append(next, fieldScan{ntyp, index})
|
||||
}
|
||||
}
|
||||
}
|
||||
return fields, nil
|
||||
}
|
||||
|
||||
func newField(f reflect.StructField, tagName string, other interface{}, index []int, i int) Field {
|
||||
name := tagName
|
||||
if name == "" {
|
||||
name = f.Name
|
||||
}
|
||||
sf := Field{
|
||||
Name: name,
|
||||
NameFromTag: tagName != "",
|
||||
Type: f.Type,
|
||||
ParsedTag: other,
|
||||
nameBytes: []byte(name),
|
||||
}
|
||||
sf.equalFold = foldFunc(sf.nameBytes)
|
||||
sf.Index = append(sf.Index, index...)
|
||||
sf.Index = append(sf.Index, i)
|
||||
return sf
|
||||
}
|
||||
|
||||
// byName sorts fields using the following criteria, in order:
|
||||
// 1. name
|
||||
// 2. embedding depth
|
||||
// 3. tag presence (preferring a tagged field)
|
||||
// 4. index sequence.
|
||||
type byName []Field
|
||||
|
||||
func (x byName) Len() int { return len(x) }
|
||||
|
||||
func (x byName) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
||||
|
||||
func (x byName) Less(i, j int) bool {
|
||||
if x[i].Name != x[j].Name {
|
||||
return x[i].Name < x[j].Name
|
||||
}
|
||||
if len(x[i].Index) != len(x[j].Index) {
|
||||
return len(x[i].Index) < len(x[j].Index)
|
||||
}
|
||||
if x[i].NameFromTag != x[j].NameFromTag {
|
||||
return x[i].NameFromTag
|
||||
}
|
||||
return byIndex(x).Less(i, j)
|
||||
}
|
||||
|
||||
// byIndex sorts field by index sequence.
|
||||
type byIndex []Field
|
||||
|
||||
func (x byIndex) Len() int { return len(x) }
|
||||
|
||||
func (x byIndex) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
||||
|
||||
func (x byIndex) Less(i, j int) bool {
|
||||
xi := x[i].Index
|
||||
xj := x[j].Index
|
||||
ln := len(xi)
|
||||
if l := len(xj); l < ln {
|
||||
ln = l
|
||||
}
|
||||
for k := 0; k < ln; k++ {
|
||||
if xi[k] != xj[k] {
|
||||
return xi[k] < xj[k]
|
||||
}
|
||||
}
|
||||
return len(xi) < len(xj)
|
||||
}
|
||||
|
||||
// dominantField looks through the fields, all of which are known to have the
|
||||
// same name, to find the single field that dominates the others using Go's
|
||||
// embedding rules, modified by the presence of tags. If there are multiple
|
||||
// top-level fields, the boolean will be false: This condition is an error in
|
||||
// Go and we skip all the fields.
|
||||
func dominantField(fs []Field) (Field, bool) {
|
||||
// The fields are sorted in increasing index-length order, then by presence of tag.
|
||||
// That means that the first field is the dominant one. We need only check
|
||||
// for error cases: two fields at top level, either both tagged or neither tagged.
|
||||
if len(fs) > 1 && len(fs[0].Index) == len(fs[1].Index) && fs[0].NameFromTag == fs[1].NameFromTag {
|
||||
return Field{}, false
|
||||
}
|
||||
return fs[0], true
|
||||
}
|
561
vendor/cloud.google.com/go/internal/fields/fields_test.go
generated
vendored
Normal file
561
vendor/cloud.google.com/go/internal/fields/fields_test.go
generated
vendored
Normal file
|
@ -0,0 +1,561 @@
|
|||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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 fields
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type embed1 struct {
|
||||
Em1 int
|
||||
Dup int // annihilates with embed2.Dup
|
||||
Shadow int
|
||||
embed3
|
||||
}
|
||||
|
||||
type embed2 struct {
|
||||
Dup int
|
||||
embed3
|
||||
embed4
|
||||
}
|
||||
|
||||
type embed3 struct {
|
||||
Em3 int // annihilated because embed3 is in both embed1 and embed2
|
||||
embed5
|
||||
}
|
||||
|
||||
type embed4 struct {
|
||||
Em4 int
|
||||
Dup int // annihilation of Dup in embed1, embed2 hides this Dup
|
||||
*embed1 // ignored because it occurs at a higher level
|
||||
}
|
||||
|
||||
type embed5 struct {
|
||||
x int
|
||||
}
|
||||
|
||||
type Anonymous int
|
||||
|
||||
type S1 struct {
|
||||
Exported int
|
||||
unexported int
|
||||
Shadow int // shadows S1.Shadow
|
||||
embed1
|
||||
*embed2
|
||||
Anonymous
|
||||
}
|
||||
|
||||
type Time struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
var intType = reflect.TypeOf(int(0))
|
||||
|
||||
func field(name string, tval interface{}, index ...int) *Field {
|
||||
return &Field{
|
||||
Name: name,
|
||||
Type: reflect.TypeOf(tval),
|
||||
Index: index,
|
||||
}
|
||||
}
|
||||
|
||||
func tfield(name string, tval interface{}, index ...int) *Field {
|
||||
return &Field{
|
||||
Name: name,
|
||||
Type: reflect.TypeOf(tval),
|
||||
Index: index,
|
||||
NameFromTag: true,
|
||||
}
|
||||
}
|
||||
|
||||
func TestFieldsNoTags(t *testing.T) {
|
||||
c := NewCache(nil, nil, nil)
|
||||
got, err := c.Fields(reflect.TypeOf(S1{}))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []*Field{
|
||||
field("Exported", int(0), 0),
|
||||
field("Shadow", int(0), 2),
|
||||
field("Em1", int(0), 3, 0),
|
||||
field("Em4", int(0), 4, 2, 0),
|
||||
field("Anonymous", Anonymous(0), 5),
|
||||
}
|
||||
if msg, ok := compareFields(got, want); !ok {
|
||||
t.Error(msg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgainstJSONEncodingNoTags(t *testing.T) {
|
||||
// Demonstrates that this package produces the same set of fields as encoding/json.
|
||||
s1 := S1{
|
||||
Exported: 1,
|
||||
unexported: 2,
|
||||
Shadow: 3,
|
||||
embed1: embed1{
|
||||
Em1: 4,
|
||||
Dup: 5,
|
||||
Shadow: 6,
|
||||
embed3: embed3{
|
||||
Em3: 7,
|
||||
embed5: embed5{x: 8},
|
||||
},
|
||||
},
|
||||
embed2: &embed2{
|
||||
Dup: 9,
|
||||
embed3: embed3{
|
||||
Em3: 10,
|
||||
embed5: embed5{x: 11},
|
||||
},
|
||||
embed4: embed4{
|
||||
Em4: 12,
|
||||
Dup: 13,
|
||||
embed1: &embed1{Em1: 14},
|
||||
},
|
||||
},
|
||||
Anonymous: Anonymous(15),
|
||||
}
|
||||
var want S1
|
||||
jsonRoundTrip(t, s1, &want)
|
||||
var got S1
|
||||
got.embed2 = &embed2{} // need this because reflection won't create it
|
||||
fields, err := NewCache(nil, nil, nil).Fields(reflect.TypeOf(got))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
setFields(fields, &got, s1)
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("got\n%+v\nwant\n%+v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// Tests use of LeafTypes parameter to NewCache
|
||||
func TestAgainstJSONEncodingEmbeddedTime(t *testing.T) {
|
||||
timeLeafFn := func(t reflect.Type) bool {
|
||||
return t == reflect.TypeOf(time.Time{})
|
||||
}
|
||||
// Demonstrates that this package can produce the same set of
|
||||
// fields as encoding/json for a struct with an embedded time.Time.
|
||||
now := time.Now().UTC()
|
||||
myt := Time{
|
||||
now,
|
||||
}
|
||||
var want Time
|
||||
jsonRoundTrip(t, myt, &want)
|
||||
var got Time
|
||||
fields, err := NewCache(nil, nil, timeLeafFn).Fields(reflect.TypeOf(got))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
setFields(fields, &got, myt)
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("got\n%+v\nwant\n%+v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
type S2 struct {
|
||||
NoTag int
|
||||
XXX int `json:"tag"` // tag name takes precedence
|
||||
Anonymous `json:"anon"` // anonymous non-structs also get their name from the tag
|
||||
unexported int `json:"tag"`
|
||||
Embed `json:"em"` // embedded structs with tags become fields
|
||||
Tag int
|
||||
YYY int `json:"Tag"` // tag takes precedence over untagged field of the same name
|
||||
Empty int `json:""` // empty tag is noop
|
||||
tEmbed1
|
||||
tEmbed2
|
||||
}
|
||||
|
||||
type Embed struct {
|
||||
Em int
|
||||
}
|
||||
|
||||
type tEmbed1 struct {
|
||||
Dup int
|
||||
X int `json:"Dup2"`
|
||||
}
|
||||
|
||||
type tEmbed2 struct {
|
||||
Y int `json:"Dup"` // takes precedence over tEmbed1.Dup because it is tagged
|
||||
Z int `json:"Dup2"` // same name as tEmbed1.X and both tagged, so ignored
|
||||
}
|
||||
|
||||
func jsonTagParser(t reflect.StructTag) (name string, keep bool, other interface{}, err error) {
|
||||
s := t.Get("json")
|
||||
parts := strings.Split(s, ",")
|
||||
if parts[0] == "-" {
|
||||
return "", false, nil, nil
|
||||
}
|
||||
if len(parts) > 1 {
|
||||
other = parts[1:]
|
||||
}
|
||||
return parts[0], true, other, nil
|
||||
}
|
||||
|
||||
func validateFunc(t reflect.Type) (err error) {
|
||||
if t.Kind() != reflect.Struct {
|
||||
return errors.New("non-struct type used")
|
||||
}
|
||||
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
if t.Field(i).Type.Kind() == reflect.Slice {
|
||||
return fmt.Errorf("slice field found at field %s on struct %s", t.Field(i).Name, t.Name())
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestFieldsWithTags(t *testing.T) {
|
||||
got, err := NewCache(jsonTagParser, nil, nil).Fields(reflect.TypeOf(S2{}))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []*Field{
|
||||
field("NoTag", int(0), 0),
|
||||
tfield("tag", int(0), 1),
|
||||
tfield("anon", Anonymous(0), 2),
|
||||
tfield("em", Embed{}, 4),
|
||||
tfield("Tag", int(0), 6),
|
||||
field("Empty", int(0), 7),
|
||||
tfield("Dup", int(0), 8, 0),
|
||||
}
|
||||
if msg, ok := compareFields(got, want); !ok {
|
||||
t.Error(msg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgainstJSONEncodingWithTags(t *testing.T) {
|
||||
// Demonstrates that this package produces the same set of fields as encoding/json.
|
||||
s2 := S2{
|
||||
NoTag: 1,
|
||||
XXX: 2,
|
||||
Anonymous: 3,
|
||||
Embed: Embed{
|
||||
Em: 4,
|
||||
},
|
||||
tEmbed1: tEmbed1{
|
||||
Dup: 5,
|
||||
X: 6,
|
||||
},
|
||||
tEmbed2: tEmbed2{
|
||||
Y: 7,
|
||||
Z: 8,
|
||||
},
|
||||
}
|
||||
var want S2
|
||||
jsonRoundTrip(t, s2, &want)
|
||||
var got S2
|
||||
fields, err := NewCache(jsonTagParser, nil, nil).Fields(reflect.TypeOf(got))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
setFields(fields, &got, s2)
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("got\n%+v\nwant\n%+v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnexportedAnonymousNonStruct(t *testing.T) {
|
||||
// An unexported anonymous non-struct field should not be recorded.
|
||||
// This is currently a bug in encoding/json.
|
||||
// https://github.com/golang/go/issues/18009
|
||||
type (
|
||||
u int
|
||||
v int
|
||||
S struct {
|
||||
u
|
||||
v `json:"x"`
|
||||
int
|
||||
}
|
||||
)
|
||||
|
||||
got, err := NewCache(jsonTagParser, nil, nil).Fields(reflect.TypeOf(S{}))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 0 {
|
||||
t.Errorf("got %d fields, want 0", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnexportedAnonymousStruct(t *testing.T) {
|
||||
// An unexported anonymous struct with a tag is ignored.
|
||||
// This is currently a bug in encoding/json.
|
||||
// https://github.com/golang/go/issues/18009
|
||||
type (
|
||||
s1 struct{ X int }
|
||||
S2 struct {
|
||||
s1 `json:"Y"`
|
||||
}
|
||||
)
|
||||
got, err := NewCache(jsonTagParser, nil, nil).Fields(reflect.TypeOf(S2{}))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 0 {
|
||||
t.Errorf("got %d fields, want 0", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestDominantField(t *testing.T) {
|
||||
// With fields sorted by index length and then by tag presence,
|
||||
// the dominant field is always the first. Make sure all error
|
||||
// cases are caught.
|
||||
for _, test := range []struct {
|
||||
fields []Field
|
||||
wantOK bool
|
||||
}{
|
||||
// A single field is OK.
|
||||
{[]Field{{Index: []int{0}}}, true},
|
||||
{[]Field{{Index: []int{0}, NameFromTag: true}}, true},
|
||||
// A single field at top level is OK.
|
||||
{[]Field{{Index: []int{0}}, {Index: []int{1, 0}}}, true},
|
||||
{[]Field{{Index: []int{0}}, {Index: []int{1, 0}, NameFromTag: true}}, true},
|
||||
{[]Field{{Index: []int{0}, NameFromTag: true}, {Index: []int{1, 0}, NameFromTag: true}}, true},
|
||||
// A single tagged field is OK.
|
||||
{[]Field{{Index: []int{0}, NameFromTag: true}, {Index: []int{1}}}, true},
|
||||
// Two untagged fields at the same level is an error.
|
||||
{[]Field{{Index: []int{0}}, {Index: []int{1}}}, false},
|
||||
// Two tagged fields at the same level is an error.
|
||||
{[]Field{{Index: []int{0}, NameFromTag: true}, {Index: []int{1}, NameFromTag: true}}, false},
|
||||
} {
|
||||
_, gotOK := dominantField(test.fields)
|
||||
if gotOK != test.wantOK {
|
||||
t.Errorf("%v: got %t, want %t", test.fields, gotOK, test.wantOK)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIgnore(t *testing.T) {
|
||||
type S struct {
|
||||
X int `json:"-"`
|
||||
}
|
||||
got, err := NewCache(jsonTagParser, nil, nil).Fields(reflect.TypeOf(S{}))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 0 {
|
||||
t.Errorf("got %d fields, want 0", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
func TestParsedTag(t *testing.T) {
|
||||
type S struct {
|
||||
X int `json:"name,omitempty"`
|
||||
}
|
||||
got, err := NewCache(jsonTagParser, nil, nil).Fields(reflect.TypeOf(S{}))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []*Field{
|
||||
{Name: "name", NameFromTag: true, Type: intType,
|
||||
Index: []int{0}, ParsedTag: []string{"omitempty"}},
|
||||
}
|
||||
if msg, ok := compareFields(got, want); !ok {
|
||||
t.Error(msg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateFunc(t *testing.T) {
|
||||
type MyInvalidStruct struct {
|
||||
A string
|
||||
B []int
|
||||
}
|
||||
|
||||
_, err := NewCache(nil, validateFunc, nil).Fields(reflect.TypeOf(MyInvalidStruct{}))
|
||||
if err == nil {
|
||||
t.Fatal("expected error, got nil")
|
||||
}
|
||||
|
||||
type MyValidStruct struct {
|
||||
A string
|
||||
B int
|
||||
}
|
||||
_, err = NewCache(nil, validateFunc, nil).Fields(reflect.TypeOf(MyValidStruct{}))
|
||||
if err != nil {
|
||||
t.Fatalf("expected nil, got error: %s\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func compareFields(got []Field, want []*Field) (msg string, ok bool) {
|
||||
if len(got) != len(want) {
|
||||
return fmt.Sprintf("got %d fields, want %d", len(got), len(want)), false
|
||||
}
|
||||
for i, g := range got {
|
||||
w := *want[i]
|
||||
if !fieldsEqual(&g, &w) {
|
||||
return fmt.Sprintf("got %+v, want %+v", g, w), false
|
||||
}
|
||||
}
|
||||
return "", true
|
||||
}
|
||||
|
||||
// Need this because Field contains a function, which cannot be compared even
|
||||
// by reflect.DeepEqual.
|
||||
func fieldsEqual(f1, f2 *Field) bool {
|
||||
if f1 == nil || f2 == nil {
|
||||
return f1 == f2
|
||||
}
|
||||
return f1.Name == f2.Name &&
|
||||
f1.NameFromTag == f2.NameFromTag &&
|
||||
f1.Type == f2.Type &&
|
||||
reflect.DeepEqual(f1.ParsedTag, f2.ParsedTag)
|
||||
}
|
||||
|
||||
// Set the fields of dst from those of src.
|
||||
// dst must be a pointer to a struct value.
|
||||
// src must be a struct value.
|
||||
func setFields(fields []Field, dst, src interface{}) {
|
||||
vsrc := reflect.ValueOf(src)
|
||||
vdst := reflect.ValueOf(dst).Elem()
|
||||
for _, f := range fields {
|
||||
fdst := vdst.FieldByIndex(f.Index)
|
||||
fsrc := vsrc.FieldByIndex(f.Index)
|
||||
fdst.Set(fsrc)
|
||||
}
|
||||
}
|
||||
|
||||
func jsonRoundTrip(t *testing.T, in, out interface{}) {
|
||||
bytes, err := json.Marshal(in)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := json.Unmarshal(bytes, out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
type S3 struct {
|
||||
S4
|
||||
Abc int
|
||||
AbC int
|
||||
Tag int
|
||||
X int `json:"Tag"`
|
||||
unexported int
|
||||
}
|
||||
|
||||
type S4 struct {
|
||||
ABc int
|
||||
Y int `json:"Abc"` // ignored because of top-level Abc
|
||||
}
|
||||
|
||||
func TestMatchingField(t *testing.T) {
|
||||
fields, err := NewCache(jsonTagParser, nil, nil).Fields(reflect.TypeOf(S3{}))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
want *Field
|
||||
}{
|
||||
// Exact match wins.
|
||||
{"Abc", field("Abc", int(0), 1)},
|
||||
{"AbC", field("AbC", int(0), 2)},
|
||||
{"ABc", field("ABc", int(0), 0, 0)},
|
||||
// If there are multiple matches but no exact match or tag,
|
||||
// the first field wins, lexicographically by index.
|
||||
// Here, "ABc" is at a deeper embedding level, but since S4 appears
|
||||
// first in S3, its index precedes the other fields of S3.
|
||||
{"abc", field("ABc", int(0), 0, 0)},
|
||||
// Tag name takes precedence over untagged field of the same name.
|
||||
{"Tag", tfield("Tag", int(0), 4)},
|
||||
// Unexported fields disappear.
|
||||
{"unexported", nil},
|
||||
// Untagged embedded structs disappear.
|
||||
{"S4", nil},
|
||||
} {
|
||||
if got := fields.Match(test.name); !fieldsEqual(got, test.want) {
|
||||
t.Errorf("match %q:\ngot %+v\nwant %+v", test.name, got, test.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgainstJSONMatchingField(t *testing.T) {
|
||||
s3 := S3{
|
||||
S4: S4{ABc: 1, Y: 2},
|
||||
Abc: 3,
|
||||
AbC: 4,
|
||||
Tag: 5,
|
||||
X: 6,
|
||||
unexported: 7,
|
||||
}
|
||||
var want S3
|
||||
jsonRoundTrip(t, s3, &want)
|
||||
v := reflect.ValueOf(want)
|
||||
fields, err := NewCache(jsonTagParser, nil, nil).Fields(reflect.TypeOf(S3{}))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
got int
|
||||
}{
|
||||
{"Abc", 3},
|
||||
{"AbC", 4},
|
||||
{"ABc", 1},
|
||||
{"abc", 1},
|
||||
{"Tag", 6},
|
||||
} {
|
||||
f := fields.Match(test.name)
|
||||
if f == nil {
|
||||
t.Fatalf("%s: no match", test.name)
|
||||
}
|
||||
w := v.FieldByIndex(f.Index).Interface()
|
||||
if test.got != w {
|
||||
t.Errorf("%s: got %d, want %d", test.name, test.got, w)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTagErrors(t *testing.T) {
|
||||
called := false
|
||||
c := NewCache(func(t reflect.StructTag) (string, bool, interface{}, error) {
|
||||
called = true
|
||||
s := t.Get("f")
|
||||
if s == "bad" {
|
||||
return "", false, nil, errors.New("error")
|
||||
}
|
||||
return s, true, nil, nil
|
||||
}, nil, nil)
|
||||
|
||||
type T struct {
|
||||
X int `f:"ok"`
|
||||
Y int `f:"bad"`
|
||||
}
|
||||
|
||||
_, err := c.Fields(reflect.TypeOf(T{}))
|
||||
if !called {
|
||||
t.Fatal("tag parser not called")
|
||||
}
|
||||
if err == nil {
|
||||
t.Error("want error, got nil")
|
||||
}
|
||||
// Second time, we should cache the error.
|
||||
called = false
|
||||
_, err = c.Fields(reflect.TypeOf(T{}))
|
||||
if called {
|
||||
t.Fatal("tag parser called on second time")
|
||||
}
|
||||
if err == nil {
|
||||
t.Error("want error, got nil")
|
||||
}
|
||||
}
|
156
vendor/cloud.google.com/go/internal/fields/fold.go
generated
vendored
Normal file
156
vendor/cloud.google.com/go/internal/fields/fold.go
generated
vendored
Normal file
|
@ -0,0 +1,156 @@
|
|||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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 fields
|
||||
|
||||
// This file was copied from https://go.googlesource.com/go/+/go1.7.3/src/encoding/json/fold.go.
|
||||
// Only the license and package were changed.
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
const (
|
||||
caseMask = ^byte(0x20) // Mask to ignore case in ASCII.
|
||||
kelvin = '\u212a'
|
||||
smallLongEss = '\u017f'
|
||||
)
|
||||
|
||||
// foldFunc returns one of four different case folding equivalence
|
||||
// functions, from most general (and slow) to fastest:
|
||||
//
|
||||
// 1) bytes.EqualFold, if the key s contains any non-ASCII UTF-8
|
||||
// 2) equalFoldRight, if s contains special folding ASCII ('k', 'K', 's', 'S')
|
||||
// 3) asciiEqualFold, no special, but includes non-letters (including _)
|
||||
// 4) simpleLetterEqualFold, no specials, no non-letters.
|
||||
//
|
||||
// The letters S and K are special because they map to 3 runes, not just 2:
|
||||
// * S maps to s and to U+017F 'ſ' Latin small letter long s
|
||||
// * k maps to K and to U+212A 'K' Kelvin sign
|
||||
// See https://play.golang.org/p/tTxjOc0OGo
|
||||
//
|
||||
// The returned function is specialized for matching against s and
|
||||
// should only be given s. It's not curried for performance reasons.
|
||||
func foldFunc(s []byte) func(s, t []byte) bool {
|
||||
nonLetter := false
|
||||
special := false // special letter
|
||||
for _, b := range s {
|
||||
if b >= utf8.RuneSelf {
|
||||
return bytes.EqualFold
|
||||
}
|
||||
upper := b & caseMask
|
||||
if upper < 'A' || upper > 'Z' {
|
||||
nonLetter = true
|
||||
} else if upper == 'K' || upper == 'S' {
|
||||
// See above for why these letters are special.
|
||||
special = true
|
||||
}
|
||||
}
|
||||
if special {
|
||||
return equalFoldRight
|
||||
}
|
||||
if nonLetter {
|
||||
return asciiEqualFold
|
||||
}
|
||||
return simpleLetterEqualFold
|
||||
}
|
||||
|
||||
// equalFoldRight is a specialization of bytes.EqualFold when s is
|
||||
// known to be all ASCII (including punctuation), but contains an 's',
|
||||
// 'S', 'k', or 'K', requiring a Unicode fold on the bytes in t.
|
||||
// See comments on foldFunc.
|
||||
func equalFoldRight(s, t []byte) bool {
|
||||
for _, sb := range s {
|
||||
if len(t) == 0 {
|
||||
return false
|
||||
}
|
||||
tb := t[0]
|
||||
if tb < utf8.RuneSelf {
|
||||
if sb != tb {
|
||||
sbUpper := sb & caseMask
|
||||
if 'A' <= sbUpper && sbUpper <= 'Z' {
|
||||
if sbUpper != tb&caseMask {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
t = t[1:]
|
||||
continue
|
||||
}
|
||||
// sb is ASCII and t is not. t must be either kelvin
|
||||
// sign or long s; sb must be s, S, k, or K.
|
||||
tr, size := utf8.DecodeRune(t)
|
||||
switch sb {
|
||||
case 's', 'S':
|
||||
if tr != smallLongEss {
|
||||
return false
|
||||
}
|
||||
case 'k', 'K':
|
||||
if tr != kelvin {
|
||||
return false
|
||||
}
|
||||
default:
|
||||
return false
|
||||
}
|
||||
t = t[size:]
|
||||
|
||||
}
|
||||
if len(t) > 0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// asciiEqualFold is a specialization of bytes.EqualFold for use when
|
||||
// s is all ASCII (but may contain non-letters) and contains no
|
||||
// special-folding letters.
|
||||
// See comments on foldFunc.
|
||||
func asciiEqualFold(s, t []byte) bool {
|
||||
if len(s) != len(t) {
|
||||
return false
|
||||
}
|
||||
for i, sb := range s {
|
||||
tb := t[i]
|
||||
if sb == tb {
|
||||
continue
|
||||
}
|
||||
if ('a' <= sb && sb <= 'z') || ('A' <= sb && sb <= 'Z') {
|
||||
if sb&caseMask != tb&caseMask {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// simpleLetterEqualFold is a specialization of bytes.EqualFold for
|
||||
// use when s is all ASCII letters (no underscores, etc) and also
|
||||
// doesn't contain 'k', 'K', 's', or 'S'.
|
||||
// See comments on foldFunc.
|
||||
func simpleLetterEqualFold(s, t []byte) bool {
|
||||
if len(s) != len(t) {
|
||||
return false
|
||||
}
|
||||
for i, b := range s {
|
||||
if b&caseMask != t[i]&caseMask {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
129
vendor/cloud.google.com/go/internal/fields/fold_test.go
generated
vendored
Normal file
129
vendor/cloud.google.com/go/internal/fields/fold_test.go
generated
vendored
Normal file
|
@ -0,0 +1,129 @@
|
|||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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 fields
|
||||
|
||||
// This file was copied from https://go.googlesource.com/go/+/go1.7.3/src/encoding/json/fold_test.go.
|
||||
// Only the license and package were changed.
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
var foldTests = []struct {
|
||||
fn func(s, t []byte) bool
|
||||
s, t string
|
||||
want bool
|
||||
}{
|
||||
{equalFoldRight, "", "", true},
|
||||
{equalFoldRight, "a", "a", true},
|
||||
{equalFoldRight, "", "a", false},
|
||||
{equalFoldRight, "a", "", false},
|
||||
{equalFoldRight, "a", "A", true},
|
||||
{equalFoldRight, "AB", "ab", true},
|
||||
{equalFoldRight, "AB", "ac", false},
|
||||
{equalFoldRight, "sbkKc", "ſbKKc", true},
|
||||
{equalFoldRight, "SbKkc", "ſbKKc", true},
|
||||
{equalFoldRight, "SbKkc", "ſbKK", false},
|
||||
{equalFoldRight, "e", "é", false},
|
||||
{equalFoldRight, "s", "S", true},
|
||||
|
||||
{simpleLetterEqualFold, "", "", true},
|
||||
{simpleLetterEqualFold, "abc", "abc", true},
|
||||
{simpleLetterEqualFold, "abc", "ABC", true},
|
||||
{simpleLetterEqualFold, "abc", "ABCD", false},
|
||||
{simpleLetterEqualFold, "abc", "xxx", false},
|
||||
|
||||
{asciiEqualFold, "a_B", "A_b", true},
|
||||
{asciiEqualFold, "aa@", "aa`", false}, // verify 0x40 and 0x60 aren't case-equivalent
|
||||
}
|
||||
|
||||
func TestFold(t *testing.T) {
|
||||
for i, tt := range foldTests {
|
||||
if got := tt.fn([]byte(tt.s), []byte(tt.t)); got != tt.want {
|
||||
t.Errorf("%d. %q, %q = %v; want %v", i, tt.s, tt.t, got, tt.want)
|
||||
}
|
||||
truth := strings.EqualFold(tt.s, tt.t)
|
||||
if truth != tt.want {
|
||||
t.Errorf("strings.EqualFold doesn't agree with case %d", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFoldAgainstUnicode(t *testing.T) {
|
||||
const bufSize = 5
|
||||
buf1 := make([]byte, 0, bufSize)
|
||||
buf2 := make([]byte, 0, bufSize)
|
||||
var runes []rune
|
||||
for i := 0x20; i <= 0x7f; i++ {
|
||||
runes = append(runes, rune(i))
|
||||
}
|
||||
runes = append(runes, kelvin, smallLongEss)
|
||||
|
||||
funcs := []struct {
|
||||
name string
|
||||
fold func(s, t []byte) bool
|
||||
letter bool // must be ASCII letter
|
||||
simple bool // must be simple ASCII letter (not 'S' or 'K')
|
||||
}{
|
||||
{
|
||||
name: "equalFoldRight",
|
||||
fold: equalFoldRight,
|
||||
},
|
||||
{
|
||||
name: "asciiEqualFold",
|
||||
fold: asciiEqualFold,
|
||||
simple: true,
|
||||
},
|
||||
{
|
||||
name: "simpleLetterEqualFold",
|
||||
fold: simpleLetterEqualFold,
|
||||
simple: true,
|
||||
letter: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, ff := range funcs {
|
||||
for _, r := range runes {
|
||||
if r >= utf8.RuneSelf {
|
||||
continue
|
||||
}
|
||||
if ff.letter && !isASCIILetter(byte(r)) {
|
||||
continue
|
||||
}
|
||||
if ff.simple && (r == 's' || r == 'S' || r == 'k' || r == 'K') {
|
||||
continue
|
||||
}
|
||||
for _, r2 := range runes {
|
||||
buf1 := append(buf1[:0], 'x')
|
||||
buf2 := append(buf2[:0], 'x')
|
||||
buf1 = buf1[:1+utf8.EncodeRune(buf1[1:bufSize], r)]
|
||||
buf2 = buf2[:1+utf8.EncodeRune(buf2[1:bufSize], r2)]
|
||||
buf1 = append(buf1, 'x')
|
||||
buf2 = append(buf2, 'x')
|
||||
want := bytes.EqualFold(buf1, buf2)
|
||||
if got := ff.fold(buf1, buf2); got != want {
|
||||
t.Errorf("%s(%q, %q) = %v; want %v", ff.name, buf1, buf2, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func isASCIILetter(b byte) bool {
|
||||
return ('A' <= b && b <= 'Z') || ('a' <= b && b <= 'z')
|
||||
}
|
41
vendor/cloud.google.com/go/internal/kokoro/build.sh
generated
vendored
Executable file
41
vendor/cloud.google.com/go/internal/kokoro/build.sh
generated
vendored
Executable file
|
@ -0,0 +1,41 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Fail on any error
|
||||
set -eo pipefail
|
||||
|
||||
# Display commands being run
|
||||
set -x
|
||||
|
||||
# cd to project dir on Kokoro instance
|
||||
cd git/gocloud
|
||||
|
||||
go version
|
||||
|
||||
# Set $GOPATH
|
||||
export GOPATH="$HOME/go"
|
||||
GOCLOUD_HOME=$GOPATH/src/cloud.google.com/go
|
||||
mkdir -p $GOCLOUD_HOME
|
||||
|
||||
# Move code into $GOPATH and get dependencies
|
||||
cp -R ./* $GOCLOUD_HOME
|
||||
cd $GOCLOUD_HOME
|
||||
go get -v ./...
|
||||
|
||||
# # Don't run integration tests until we can protect against code from
|
||||
# # untrusted forks reading and storing our service account key.
|
||||
# cd internal/kokoro
|
||||
# # Don't print out encryption keys, etc
|
||||
# set +x
|
||||
# key=$(cat $KOKORO_ARTIFACTS_DIR/keystore/*_encrypted_ba2d6f7723ed_key)
|
||||
# iv=$(cat $KOKORO_ARTIFACTS_DIR/keystore/*_encrypted_ba2d6f7723ed_iv)
|
||||
# pass=$(cat $KOKORO_ARTIFACTS_DIR/keystore/*_encrypted_ba2d6f7723ed_pass)
|
||||
|
||||
# openssl aes-256-cbc -K $key -iv $iv -pass pass:$pass -in kokoro-key.json.enc -out key.json -d
|
||||
# set -x
|
||||
|
||||
# export GCLOUD_TESTS_GOLANG_KEY="$(pwd)/key.json"
|
||||
# export GCLOUD_TESTS_GOLANG_PROJECT_ID="dulcet-port-762"
|
||||
# cd $GOCLOUD_HOME
|
||||
|
||||
# Run tests and tee output to log file, to be pushed to GCS as artifact.
|
||||
go test -race -v -short ./... 2>&1 | tee $KOKORO_ARTIFACTS_DIR/$KOKORO_GERRIT_REVISION.log
|
BIN
vendor/cloud.google.com/go/internal/kokoro/kokoro-key.json.enc
generated
vendored
Normal file
BIN
vendor/cloud.google.com/go/internal/kokoro/kokoro-key.json.enc
generated
vendored
Normal file
Binary file not shown.
94
vendor/cloud.google.com/go/internal/optional/optional.go
generated
vendored
Normal file
94
vendor/cloud.google.com/go/internal/optional/optional.go
generated
vendored
Normal file
|
@ -0,0 +1,94 @@
|
|||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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 optional provides versions of primitive types that can
|
||||
// be nil. These are useful in methods that update some of an API object's
|
||||
// fields.
|
||||
package optional
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type (
|
||||
// Bool is either a bool or nil.
|
||||
Bool interface{}
|
||||
|
||||
// String is either a string or nil.
|
||||
String interface{}
|
||||
|
||||
// Int is either an int or nil.
|
||||
Int interface{}
|
||||
|
||||
// Uint is either a uint or nil.
|
||||
Uint interface{}
|
||||
|
||||
// Float64 is either a float64 or nil.
|
||||
Float64 interface{}
|
||||
)
|
||||
|
||||
// ToBool returns its argument as a bool.
|
||||
// It panics if its argument is nil or not a bool.
|
||||
func ToBool(v Bool) bool {
|
||||
x, ok := v.(bool)
|
||||
if !ok {
|
||||
doPanic("Bool", v)
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
// ToString returns its argument as a string.
|
||||
// It panics if its argument is nil or not a string.
|
||||
func ToString(v String) string {
|
||||
x, ok := v.(string)
|
||||
if !ok {
|
||||
doPanic("String", v)
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
// ToInt returns its argument as an int.
|
||||
// It panics if its argument is nil or not an int.
|
||||
func ToInt(v Int) int {
|
||||
x, ok := v.(int)
|
||||
if !ok {
|
||||
doPanic("Int", v)
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
// ToUint returns its argument as a uint.
|
||||
// It panics if its argument is nil or not a uint.
|
||||
func ToUint(v Uint) uint {
|
||||
x, ok := v.(uint)
|
||||
if !ok {
|
||||
doPanic("Uint", v)
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
// ToFloat64 returns its argument as a float64.
|
||||
// It panics if its argument is nil or not a float64.
|
||||
func ToFloat64(v Float64) float64 {
|
||||
x, ok := v.(float64)
|
||||
if !ok {
|
||||
doPanic("Float64", v)
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
func doPanic(capType string, v interface{}) {
|
||||
panic(fmt.Sprintf("optional.%s value should be %s, got %T", capType, strings.ToLower(capType), v))
|
||||
}
|
64
vendor/cloud.google.com/go/internal/optional/optional_test.go
generated
vendored
Normal file
64
vendor/cloud.google.com/go/internal/optional/optional_test.go
generated
vendored
Normal file
|
@ -0,0 +1,64 @@
|
|||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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 optional
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestConvertSuccess(t *testing.T) {
|
||||
if got, want := ToBool(false), false; got != want {
|
||||
t.Errorf("got %v, want %v", got, want)
|
||||
}
|
||||
if got, want := ToString(""), ""; got != want {
|
||||
t.Errorf("got %v, want %v", got, want)
|
||||
}
|
||||
if got, want := ToInt(0), 0; got != want {
|
||||
t.Errorf("got %v, want %v", got, want)
|
||||
}
|
||||
if got, want := ToUint(uint(0)), uint(0); got != want {
|
||||
t.Errorf("got %v, want %v", got, want)
|
||||
}
|
||||
if got, want := ToFloat64(0.0), 0.0; got != want {
|
||||
t.Errorf("got %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertFailure(t *testing.T) {
|
||||
for _, f := range []func(){
|
||||
func() { ToBool(nil) },
|
||||
func() { ToBool(3) },
|
||||
func() { ToString(nil) },
|
||||
func() { ToString(3) },
|
||||
func() { ToInt(nil) },
|
||||
func() { ToInt("") },
|
||||
func() { ToUint(nil) },
|
||||
func() { ToUint("") },
|
||||
func() { ToFloat64(nil) },
|
||||
func() { ToFloat64("") },
|
||||
} {
|
||||
if !panics(f) {
|
||||
t.Error("got no panic, want panic")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func panics(f func()) (b bool) {
|
||||
defer func() {
|
||||
if recover() != nil {
|
||||
b = true
|
||||
}
|
||||
}()
|
||||
f()
|
||||
return false
|
||||
}
|
78
vendor/cloud.google.com/go/internal/pretty/diff.go
generated
vendored
Normal file
78
vendor/cloud.google.com/go/internal/pretty/diff.go
generated
vendored
Normal file
|
@ -0,0 +1,78 @@
|
|||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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 pretty
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// Diff compares the pretty-printed representation of two values. The second
|
||||
// return value reports whether the two values' representations are identical.
|
||||
// If it is false, the first return value contains the diffs.
|
||||
//
|
||||
// The output labels the first value "want" and the second "got".
|
||||
//
|
||||
// Diff works by invoking the "diff" command. It will only succeed in
|
||||
// environments where "diff" is on the shell path.
|
||||
func Diff(want, got interface{}) (string, bool, error) {
|
||||
fname1, err := writeToTemp(want)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
defer os.Remove(fname1)
|
||||
|
||||
fname2, err := writeToTemp(got)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
defer os.Remove(fname2)
|
||||
|
||||
cmd := exec.Command("diff", "-u", "--label=want", "--label=got", fname1, fname2)
|
||||
out, err := cmd.Output()
|
||||
if err == nil {
|
||||
return string(out), true, nil
|
||||
}
|
||||
eerr, ok := err.(*exec.ExitError)
|
||||
if !ok {
|
||||
return "", false, err
|
||||
}
|
||||
ws, ok := eerr.Sys().(syscall.WaitStatus)
|
||||
if !ok {
|
||||
return "", false, err
|
||||
}
|
||||
if ws.ExitStatus() != 1 {
|
||||
return "", false, err
|
||||
}
|
||||
// Exit status of 1 means no error, but diffs were found.
|
||||
return string(out), false, nil
|
||||
}
|
||||
|
||||
func writeToTemp(v interface{}) (string, error) {
|
||||
f, err := ioutil.TempFile("", "prettyDiff")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err := fmt.Fprintf(f, "%+v\n", Value(v)); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := f.Close(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return f.Name(), nil
|
||||
}
|
50
vendor/cloud.google.com/go/internal/pretty/diff_test.go
generated
vendored
Normal file
50
vendor/cloud.google.com/go/internal/pretty/diff_test.go
generated
vendored
Normal file
|
@ -0,0 +1,50 @@
|
|||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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 pretty
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestDiff(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
v1, v2 interface{}
|
||||
ok bool
|
||||
want string
|
||||
}{
|
||||
{5, 5, true, ""},
|
||||
{"foo", "foo", true, ""},
|
||||
{[]int{1, 2, 3}, []int{1, 0, 3}, false, `--- want
|
||||
+++ got
|
||||
@@ -1,5 +1,5 @@
|
||||
[]int{
|
||||
1,
|
||||
- 2,
|
||||
+ 0,
|
||||
3,
|
||||
}
|
||||
`},
|
||||
} {
|
||||
got, ok, err := Diff(test.v1, test.v2)
|
||||
if err != nil {
|
||||
t.Errorf("%v vs. %v: %v", test.v1, test.v2, err)
|
||||
continue
|
||||
}
|
||||
if ok != test.ok {
|
||||
t.Errorf("%v vs. %v: got %t, want %t", test.v1, test.v2, ok, test.ok)
|
||||
}
|
||||
if got != test.want {
|
||||
t.Errorf("%v vs. %v: got:\n%q\nwant:\n%q", test.v1, test.v2, got, test.want)
|
||||
}
|
||||
}
|
||||
}
|
241
vendor/cloud.google.com/go/internal/pretty/pretty.go
generated
vendored
Normal file
241
vendor/cloud.google.com/go/internal/pretty/pretty.go
generated
vendored
Normal file
|
@ -0,0 +1,241 @@
|
|||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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 pretty implements a simple pretty-printer. It is intended for
|
||||
// debugging the output of tests.
|
||||
//
|
||||
// It follows pointers and produces multi-line output for complex values like
|
||||
// slices, maps and structs.
|
||||
package pretty
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Indent is the string output at each level of indentation.
|
||||
var Indent = " "
|
||||
|
||||
// Value returns a value that will print prettily when used as an
|
||||
// argument for the %v or %s format specifiers.
|
||||
// With no flags, struct fields and map keys with default values are omitted.
|
||||
// With the '+' or '#' flags, all values are displayed.
|
||||
//
|
||||
// This package does not detect cycles. Attempting to print a Value that
|
||||
// contains cycles will result in unbounded recursion.
|
||||
func Value(v interface{}) val { return val{v: v} }
|
||||
|
||||
type val struct{ v interface{} }
|
||||
|
||||
// Format implements the fmt.Formatter interface.
|
||||
func (v val) Format(s fmt.State, c rune) {
|
||||
if c == 'v' || c == 's' {
|
||||
fprint(s, reflect.ValueOf(v.v), state{
|
||||
defaults: s.Flag('+') || s.Flag('#'),
|
||||
})
|
||||
} else {
|
||||
fmt.Fprintf(s, "%%!%c(pretty.Val)", c)
|
||||
}
|
||||
}
|
||||
|
||||
type state struct {
|
||||
level int
|
||||
prefix, suffix string
|
||||
defaults bool
|
||||
}
|
||||
|
||||
func fprint(w io.Writer, v reflect.Value, s state) {
|
||||
indent := strings.Repeat(Indent, s.level)
|
||||
fmt.Fprintf(w, "%s%s", indent, s.prefix)
|
||||
if isNil(v) {
|
||||
fmt.Fprintf(w, "nil%s", s.suffix)
|
||||
return
|
||||
}
|
||||
if v.Type().Kind() == reflect.Interface {
|
||||
v = v.Elem()
|
||||
}
|
||||
for v.Type().Kind() == reflect.Ptr {
|
||||
fmt.Fprintf(w, "&")
|
||||
v = v.Elem()
|
||||
}
|
||||
switch v.Type().Kind() {
|
||||
default:
|
||||
fmt.Fprintf(w, "%s%s", short(v), s.suffix)
|
||||
|
||||
case reflect.Array:
|
||||
fmt.Fprintf(w, "%s{\n", v.Type())
|
||||
for i := 0; i < v.Len(); i++ {
|
||||
fprint(w, v.Index(i), state{
|
||||
level: s.level + 1,
|
||||
prefix: "",
|
||||
suffix: ",",
|
||||
defaults: s.defaults,
|
||||
})
|
||||
fmt.Fprintln(w)
|
||||
}
|
||||
fmt.Fprintf(w, "%s}", indent)
|
||||
|
||||
case reflect.Slice:
|
||||
fmt.Fprintf(w, "%s{", v.Type())
|
||||
if v.Len() > 0 {
|
||||
fmt.Fprintln(w)
|
||||
for i := 0; i < v.Len(); i++ {
|
||||
fprint(w, v.Index(i), state{
|
||||
level: s.level + 1,
|
||||
prefix: "",
|
||||
suffix: ",",
|
||||
defaults: s.defaults,
|
||||
})
|
||||
fmt.Fprintln(w)
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(w, "%s}%s", indent, s.suffix)
|
||||
|
||||
case reflect.Map:
|
||||
fmt.Fprintf(w, "%s{", v.Type())
|
||||
if v.Len() > 0 {
|
||||
fmt.Fprintln(w)
|
||||
keys := v.MapKeys()
|
||||
maybeSort(keys, v.Type().Key())
|
||||
for _, key := range keys {
|
||||
val := v.MapIndex(key)
|
||||
if s.defaults || !isDefault(val) {
|
||||
fprint(w, val, state{
|
||||
level: s.level + 1,
|
||||
prefix: short(key) + ": ",
|
||||
suffix: ",",
|
||||
defaults: s.defaults,
|
||||
})
|
||||
fmt.Fprintln(w)
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(w, "%s}%s", indent, s.suffix)
|
||||
|
||||
case reflect.Struct:
|
||||
t := v.Type()
|
||||
fmt.Fprintf(w, "%s{\n", t)
|
||||
for i := 0; i < t.NumField(); i++ {
|
||||
f := v.Field(i)
|
||||
if s.defaults || !isDefault(f) {
|
||||
fprint(w, f, state{
|
||||
level: s.level + 1,
|
||||
prefix: t.Field(i).Name + ": ",
|
||||
suffix: ",",
|
||||
defaults: s.defaults,
|
||||
})
|
||||
fmt.Fprintln(w)
|
||||
}
|
||||
}
|
||||
fmt.Fprintf(w, "%s}%s", indent, s.suffix)
|
||||
}
|
||||
}
|
||||
|
||||
func isNil(v reflect.Value) bool {
|
||||
if !v.IsValid() {
|
||||
return true
|
||||
}
|
||||
switch v.Type().Kind() {
|
||||
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
|
||||
return v.IsNil()
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func isDefault(v reflect.Value) bool {
|
||||
if !v.IsValid() {
|
||||
return true
|
||||
}
|
||||
t := v.Type()
|
||||
switch t.Kind() {
|
||||
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
|
||||
return v.IsNil()
|
||||
default:
|
||||
if !v.CanInterface() {
|
||||
return false
|
||||
}
|
||||
return t.Comparable() && v.Interface() == reflect.Zero(t).Interface()
|
||||
}
|
||||
}
|
||||
|
||||
// short returns a short, one-line string for v.
|
||||
func short(v reflect.Value) string {
|
||||
if !v.IsValid() {
|
||||
return "nil"
|
||||
}
|
||||
if v.Type().Kind() == reflect.String {
|
||||
return fmt.Sprintf("%q", v)
|
||||
}
|
||||
return fmt.Sprintf("%v", v)
|
||||
}
|
||||
|
||||
func indent(w io.Writer, level int) {
|
||||
for i := 0; i < level; i++ {
|
||||
io.WriteString(w, Indent) // ignore errors
|
||||
}
|
||||
}
|
||||
|
||||
func maybeSort(vs []reflect.Value, t reflect.Type) {
|
||||
if less := lessFunc(t); less != nil {
|
||||
sort.Sort(&sorter{vs, less})
|
||||
}
|
||||
}
|
||||
|
||||
// lessFunc returns a function that implements the "<" operator
|
||||
// for the given type, or nil if the type doesn't support "<" .
|
||||
func lessFunc(t reflect.Type) func(v1, v2 interface{}) bool {
|
||||
switch t.Kind() {
|
||||
case reflect.String:
|
||||
return func(v1, v2 interface{}) bool { return v1.(string) < v2.(string) }
|
||||
case reflect.Int:
|
||||
return func(v1, v2 interface{}) bool { return v1.(int) < v2.(int) }
|
||||
case reflect.Int8:
|
||||
return func(v1, v2 interface{}) bool { return v1.(int8) < v2.(int8) }
|
||||
case reflect.Int16:
|
||||
return func(v1, v2 interface{}) bool { return v1.(int16) < v2.(int16) }
|
||||
case reflect.Int32:
|
||||
return func(v1, v2 interface{}) bool { return v1.(int32) < v2.(int32) }
|
||||
case reflect.Int64:
|
||||
return func(v1, v2 interface{}) bool { return v1.(int64) < v2.(int64) }
|
||||
case reflect.Uint:
|
||||
return func(v1, v2 interface{}) bool { return v1.(uint) < v2.(uint) }
|
||||
case reflect.Uint8:
|
||||
return func(v1, v2 interface{}) bool { return v1.(uint8) < v2.(uint8) }
|
||||
case reflect.Uint16:
|
||||
return func(v1, v2 interface{}) bool { return v1.(uint16) < v2.(uint16) }
|
||||
case reflect.Uint32:
|
||||
return func(v1, v2 interface{}) bool { return v1.(uint32) < v2.(uint32) }
|
||||
case reflect.Uint64:
|
||||
return func(v1, v2 interface{}) bool { return v1.(uint64) < v2.(uint64) }
|
||||
case reflect.Float32:
|
||||
return func(v1, v2 interface{}) bool { return v1.(float32) < v2.(float32) }
|
||||
case reflect.Float64:
|
||||
return func(v1, v2 interface{}) bool { return v1.(float64) < v2.(float64) }
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
type sorter struct {
|
||||
vs []reflect.Value
|
||||
less func(v1, v2 interface{}) bool
|
||||
}
|
||||
|
||||
func (s *sorter) Len() int { return len(s.vs) }
|
||||
func (s *sorter) Swap(i, j int) { s.vs[i], s.vs[j] = s.vs[j], s.vs[i] }
|
||||
func (s *sorter) Less(i, j int) bool { return s.less(s.vs[i].Interface(), s.vs[j].Interface()) }
|
105
vendor/cloud.google.com/go/internal/pretty/pretty_test.go
generated
vendored
Normal file
105
vendor/cloud.google.com/go/internal/pretty/pretty_test.go
generated
vendored
Normal file
|
@ -0,0 +1,105 @@
|
|||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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 pretty
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type S struct {
|
||||
X int
|
||||
Y bool
|
||||
z *string
|
||||
}
|
||||
|
||||
func TestSprint(t *testing.T) {
|
||||
Indent = "~"
|
||||
i := 17
|
||||
|
||||
for _, test := range []struct {
|
||||
value interface{}
|
||||
want string
|
||||
}{
|
||||
// primitives and pointer
|
||||
{nil, "nil"},
|
||||
{3, "3"},
|
||||
{9.8, "9.8"},
|
||||
{true, "true"},
|
||||
{"foo", `"foo"`},
|
||||
{&i, "&17"},
|
||||
// array and slice
|
||||
{[3]int{1, 2, 3}, "[3]int{\n~1,\n~2,\n~3,\n}"},
|
||||
{[]int{1, 2, 3}, "[]int{\n~1,\n~2,\n~3,\n}"},
|
||||
{[]int{}, "[]int{}"},
|
||||
{[]string{"foo"}, "[]string{\n~\"foo\",\n}"},
|
||||
// map
|
||||
{map[int]bool{}, "map[int]bool{}"},
|
||||
{map[int]bool{1: true, 2: false, 3: true},
|
||||
"map[int]bool{\n~1: true,\n~3: true,\n}"},
|
||||
// struct
|
||||
{S{}, "pretty.S{\n}"},
|
||||
{S{3, true, ptr("foo")},
|
||||
"pretty.S{\n~X: 3,\n~Y: true,\n~z: &\"foo\",\n}"},
|
||||
// interface
|
||||
{[]interface{}{&i}, "[]interface {}{\n~&17,\n}"},
|
||||
// nesting
|
||||
{[]S{{1, false, ptr("a")}, {2, true, ptr("b")}},
|
||||
`[]pretty.S{
|
||||
~pretty.S{
|
||||
~~X: 1,
|
||||
~~z: &"a",
|
||||
~},
|
||||
~pretty.S{
|
||||
~~X: 2,
|
||||
~~Y: true,
|
||||
~~z: &"b",
|
||||
~},
|
||||
}`},
|
||||
} {
|
||||
got := fmt.Sprintf("%v", Value(test.value))
|
||||
if got != test.want {
|
||||
t.Errorf("%v: got:\n%q\nwant:\n%q", test.value, got, test.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithDefaults(t *testing.T) {
|
||||
Indent = "~"
|
||||
for _, test := range []struct {
|
||||
value interface{}
|
||||
want string
|
||||
}{
|
||||
{map[int]bool{1: true, 2: false, 3: true},
|
||||
"map[int]bool{\n~1: true,\n~2: false,\n~3: true,\n}"},
|
||||
{S{}, "pretty.S{\n~X: 0,\n~Y: false,\n~z: nil,\n}"},
|
||||
} {
|
||||
got := fmt.Sprintf("%+v", Value(test.value))
|
||||
if got != test.want {
|
||||
t.Errorf("%v: got:\n%q\nwant:\n%q", test.value, got, test.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadVerb(t *testing.T) {
|
||||
got := fmt.Sprintf("%d", Value(8))
|
||||
want := "%!d("
|
||||
if !strings.HasPrefix(got, want) {
|
||||
t.Errorf("got %q, want prefix %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func ptr(s string) *string { return &s }
|
55
vendor/cloud.google.com/go/internal/retry.go
generated
vendored
Normal file
55
vendor/cloud.google.com/go/internal/retry.go
generated
vendored
Normal file
|
@ -0,0 +1,55 @@
|
|||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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 internal
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
gax "github.com/googleapis/gax-go"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
// Retry calls the supplied function f repeatedly according to the provided
|
||||
// backoff parameters. It returns when one of the following occurs:
|
||||
// When f's first return value is true, Retry immediately returns with f's second
|
||||
// return value.
|
||||
// When the provided context is done, Retry returns with ctx.Err().
|
||||
func Retry(ctx context.Context, bo gax.Backoff, f func() (stop bool, err error)) error {
|
||||
return retry(ctx, bo, f, gax.Sleep)
|
||||
}
|
||||
|
||||
func retry(ctx context.Context, bo gax.Backoff, f func() (stop bool, err error),
|
||||
sleep func(context.Context, time.Duration) error) error {
|
||||
var lastErr error
|
||||
for {
|
||||
stop, err := f()
|
||||
if stop {
|
||||
return err
|
||||
}
|
||||
// Remember the last "real" error from f.
|
||||
if err != nil && err != context.Canceled && err != context.DeadlineExceeded {
|
||||
lastErr = err
|
||||
}
|
||||
p := bo.Pause()
|
||||
if cerr := sleep(ctx, p); cerr != nil {
|
||||
if lastErr != nil {
|
||||
return fmt.Errorf("%v; last function err: %v", cerr, lastErr)
|
||||
}
|
||||
return cerr
|
||||
}
|
||||
}
|
||||
}
|
64
vendor/cloud.google.com/go/internal/retry_test.go
generated
vendored
Normal file
64
vendor/cloud.google.com/go/internal/retry_test.go
generated
vendored
Normal file
|
@ -0,0 +1,64 @@
|
|||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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 internal
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
|
||||
gax "github.com/googleapis/gax-go"
|
||||
)
|
||||
|
||||
func TestRetry(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
// Without a context deadline, retry will run until the function
|
||||
// says not to retry any more.
|
||||
n := 0
|
||||
endRetry := errors.New("end retry")
|
||||
err := retry(ctx, gax.Backoff{},
|
||||
func() (bool, error) {
|
||||
n++
|
||||
if n < 10 {
|
||||
return false, nil
|
||||
}
|
||||
return true, endRetry
|
||||
},
|
||||
func(context.Context, time.Duration) error { return nil })
|
||||
if got, want := err, endRetry; got != want {
|
||||
t.Errorf("got %v, want %v", err, endRetry)
|
||||
}
|
||||
if n != 10 {
|
||||
t.Errorf("n: got %d, want %d", n, 10)
|
||||
}
|
||||
|
||||
// If the context has a deadline, sleep will return an error
|
||||
// and end the function.
|
||||
n = 0
|
||||
err = retry(ctx, gax.Backoff{},
|
||||
func() (bool, error) { return false, nil },
|
||||
func(context.Context, time.Duration) error {
|
||||
n++
|
||||
if n < 10 {
|
||||
return nil
|
||||
}
|
||||
return context.DeadlineExceeded
|
||||
})
|
||||
if err == nil {
|
||||
t.Error("got nil, want error")
|
||||
}
|
||||
}
|
67
vendor/cloud.google.com/go/internal/testutil/context.go
generated
vendored
Normal file
67
vendor/cloud.google.com/go/internal/testutil/context.go
generated
vendored
Normal file
|
@ -0,0 +1,67 @@
|
|||
// Copyright 2014 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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 testutil contains helper functions for writing tests.
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"golang.org/x/net/context"
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/oauth2/google"
|
||||
)
|
||||
|
||||
const (
|
||||
envProjID = "GCLOUD_TESTS_GOLANG_PROJECT_ID"
|
||||
envPrivateKey = "GCLOUD_TESTS_GOLANG_KEY"
|
||||
)
|
||||
|
||||
// ProjID returns the project ID to use in integration tests, or the empty
|
||||
// string if none is configured.
|
||||
func ProjID() string {
|
||||
projID := os.Getenv(envProjID)
|
||||
if projID == "" {
|
||||
return ""
|
||||
}
|
||||
return projID
|
||||
}
|
||||
|
||||
// TokenSource returns the OAuth2 token source to use in integration tests,
|
||||
// or nil if none is configured. If the environment variable is unset,
|
||||
// TokenSource will try to find 'Application Default Credentials'. Else,
|
||||
// TokenSource will return nil.
|
||||
// TokenSource will log.Fatal if the token source is specified but missing or invalid.
|
||||
func TokenSource(ctx context.Context, scopes ...string) oauth2.TokenSource {
|
||||
key := os.Getenv(envPrivateKey)
|
||||
if key == "" { // Try for application default credentials.
|
||||
ts, err := google.DefaultTokenSource(ctx, scopes...)
|
||||
if err != nil {
|
||||
log.Println("No 'Application Default Credentials' found.")
|
||||
return nil
|
||||
}
|
||||
return ts
|
||||
}
|
||||
jsonKey, err := ioutil.ReadFile(key)
|
||||
if err != nil {
|
||||
log.Fatalf("Cannot read the JSON key file, err: %v", err)
|
||||
}
|
||||
conf, err := google.JWTConfigFromJSON(jsonKey, scopes...)
|
||||
if err != nil {
|
||||
log.Fatalf("google.JWTConfigFromJSON: %v", err)
|
||||
}
|
||||
return conf.TokenSource(ctx)
|
||||
}
|
73
vendor/cloud.google.com/go/internal/testutil/server.go
generated
vendored
Normal file
73
vendor/cloud.google.com/go/internal/testutil/server.go
generated
vendored
Normal file
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
Copyright 2016 Google Inc. All Rights Reserved.
|
||||
|
||||
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 testutil
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
grpc "google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// A Server is an in-process gRPC server, listening on a system-chosen port on
|
||||
// the local loopback interface. Servers are for testing only and are not
|
||||
// intended to be used in production code.
|
||||
//
|
||||
// To create a server, make a new Server, register your handlers, then call
|
||||
// Start:
|
||||
//
|
||||
// srv, err := NewServer()
|
||||
// ...
|
||||
// mypb.RegisterMyServiceServer(srv.Gsrv, &myHandler)
|
||||
// ....
|
||||
// srv.Start()
|
||||
//
|
||||
// Clients should connect to the server with no security:
|
||||
//
|
||||
// conn, err := grpc.Dial(srv.Addr, grpc.WithInsecure())
|
||||
// ...
|
||||
type Server struct {
|
||||
Addr string
|
||||
l net.Listener
|
||||
Gsrv *grpc.Server
|
||||
}
|
||||
|
||||
// NewServer creates a new Server. The Server will be listening for gRPC connections
|
||||
// at the address named by the Addr field, without TLS.
|
||||
func NewServer() (*Server, error) {
|
||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s := &Server{
|
||||
Addr: l.Addr().String(),
|
||||
l: l,
|
||||
Gsrv: grpc.NewServer(),
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// Start causes the server to start accepting incoming connections.
|
||||
// Call Start after registering handlers.
|
||||
func (s *Server) Start() {
|
||||
go s.Gsrv.Serve(s.l)
|
||||
}
|
||||
|
||||
// Close shuts down the server.
|
||||
func (s *Server) Close() {
|
||||
s.Gsrv.Stop()
|
||||
s.l.Close()
|
||||
}
|
35
vendor/cloud.google.com/go/internal/testutil/server_test.go
generated
vendored
Normal file
35
vendor/cloud.google.com/go/internal/testutil/server_test.go
generated
vendored
Normal file
|
@ -0,0 +1,35 @@
|
|||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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 testutil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
grpc "google.golang.org/grpc"
|
||||
)
|
||||
|
||||
func TestNewServer(t *testing.T) {
|
||||
srv, err := NewServer()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
srv.Start()
|
||||
conn, err := grpc.Dial(srv.Addr, grpc.WithInsecure())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
conn.Close()
|
||||
srv.Close()
|
||||
}
|
6
vendor/cloud.google.com/go/internal/version/update_version.sh
generated
vendored
Executable file
6
vendor/cloud.google.com/go/internal/version/update_version.sh
generated
vendored
Executable file
|
@ -0,0 +1,6 @@
|
|||
#!/bin/bash
|
||||
|
||||
today=$(date +%Y%m%d)
|
||||
|
||||
sed -i -r -e 's/const Repo = "([0-9]{8})"/const Repo = "'$today'"/' $GOFILE
|
||||
|
49
vendor/cloud.google.com/go/internal/version/version.go
generated
vendored
Normal file
49
vendor/cloud.google.com/go/internal/version/version.go
generated
vendored
Normal file
|
@ -0,0 +1,49 @@
|
|||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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.
|
||||
|
||||
//go:generate ./update_version.sh
|
||||
|
||||
// Package version contains version information for Google Cloud Client
|
||||
// Libraries for Go, as reported in request headers.
|
||||
package version
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"runtime"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
// Repo is the current version of the client libraries in this
|
||||
// repo. It should be a date in YYYYMMDD format.
|
||||
const Repo = "20170210"
|
||||
|
||||
// Go returns the Go runtime version. The returned string
|
||||
// has no whitespace.
|
||||
func Go() string {
|
||||
return goVersion
|
||||
}
|
||||
|
||||
var goVersion = removeWhitespace(runtime.Version())
|
||||
|
||||
func removeWhitespace(s string) string {
|
||||
var buf bytes.Buffer
|
||||
for _, r := range s {
|
||||
if unicode.IsSpace(r) {
|
||||
buf.WriteByte('_')
|
||||
} else {
|
||||
buf.WriteRune(r)
|
||||
}
|
||||
}
|
||||
return buf.String()
|
||||
}
|
47
vendor/cloud.google.com/go/internal/version/version_test.go
generated
vendored
Normal file
47
vendor/cloud.google.com/go/internal/version/version_test.go
generated
vendored
Normal file
|
@ -0,0 +1,47 @@
|
|||
// Copyright 2016 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// 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 version
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGo(t *testing.T) {
|
||||
got := Go()
|
||||
want := `^\S+$`
|
||||
match, err := regexp.MatchString(want, got)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !match {
|
||||
t.Errorf("got %q, want match of regexp %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveWhitespace(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
in, want string
|
||||
}{
|
||||
{"", ""},
|
||||
{"go1.7", "go1.7"},
|
||||
{" a b c ", "_a_b_c_"},
|
||||
{"a\tb\t c\n", "a_b__c_"},
|
||||
} {
|
||||
if got := removeWhitespace(test.in); got != test.want {
|
||||
t.Errorf("removeWhitespace(%q) = %q, want %q", test.in, got, test.want)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue