From 4e61de3e5754c70643d13c4dfa6a45fa3e9e98cc Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Wed, 12 Apr 2017 15:42:45 -0700 Subject: [PATCH] vendor new go-memdb --- vendor/github.com/hashicorp/go-memdb/index.go | 75 +++++++++++++++++++ vendor/vendor.json | 6 +- 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/vendor/github.com/hashicorp/go-memdb/index.go b/vendor/github.com/hashicorp/go-memdb/index.go index 17aa02699..a40312aa5 100644 --- a/vendor/github.com/hashicorp/go-memdb/index.go +++ b/vendor/github.com/hashicorp/go-memdb/index.go @@ -1,6 +1,7 @@ package memdb import ( + "encoding/binary" "encoding/hex" "fmt" "reflect" @@ -249,6 +250,80 @@ func (s *StringMapFieldIndex) FromArgs(args ...interface{}) ([]byte, error) { return []byte(key), nil } +// UintFieldIndex is used to extract a uint field from an object using +// reflection and builds an index on that field. +type UintFieldIndex struct { + Field string +} + +func (u *UintFieldIndex) FromObject(obj interface{}) (bool, []byte, error) { + v := reflect.ValueOf(obj) + v = reflect.Indirect(v) // Dereference the pointer if any + + fv := v.FieldByName(u.Field) + if !fv.IsValid() { + return false, nil, + fmt.Errorf("field '%s' for %#v is invalid", u.Field, obj) + } + + // Check the type + k := fv.Kind() + size, ok := IsUintType(k) + if !ok { + return false, nil, fmt.Errorf("field %q is of type %v; want a uint", u.Field, k) + } + + // Get the value and encode it + val := fv.Uint() + buf := make([]byte, size) + binary.PutUvarint(buf, val) + + return true, buf, nil +} + +func (u *UintFieldIndex) FromArgs(args ...interface{}) ([]byte, error) { + if len(args) != 1 { + return nil, fmt.Errorf("must provide only a single argument") + } + + v := reflect.ValueOf(args[0]) + if !v.IsValid() { + return nil, fmt.Errorf("%#v is invalid", args[0]) + } + + k := v.Kind() + size, ok := IsUintType(k) + if !ok { + return nil, fmt.Errorf("arg is of type %v; want a uint", k) + } + + val := v.Uint() + buf := make([]byte, size) + binary.PutUvarint(buf, val) + + return buf, nil +} + +// IsUintType returns whether the passed type is a type of uint and the number +// of bytes the type is. To avoid platform specific sizes, the uint type returns +// 8 bytes regardless of if it is smaller. +func IsUintType(k reflect.Kind) (size int, okay bool) { + switch k { + case reflect.Uint: + return 8, true + case reflect.Uint8: + return 1, true + case reflect.Uint16: + return 2, true + case reflect.Uint32: + return 4, true + case reflect.Uint64: + return 8, true + default: + return 0, false + } +} + // UUIDFieldIndex is used to extract a field from an object // using reflection and builds an index on that field by treating // it as a UUID. This is an optimization to using a StringFieldIndex diff --git a/vendor/vendor.json b/vendor/vendor.json index f28d574d2..ec1b17877 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -701,10 +701,10 @@ "revisionTime": "2017-02-14T02:52:36Z" }, { - "checksumSHA1": "pc2EZl8culxKGGIdstQ60l6avjU=", + "checksumSHA1": "KeH4FuTKuv3tqFOr3NpLQtL1jPs=", "path": "github.com/hashicorp/go-memdb", - "revision": "6b158e314030abfb0dbbbc24d0ac71b132259ebf", - "revisionTime": "2017-02-10T23:23:44Z" + "revision": "ed59a4bb9146689d4b00d060b70b9e9648b523af", + "revisionTime": "2017-04-11T17:33:47Z" }, { "path": "github.com/hashicorp/go-msgpack/codec",