Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

client: add Nodes type to faciliate sorting #3482

Merged
merged 2 commits into from
Sep 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 66 additions & 48 deletions client/keys.generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ import (
)

const (
codecSelferC_UTF85311 = 1
codecSelferC_RAW5311 = 0
codecSelverValueTypeArray5311 = 10
codecSelverValueTypeMap5311 = 9
codecSelferC_UTF81819 = 1
codecSelferC_RAW1819 = 0
codecSelverValueTypeArray1819 = 10
codecSelverValueTypeMap1819 = 9
)

var (
codecSelferBitsize5311 = uint8(reflect.TypeOf(uint(0)).Bits())
codecSelferOnlyMapOrArrayEncodeToStructErr5311 = errors.New(`only encoded map or array can be decoded into a struct`)
codecSelferBitsize1819 = uint8(reflect.TypeOf(uint(0)).Bits())
codecSelferOnlyMapOrArrayEncodeToStructErr1819 = errors.New(`only encoded map or array can be decoded into a struct`)
)

type codecSelfer5311 struct{}
type codecSelfer1819 struct{}

func init() {
if codec1978.GenVersion != 2 {
Expand All @@ -42,7 +42,7 @@ func init() {
}

func (x *Response) CodecEncodeSelf(e *codec1978.Encoder) {
var h codecSelfer5311
var h codecSelfer1819
z, r := codec1978.GenHelperEncoder(e)
_, _, _ = h, z, r
if x == nil {
Expand All @@ -66,14 +66,14 @@ func (x *Response) CodecEncodeSelf(e *codec1978.Encoder) {
r.EncodeMapStart(yynn1)
}
if yyr1 || yy2arr1 {
r.EncodeString(codecSelferC_UTF85311, string(x.Action))
r.EncodeString(codecSelferC_UTF81819, string(x.Action))
} else {
yyfirst1 = true
r.EncodeString(codecSelferC_UTF85311, string("action"))
r.EncodeString(codecSelferC_UTF81819, string("action"))
if yysep1 {
r.EncodeMapKVSeparator()
}
r.EncodeString(codecSelferC_UTF85311, string(x.Action))
r.EncodeString(codecSelferC_UTF81819, string(x.Action))
}
if yyr1 || yy2arr1 {
if yysep1 {
Expand All @@ -90,7 +90,7 @@ func (x *Response) CodecEncodeSelf(e *codec1978.Encoder) {
} else {
yyfirst1 = true
}
r.EncodeString(codecSelferC_UTF85311, string("node"))
r.EncodeString(codecSelferC_UTF81819, string("node"))
if yysep1 {
r.EncodeMapKVSeparator()
}
Expand All @@ -115,7 +115,7 @@ func (x *Response) CodecEncodeSelf(e *codec1978.Encoder) {
} else {
yyfirst1 = true
}
r.EncodeString(codecSelferC_UTF85311, string("prevNode"))
r.EncodeString(codecSelferC_UTF81819, string("prevNode"))
if yysep1 {
r.EncodeMapKVSeparator()
}
Expand All @@ -136,30 +136,30 @@ func (x *Response) CodecEncodeSelf(e *codec1978.Encoder) {
}

func (x *Response) CodecDecodeSelf(d *codec1978.Decoder) {
var h codecSelfer5311
var h codecSelfer1819
z, r := codec1978.GenHelperDecoder(d)
_, _, _ = h, z, r
if r.IsContainerType(codecSelverValueTypeMap5311) {
if r.IsContainerType(codecSelverValueTypeMap1819) {
yyl5 := r.ReadMapStart()
if yyl5 == 0 {
r.ReadMapEnd()
} else {
x.codecDecodeSelfFromMap(yyl5, d)
}
} else if r.IsContainerType(codecSelverValueTypeArray5311) {
} else if r.IsContainerType(codecSelverValueTypeArray1819) {
yyl5 := r.ReadArrayStart()
if yyl5 == 0 {
r.ReadArrayEnd()
} else {
x.codecDecodeSelfFromArray(yyl5, d)
}
} else {
panic(codecSelferOnlyMapOrArrayEncodeToStructErr5311)
panic(codecSelferOnlyMapOrArrayEncodeToStructErr1819)
}
}

func (x *Response) codecDecodeSelfFromMap(l int, d *codec1978.Decoder) {
var h codecSelfer5311
var h codecSelfer1819
z, r := codec1978.GenHelperDecoder(d)
_, _, _ = h, z, r
var yys6Slc = z.DecScratchBuffer() // default slice to decode into
Expand Down Expand Up @@ -222,7 +222,7 @@ func (x *Response) codecDecodeSelfFromMap(l int, d *codec1978.Decoder) {
}

func (x *Response) codecDecodeSelfFromArray(l int, d *codec1978.Decoder) {
var h codecSelfer5311
var h codecSelfer1819
z, r := codec1978.GenHelperDecoder(d)
_, _, _ = h, z, r
var yyj10 int
Expand Down Expand Up @@ -304,7 +304,7 @@ func (x *Response) codecDecodeSelfFromArray(l int, d *codec1978.Decoder) {
}

func (x *Node) CodecEncodeSelf(e *codec1978.Encoder) {
var h codecSelfer5311
var h codecSelfer1819
z, r := codec1978.GenHelperEncoder(e)
_, _, _ = h, z, r
if x == nil {
Expand All @@ -331,14 +331,14 @@ func (x *Node) CodecEncodeSelf(e *codec1978.Encoder) {
r.EncodeMapStart(yynn14)
}
if yyr14 || yy2arr14 {
r.EncodeString(codecSelferC_UTF85311, string(x.Key))
r.EncodeString(codecSelferC_UTF81819, string(x.Key))
} else {
yyfirst14 = true
r.EncodeString(codecSelferC_UTF85311, string("key"))
r.EncodeString(codecSelferC_UTF81819, string("key"))
if yysep14 {
r.EncodeMapKVSeparator()
}
r.EncodeString(codecSelferC_UTF85311, string(x.Key))
r.EncodeString(codecSelferC_UTF81819, string(x.Key))
}
if yyr14 || yy2arr14 {
if yysep14 {
Expand All @@ -356,7 +356,7 @@ func (x *Node) CodecEncodeSelf(e *codec1978.Encoder) {
} else {
yyfirst14 = true
}
r.EncodeString(codecSelferC_UTF85311, string("dir"))
r.EncodeString(codecSelferC_UTF81819, string("dir"))
if yysep14 {
r.EncodeMapKVSeparator()
}
Expand All @@ -367,18 +367,18 @@ func (x *Node) CodecEncodeSelf(e *codec1978.Encoder) {
if yysep14 {
r.EncodeArrayEntrySeparator()
}
r.EncodeString(codecSelferC_UTF85311, string(x.Value))
r.EncodeString(codecSelferC_UTF81819, string(x.Value))
} else {
if yyfirst14 {
r.EncodeMapEntrySeparator()
} else {
yyfirst14 = true
}
r.EncodeString(codecSelferC_UTF85311, string("value"))
r.EncodeString(codecSelferC_UTF81819, string("value"))
if yysep14 {
r.EncodeMapKVSeparator()
}
r.EncodeString(codecSelferC_UTF85311, string(x.Value))
r.EncodeString(codecSelferC_UTF81819, string(x.Value))
}
if yyr14 || yy2arr14 {
if yysep14 {
Expand All @@ -387,22 +387,22 @@ func (x *Node) CodecEncodeSelf(e *codec1978.Encoder) {
if x.Nodes == nil {
r.EncodeNil()
} else {
h.encSlicePtrtoNode(([]*Node)(x.Nodes), e)
x.Nodes.CodecEncodeSelf(e)
}
} else {
if yyfirst14 {
r.EncodeMapEntrySeparator()
} else {
yyfirst14 = true
}
r.EncodeString(codecSelferC_UTF85311, string("nodes"))
r.EncodeString(codecSelferC_UTF81819, string("nodes"))
if yysep14 {
r.EncodeMapKVSeparator()
}
if x.Nodes == nil {
r.EncodeNil()
} else {
h.encSlicePtrtoNode(([]*Node)(x.Nodes), e)
x.Nodes.CodecEncodeSelf(e)
}
}
if yyr14 || yy2arr14 {
Expand All @@ -416,7 +416,7 @@ func (x *Node) CodecEncodeSelf(e *codec1978.Encoder) {
} else {
yyfirst14 = true
}
r.EncodeString(codecSelferC_UTF85311, string("createdIndex"))
r.EncodeString(codecSelferC_UTF81819, string("createdIndex"))
if yysep14 {
r.EncodeMapKVSeparator()
}
Expand All @@ -433,7 +433,7 @@ func (x *Node) CodecEncodeSelf(e *codec1978.Encoder) {
} else {
yyfirst14 = true
}
r.EncodeString(codecSelferC_UTF85311, string("modifiedIndex"))
r.EncodeString(codecSelferC_UTF81819, string("modifiedIndex"))
if yysep14 {
r.EncodeMapKVSeparator()
}
Expand All @@ -459,7 +459,7 @@ func (x *Node) CodecEncodeSelf(e *codec1978.Encoder) {
} else {
yyfirst14 = true
}
r.EncodeString(codecSelferC_UTF85311, string("expiration"))
r.EncodeString(codecSelferC_UTF81819, string("expiration"))
if yysep14 {
r.EncodeMapKVSeparator()
}
Expand All @@ -486,7 +486,7 @@ func (x *Node) CodecEncodeSelf(e *codec1978.Encoder) {
} else {
yyfirst14 = true
}
r.EncodeString(codecSelferC_UTF85311, string("ttl"))
r.EncodeString(codecSelferC_UTF81819, string("ttl"))
if yysep14 {
r.EncodeMapKVSeparator()
}
Expand All @@ -504,30 +504,30 @@ func (x *Node) CodecEncodeSelf(e *codec1978.Encoder) {
}

func (x *Node) CodecDecodeSelf(d *codec1978.Decoder) {
var h codecSelfer5311
var h codecSelfer1819
z, r := codec1978.GenHelperDecoder(d)
_, _, _ = h, z, r
if r.IsContainerType(codecSelverValueTypeMap5311) {
if r.IsContainerType(codecSelverValueTypeMap1819) {
yyl23 := r.ReadMapStart()
if yyl23 == 0 {
r.ReadMapEnd()
} else {
x.codecDecodeSelfFromMap(yyl23, d)
}
} else if r.IsContainerType(codecSelverValueTypeArray5311) {
} else if r.IsContainerType(codecSelverValueTypeArray1819) {
yyl23 := r.ReadArrayStart()
if yyl23 == 0 {
r.ReadArrayEnd()
} else {
x.codecDecodeSelfFromArray(yyl23, d)
}
} else {
panic(codecSelferOnlyMapOrArrayEncodeToStructErr5311)
panic(codecSelferOnlyMapOrArrayEncodeToStructErr1819)
}
}

func (x *Node) codecDecodeSelfFromMap(l int, d *codec1978.Decoder) {
var h codecSelfer5311
var h codecSelfer1819
z, r := codec1978.GenHelperDecoder(d)
_, _, _ = h, z, r
var yys24Slc = z.DecScratchBuffer() // default slice to decode into
Expand Down Expand Up @@ -575,7 +575,7 @@ func (x *Node) codecDecodeSelfFromMap(l int, d *codec1978.Decoder) {
x.Nodes = nil
} else {
yyv28 := &x.Nodes
h.decSlicePtrtoNode((*[]*Node)(yyv28), d)
yyv28.CodecDecodeSelf(d)
}
case "createdIndex":
if r.TryDecodeAsNil() {
Expand Down Expand Up @@ -616,7 +616,7 @@ func (x *Node) codecDecodeSelfFromMap(l int, d *codec1978.Decoder) {
}

func (x *Node) codecDecodeSelfFromArray(l int, d *codec1978.Decoder) {
var h codecSelfer5311
var h codecSelfer1819
z, r := codec1978.GenHelperDecoder(d)
_, _, _ = h, z, r
var yyj33 int
Expand Down Expand Up @@ -684,7 +684,7 @@ func (x *Node) codecDecodeSelfFromArray(l int, d *codec1978.Decoder) {
x.Nodes = nil
} else {
yyv37 := &x.Nodes
h.decSlicePtrtoNode((*[]*Node)(yyv37), d)
yyv37.CodecDecodeSelf(d)
}
yyj33++
if yyhl33 {
Expand Down Expand Up @@ -773,8 +773,26 @@ func (x *Node) codecDecodeSelfFromArray(l int, d *codec1978.Decoder) {
r.ReadArrayEnd()
}

func (x codecSelfer5311) encSlicePtrtoNode(v []*Node, e *codec1978.Encoder) {
var h codecSelfer5311
func (x Nodes) CodecEncodeSelf(e *codec1978.Encoder) {
var h codecSelfer1819
z, r := codec1978.GenHelperEncoder(e)
_, _, _ = h, z, r
if x == nil {
r.EncodeNil()
} else {
h.encNodes((Nodes)(x), e)
}
}

func (x *Nodes) CodecDecodeSelf(d *codec1978.Decoder) {
var h codecSelfer1819
z, r := codec1978.GenHelperDecoder(d)
_, _, _ = h, z, r
h.decNodes((*Nodes)(x), d)
}

func (x codecSelfer1819) encNodes(v Nodes, e *codec1978.Encoder) {
var h codecSelfer1819
z, r := codec1978.GenHelperEncoder(e)
_, _, _ = h, z, r
r.EncodeArrayStart(len(v))
Expand Down Expand Up @@ -802,8 +820,8 @@ func (x codecSelfer5311) encSlicePtrtoNode(v []*Node, e *codec1978.Encoder) {
}
}

func (x codecSelfer5311) decSlicePtrtoNode(v *[]*Node, d *codec1978.Decoder) {
var h codecSelfer5311
func (x codecSelfer1819) decNodes(v *Nodes, d *codec1978.Decoder) {
var h codecSelfer1819
z, r := codec1978.GenHelperDecoder(d)
_, _, _ = h, z, r

Expand All @@ -815,9 +833,9 @@ func (x codecSelfer5311) decSlicePtrtoNode(v *[]*Node, d *codec1978.Decoder) {

if yyv43 == nil {
if yyl43 <= 0 {
yyv43 = make([]*Node, 0)
yyv43 = make(Nodes, 0)
} else {
yyv43 = make([]*Node, yyl43)
yyv43 = make(Nodes, yyl43)
}
yyc43 = true
}
Expand Down
11 changes: 9 additions & 2 deletions client/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package client

//go:generate codecgen -r "Node|Response" -o keys.generated.go keys.go
//go:generate codecgen -r "Node|Response|Nodes" -o keys.generated.go keys.go

import (
"encoding/json"
Expand Down Expand Up @@ -279,7 +279,7 @@ type Node struct {
// Nodes holds the children of this Node, only if this Node is a directory.
// This slice of will be arbitrarily deep (children, grandchildren, great-
// grandchildren, etc.) if a recursive Get or Watch request were made.
Nodes []*Node `json:"nodes"`
Nodes Nodes `json:"nodes"`

// CreatedIndex is the etcd index at-which this Node was created.
CreatedIndex uint64 `json:"createdIndex"`
Expand All @@ -303,6 +303,13 @@ func (n *Node) TTLDuration() time.Duration {
return time.Duration(n.TTL) * time.Second
}

type Nodes []*Node

// interfaces for sorting
func (ns Nodes) Len() int { return len(ns) }
func (ns Nodes) Less(i, j int) bool { return ns[i].Key < ns[j].Key }
func (ns Nodes) Swap(i, j int) { ns[i], ns[j] = ns[j], ns[i] }

type httpKeysAPI struct {
client httpClient
prefix string
Expand Down