libs/common: refactor libs/common 01 (#4230)

* libs/common: Refactor libs/common 01

Signed-off-by: Marko Baricevic <marbar3778@yahoo.com>

* regenerate proto files, move intslice to where its used

* update kv.KVPair(s) to kv.Pair(s)

* add changelog entry

* make intInSlice private
This commit is contained in:
Marko
2019-12-10 12:40:01 +01:00
committed by GitHub
parent 6b1fef7f4d
commit dfebac86f7
26 changed files with 428 additions and 451 deletions

View File

@@ -1,11 +0,0 @@
package common
// IntInSlice returns true if a is found in the list.
func IntInSlice(a int, list []int) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}

View File

@@ -1,14 +0,0 @@
package common
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestIntInSlice(t *testing.T) {
assert.True(t, IntInSlice(1, []int{1, 2, 3}))
assert.False(t, IntInSlice(4, []int{1, 2, 3}))
assert.True(t, IntInSlice(0, []int{0}))
assert.False(t, IntInSlice(0, []int{}))
}

View File

@@ -1,4 +1,4 @@
package common
package kv
import (
"bytes"
@@ -11,17 +11,17 @@ import (
/*
Defined in types.proto
type KVPair struct {
type Pair struct {
Key []byte
Value []byte
}
*/
type KVPairs []KVPair
type Pairs []Pair
// Sorting
func (kvs KVPairs) Len() int { return len(kvs) }
func (kvs KVPairs) Less(i, j int) bool {
func (kvs Pairs) Len() int { return len(kvs) }
func (kvs Pairs) Less(i, j int) bool {
switch bytes.Compare(kvs[i].Key, kvs[j].Key) {
case -1:
return true
@@ -33,8 +33,8 @@ func (kvs KVPairs) Less(i, j int) bool {
panic("invalid comparison result")
}
}
func (kvs KVPairs) Swap(i, j int) { kvs[i], kvs[j] = kvs[j], kvs[i] }
func (kvs KVPairs) Sort() { sort.Sort(kvs) }
func (kvs Pairs) Swap(i, j int) { kvs[i], kvs[j] = kvs[j], kvs[i] }
func (kvs Pairs) Sort() { sort.Sort(kvs) }
//----------------------------------------
// KI64Pair

View File

@@ -1,6 +1,6 @@
// nolint: dupl
// dupl is reading this as the same file as crypto/merkle/result.go
package common
package kv
import (
"bytes"
@@ -20,12 +20,12 @@ var (
jsonpbUnmarshaller = jsonpb.Unmarshaler{}
)
func (r *KVPair) MarshalJSON() ([]byte, error) {
func (r *Pair) MarshalJSON() ([]byte, error) {
s, err := jsonpbMarshaller.MarshalToString(r)
return []byte(s), err
}
func (r *KVPair) UnmarshalJSON(b []byte) error {
func (r *Pair) UnmarshalJSON(b []byte) error {
reader := bytes.NewBuffer(b)
return jsonpbUnmarshaller.Unmarshal(reader, r)
}
@@ -50,5 +50,5 @@ type jsonRoundTripper interface {
json.Unmarshaler
}
var _ jsonRoundTripper = (*KVPair)(nil)
var _ jsonRoundTripper = (*Pair)(nil)
var _ jsonRoundTripper = (*KI64Pair)(nil)

View File

@@ -1,7 +1,7 @@
// Code generated by protoc-gen-gogo. DO NOT EDIT.
// source: libs/common/types.proto
// source: libs/kv/types.proto
package common
package kv
import (
bytes "bytes"
@@ -27,7 +27,7 @@ var _ = math.Inf
const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package
// Define these here for compatibility but use tmlibs/common.KVPair.
type KVPair struct {
type Pair struct {
Key []byte `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
Value []byte `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
@@ -35,18 +35,18 @@ type KVPair struct {
XXX_sizecache int32 `json:"-"`
}
func (m *KVPair) Reset() { *m = KVPair{} }
func (m *KVPair) String() string { return proto.CompactTextString(m) }
func (*KVPair) ProtoMessage() {}
func (*KVPair) Descriptor() ([]byte, []int) {
return fileDescriptor_28b36ea5054b507d, []int{0}
func (m *Pair) Reset() { *m = Pair{} }
func (m *Pair) String() string { return proto.CompactTextString(m) }
func (*Pair) ProtoMessage() {}
func (*Pair) Descriptor() ([]byte, []int) {
return fileDescriptor_31432671d164f444, []int{0}
}
func (m *KVPair) XXX_Unmarshal(b []byte) error {
func (m *Pair) XXX_Unmarshal(b []byte) error {
return m.Unmarshal(b)
}
func (m *KVPair) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
func (m *Pair) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
if deterministic {
return xxx_messageInfo_KVPair.Marshal(b, m, deterministic)
return xxx_messageInfo_Pair.Marshal(b, m, deterministic)
} else {
b = b[:cap(b)]
n, err := m.MarshalToSizedBuffer(b)
@@ -56,26 +56,26 @@ func (m *KVPair) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return b[:n], nil
}
}
func (m *KVPair) XXX_Merge(src proto.Message) {
xxx_messageInfo_KVPair.Merge(m, src)
func (m *Pair) XXX_Merge(src proto.Message) {
xxx_messageInfo_Pair.Merge(m, src)
}
func (m *KVPair) XXX_Size() int {
func (m *Pair) XXX_Size() int {
return m.Size()
}
func (m *KVPair) XXX_DiscardUnknown() {
xxx_messageInfo_KVPair.DiscardUnknown(m)
func (m *Pair) XXX_DiscardUnknown() {
xxx_messageInfo_Pair.DiscardUnknown(m)
}
var xxx_messageInfo_KVPair proto.InternalMessageInfo
var xxx_messageInfo_Pair proto.InternalMessageInfo
func (m *KVPair) GetKey() []byte {
func (m *Pair) GetKey() []byte {
if m != nil {
return m.Key
}
return nil
}
func (m *KVPair) GetValue() []byte {
func (m *Pair) GetValue() []byte {
if m != nil {
return m.Value
}
@@ -95,7 +95,7 @@ func (m *KI64Pair) Reset() { *m = KI64Pair{} }
func (m *KI64Pair) String() string { return proto.CompactTextString(m) }
func (*KI64Pair) ProtoMessage() {}
func (*KI64Pair) Descriptor() ([]byte, []int) {
return fileDescriptor_28b36ea5054b507d, []int{1}
return fileDescriptor_31432671d164f444, []int{1}
}
func (m *KI64Pair) XXX_Unmarshal(b []byte) error {
return m.Unmarshal(b)
@@ -139,40 +139,40 @@ func (m *KI64Pair) GetValue() int64 {
}
func init() {
proto.RegisterType((*KVPair)(nil), "tendermint.libs.common.KVPair")
golang_proto.RegisterType((*KVPair)(nil), "tendermint.libs.common.KVPair")
proto.RegisterType((*KI64Pair)(nil), "tendermint.libs.common.KI64Pair")
golang_proto.RegisterType((*KI64Pair)(nil), "tendermint.libs.common.KI64Pair")
proto.RegisterType((*Pair)(nil), "tendermint.libs.kv.Pair")
golang_proto.RegisterType((*Pair)(nil), "tendermint.libs.kv.Pair")
proto.RegisterType((*KI64Pair)(nil), "tendermint.libs.kv.KI64Pair")
golang_proto.RegisterType((*KI64Pair)(nil), "tendermint.libs.kv.KI64Pair")
}
func init() { proto.RegisterFile("libs/common/types.proto", fileDescriptor_28b36ea5054b507d) }
func init() { golang_proto.RegisterFile("libs/common/types.proto", fileDescriptor_28b36ea5054b507d) }
func init() { proto.RegisterFile("libs/kv/types.proto", fileDescriptor_31432671d164f444) }
func init() { golang_proto.RegisterFile("libs/kv/types.proto", fileDescriptor_31432671d164f444) }
var fileDescriptor_28b36ea5054b507d = []byte{
// 197 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0xcf, 0xc9, 0x4c, 0x2a,
0xd6, 0x4f, 0xce, 0xcf, 0xcd, 0xcd, 0xcf, 0xd3, 0x2f, 0xa9, 0x2c, 0x48, 0x2d, 0xd6, 0x2b, 0x28,
0xca, 0x2f, 0xc9, 0x17, 0x12, 0x2b, 0x49, 0xcd, 0x4b, 0x49, 0x2d, 0xca, 0xcd, 0xcc, 0x2b, 0xd1,
0x03, 0xa9, 0xd1, 0x83, 0xa8, 0x91, 0xd2, 0x4d, 0xcf, 0x2c, 0xc9, 0x28, 0x4d, 0x02, 0x71, 0xf5,
0xd3, 0xf3, 0xd3, 0xf3, 0xf5, 0xc1, 0xca, 0x93, 0x4a, 0xd3, 0xc0, 0x3c, 0x30, 0x07, 0xcc, 0x82,
0x18, 0xa3, 0x64, 0xc0, 0xc5, 0xe6, 0x1d, 0x16, 0x90, 0x98, 0x59, 0x24, 0x24, 0xc0, 0xc5, 0x9c,
0x9d, 0x5a, 0x29, 0xc1, 0xa8, 0xc0, 0xa8, 0xc1, 0x13, 0x04, 0x62, 0x0a, 0x89, 0x70, 0xb1, 0x96,
0x25, 0xe6, 0x94, 0xa6, 0x4a, 0x30, 0x81, 0xc5, 0x20, 0x1c, 0x25, 0x23, 0x2e, 0x0e, 0x6f, 0x4f,
0x33, 0x13, 0x62, 0xf4, 0x30, 0x43, 0xf5, 0x38, 0x79, 0xfd, 0x78, 0x28, 0xc7, 0xb8, 0xe2, 0x91,
0x1c, 0xe3, 0x8e, 0x47, 0x72, 0x8c, 0x27, 0x1e, 0xc9, 0x31, 0x5e, 0x78, 0x24, 0xc7, 0xf8, 0xe0,
0x91, 0x1c, 0xe3, 0x81, 0xc7, 0x72, 0x8c, 0x51, 0x3a, 0x48, 0x4e, 0x46, 0xf8, 0x0a, 0x99, 0x89,
0x14, 0x08, 0x49, 0x6c, 0x60, 0x87, 0x1b, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0xd4, 0xc3, 0x96,
0x94, 0x1a, 0x01, 0x00, 0x00,
var fileDescriptor_31432671d164f444 = []byte{
// 193 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0xce, 0xc9, 0x4c, 0x2a,
0xd6, 0xcf, 0x2e, 0xd3, 0x2f, 0xa9, 0x2c, 0x48, 0x2d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17,
0x12, 0x2a, 0x49, 0xcd, 0x4b, 0x49, 0x2d, 0xca, 0xcd, 0xcc, 0x2b, 0xd1, 0x03, 0xc9, 0xeb, 0x65,
0x97, 0x49, 0xe9, 0xa6, 0x67, 0x96, 0x64, 0x94, 0x26, 0xe9, 0x25, 0xe7, 0xe7, 0xea, 0xa7, 0xe7,
0xa7, 0xe7, 0xeb, 0x83, 0x95, 0x26, 0x95, 0xa6, 0x81, 0x79, 0x60, 0x0e, 0x98, 0x05, 0x31, 0x42,
0x49, 0x8f, 0x8b, 0x25, 0x20, 0x31, 0xb3, 0x48, 0x48, 0x80, 0x8b, 0x39, 0x3b, 0xb5, 0x52, 0x82,
0x51, 0x81, 0x51, 0x83, 0x27, 0x08, 0xc4, 0x14, 0x12, 0xe1, 0x62, 0x2d, 0x4b, 0xcc, 0x29, 0x4d,
0x95, 0x60, 0x02, 0x8b, 0x41, 0x38, 0x4a, 0x46, 0x5c, 0x1c, 0xde, 0x9e, 0x66, 0x26, 0xc4, 0xe8,
0x61, 0x86, 0xea, 0x71, 0x72, 0xfb, 0xf1, 0x50, 0x8e, 0x71, 0xc5, 0x23, 0x39, 0xc6, 0x1d, 0x8f,
0xe4, 0x18, 0x4f, 0x3c, 0x92, 0x63, 0xbc, 0xf0, 0x48, 0x8e, 0xf1, 0xc1, 0x23, 0x39, 0xc6, 0x03,
0x8f, 0xe5, 0x18, 0xa3, 0x34, 0x90, 0x1c, 0x8c, 0xf0, 0x0f, 0x32, 0x13, 0xea, 0xf5, 0x24, 0x36,
0xb0, 0x93, 0x8d, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0x01, 0x3f, 0x5a, 0xca, 0x0c, 0x01, 0x00,
0x00,
}
func (this *KVPair) Equal(that interface{}) bool {
func (this *Pair) Equal(that interface{}) bool {
if that == nil {
return this == nil
}
that1, ok := that.(*KVPair)
that1, ok := that.(*Pair)
if !ok {
that2, ok := that.(KVPair)
that2, ok := that.(Pair)
if ok {
that1 = &that2
} else {
@@ -225,7 +225,7 @@ func (this *KI64Pair) Equal(that interface{}) bool {
}
return true
}
func (m *KVPair) Marshal() (dAtA []byte, err error) {
func (m *Pair) Marshal() (dAtA []byte, err error) {
size := m.Size()
dAtA = make([]byte, size)
n, err := m.MarshalToSizedBuffer(dAtA[:size])
@@ -235,12 +235,12 @@ func (m *KVPair) Marshal() (dAtA []byte, err error) {
return dAtA[:n], nil
}
func (m *KVPair) MarshalTo(dAtA []byte) (int, error) {
func (m *Pair) MarshalTo(dAtA []byte) (int, error) {
size := m.Size()
return m.MarshalToSizedBuffer(dAtA[:size])
}
func (m *KVPair) MarshalToSizedBuffer(dAtA []byte) (int, error) {
func (m *Pair) MarshalToSizedBuffer(dAtA []byte) (int, error) {
i := len(dAtA)
_ = i
var l int
@@ -316,8 +316,8 @@ func encodeVarintTypes(dAtA []byte, offset int, v uint64) int {
dAtA[offset] = uint8(v)
return base
}
func NewPopulatedKVPair(r randyTypes, easy bool) *KVPair {
this := &KVPair{}
func NewPopulatedPair(r randyTypes, easy bool) *Pair {
this := &Pair{}
v1 := r.Intn(100)
this.Key = make([]byte, v1)
for i := 0; i < v1; i++ {
@@ -423,7 +423,7 @@ func encodeVarintPopulateTypes(dAtA []byte, v uint64) []byte {
dAtA = append(dAtA, uint8(v))
return dAtA
}
func (m *KVPair) Size() (n int) {
func (m *Pair) Size() (n int) {
if m == nil {
return 0
}
@@ -468,7 +468,7 @@ func sovTypes(x uint64) (n int) {
func sozTypes(x uint64) (n int) {
return sovTypes(uint64((x << 1) ^ uint64((int64(x) >> 63))))
}
func (m *KVPair) Unmarshal(dAtA []byte) error {
func (m *Pair) Unmarshal(dAtA []byte) error {
l := len(dAtA)
iNdEx := 0
for iNdEx < l {
@@ -491,10 +491,10 @@ func (m *KVPair) Unmarshal(dAtA []byte) error {
fieldNum := int32(wire >> 3)
wireType := int(wire & 0x7)
if wireType == 4 {
return fmt.Errorf("proto: KVPair: wiretype end group for non-group")
return fmt.Errorf("proto: Pair: wiretype end group for non-group")
}
if fieldNum <= 0 {
return fmt.Errorf("proto: KVPair: illegal tag %d (wire type %d)", fieldNum, wire)
return fmt.Errorf("proto: Pair: illegal tag %d (wire type %d)", fieldNum, wire)
}
switch fieldNum {
case 1:
@@ -700,6 +700,7 @@ func (m *KI64Pair) Unmarshal(dAtA []byte) error {
func skipTypes(dAtA []byte) (n int, err error) {
l := len(dAtA)
iNdEx := 0
depth := 0
for iNdEx < l {
var wire uint64
for shift := uint(0); ; shift += 7 {
@@ -731,10 +732,8 @@ func skipTypes(dAtA []byte) (n int, err error) {
break
}
}
return iNdEx, nil
case 1:
iNdEx += 8
return iNdEx, nil
case 2:
var length int
for shift := uint(0); ; shift += 7 {
@@ -755,55 +754,30 @@ func skipTypes(dAtA []byte) (n int, err error) {
return 0, ErrInvalidLengthTypes
}
iNdEx += length
if iNdEx < 0 {
return 0, ErrInvalidLengthTypes
}
return iNdEx, nil
case 3:
for {
var innerWire uint64
var start int = iNdEx
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return 0, ErrIntOverflowTypes
}
if iNdEx >= l {
return 0, io.ErrUnexpectedEOF
}
b := dAtA[iNdEx]
iNdEx++
innerWire |= (uint64(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
innerWireType := int(innerWire & 0x7)
if innerWireType == 4 {
break
}
next, err := skipTypes(dAtA[start:])
if err != nil {
return 0, err
}
iNdEx = start + next
if iNdEx < 0 {
return 0, ErrInvalidLengthTypes
}
}
return iNdEx, nil
depth++
case 4:
return iNdEx, nil
if depth == 0 {
return 0, ErrUnexpectedEndOfGroupTypes
}
depth--
case 5:
iNdEx += 4
return iNdEx, nil
default:
return 0, fmt.Errorf("proto: illegal wireType %d", wireType)
}
if iNdEx < 0 {
return 0, ErrInvalidLengthTypes
}
if depth == 0 {
return iNdEx, nil
}
}
panic("unreachable")
return 0, io.ErrUnexpectedEOF
}
var (
ErrInvalidLengthTypes = fmt.Errorf("proto: negative length found during unmarshaling")
ErrIntOverflowTypes = fmt.Errorf("proto: integer overflow")
ErrInvalidLengthTypes = fmt.Errorf("proto: negative length found during unmarshaling")
ErrIntOverflowTypes = fmt.Errorf("proto: integer overflow")
ErrUnexpectedEndOfGroupTypes = fmt.Errorf("proto: unexpected end of group")
)

View File

@@ -1,6 +1,6 @@
syntax = "proto3";
package tendermint.libs.common;
option go_package = "github.com/tendermint/tendermint/libs/common";
package tendermint.libs.kv;
option go_package = "github.com/tendermint/tendermint/libs/kv";
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
@@ -17,7 +17,7 @@ option (gogoproto.testgen_all) = true;
// Abstract types
// Define these here for compatibility but use tmlibs/common.KVPair.
message KVPair {
message Pair {
bytes key = 1;
bytes value = 2;
}

View File

@@ -1,7 +1,7 @@
// Code generated by protoc-gen-gogo. DO NOT EDIT.
// source: libs/common/types.proto
// source: libs/kv/types.proto
package common
package kv
import (
fmt "fmt"
@@ -22,15 +22,15 @@ var _ = golang_proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
func TestKVPairProto(t *testing.T) {
func TestPairProto(t *testing.T) {
seed := time.Now().UnixNano()
popr := math_rand.New(math_rand.NewSource(seed))
p := NewPopulatedKVPair(popr, false)
p := NewPopulatedPair(popr, false)
dAtA, err := github_com_gogo_protobuf_proto.Marshal(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &KVPair{}
msg := &Pair{}
if err := github_com_gogo_protobuf_proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
@@ -53,10 +53,10 @@ func TestKVPairProto(t *testing.T) {
}
}
func TestKVPairMarshalTo(t *testing.T) {
func TestPairMarshalTo(t *testing.T) {
seed := time.Now().UnixNano()
popr := math_rand.New(math_rand.NewSource(seed))
p := NewPopulatedKVPair(popr, false)
p := NewPopulatedPair(popr, false)
size := p.Size()
dAtA := make([]byte, size)
for i := range dAtA {
@@ -66,7 +66,7 @@ func TestKVPairMarshalTo(t *testing.T) {
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &KVPair{}
msg := &Pair{}
if err := github_com_gogo_protobuf_proto.Unmarshal(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
@@ -134,16 +134,16 @@ func TestKI64PairMarshalTo(t *testing.T) {
}
}
func TestKVPairJSON(t *testing.T) {
func TestPairJSON(t *testing.T) {
seed := time.Now().UnixNano()
popr := math_rand.New(math_rand.NewSource(seed))
p := NewPopulatedKVPair(popr, true)
p := NewPopulatedPair(popr, true)
marshaler := github_com_gogo_protobuf_jsonpb.Marshaler{}
jsondata, err := marshaler.MarshalToString(p)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
msg := &KVPair{}
msg := &Pair{}
err = github_com_gogo_protobuf_jsonpb.UnmarshalString(jsondata, msg)
if err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
@@ -170,12 +170,12 @@ func TestKI64PairJSON(t *testing.T) {
t.Fatalf("seed = %d, %#v !Json Equal %#v", seed, msg, p)
}
}
func TestKVPairProtoText(t *testing.T) {
func TestPairProtoText(t *testing.T) {
seed := time.Now().UnixNano()
popr := math_rand.New(math_rand.NewSource(seed))
p := NewPopulatedKVPair(popr, true)
p := NewPopulatedPair(popr, true)
dAtA := github_com_gogo_protobuf_proto.MarshalTextString(p)
msg := &KVPair{}
msg := &Pair{}
if err := github_com_gogo_protobuf_proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
@@ -184,12 +184,12 @@ func TestKVPairProtoText(t *testing.T) {
}
}
func TestKVPairProtoCompactText(t *testing.T) {
func TestPairProtoCompactText(t *testing.T) {
seed := time.Now().UnixNano()
popr := math_rand.New(math_rand.NewSource(seed))
p := NewPopulatedKVPair(popr, true)
p := NewPopulatedPair(popr, true)
dAtA := github_com_gogo_protobuf_proto.CompactTextString(p)
msg := &KVPair{}
msg := &Pair{}
if err := github_com_gogo_protobuf_proto.UnmarshalText(dAtA, msg); err != nil {
t.Fatalf("seed = %d, err = %v", seed, err)
}
@@ -226,10 +226,10 @@ func TestKI64PairProtoCompactText(t *testing.T) {
}
}
func TestKVPairSize(t *testing.T) {
func TestPairSize(t *testing.T) {
seed := time.Now().UnixNano()
popr := math_rand.New(math_rand.NewSource(seed))
p := NewPopulatedKVPair(popr, true)
p := NewPopulatedPair(popr, true)
size2 := github_com_gogo_protobuf_proto.Size(p)
dAtA, err := github_com_gogo_protobuf_proto.Marshal(p)
if err != nil {