Browse Source

client: add dir/ttl fields into node

Xiang Li 10 years ago
parent
commit
666a97271d
2 changed files with 34 additions and 2 deletions
  1. 10 1
      client/keys.go
  2. 24 1
      client/keys_test.go

+ 10 - 1
client/keys.go

@@ -234,6 +234,9 @@ type Node struct {
 	// Key represents the unique location of this Node (e.g. "/foo/bar").
 	Key string `json:"key"`
 
+	// Dir reports whether node describes a directory.
+	Dir bool `json:"dir,omitempty"`
+
 	// Value is the current data stored on this Node. If this Node
 	// is a directory, Value will be empty.
 	Value string `json:"value"`
@@ -248,10 +251,16 @@ type Node struct {
 
 	// ModifiedIndex is the etcd index at-which this Node was last modified.
 	ModifiedIndex uint64 `json:"modifiedIndex"`
+
+	// Expiration is the server side expiration time of the key.
+	Expiration *time.Time `json:"expiration,omitempty"`
+
+	// TTL is the time to live of the key in second.
+	TTL int64 `json:"ttl,omitempty"`
 }
 
 func (n *Node) String() string {
-	return fmt.Sprintf("{Key: %s, CreatedIndex: %d, ModifiedIndex: %d}", n.Key, n.CreatedIndex, n.ModifiedIndex)
+	return fmt.Sprintf("{Key: %s, CreatedIndex: %d, ModifiedIndex: %d, TTL: %d}", n.Key, n.CreatedIndex, n.ModifiedIndex, n.TTL)
 }
 
 type httpKeysAPI struct {

+ 24 - 1
client/keys_test.go

@@ -483,6 +483,9 @@ func assertRequest(got http.Request, wantMethod string, wantURL *url.URL, wantHe
 }
 
 func TestUnmarshalSuccessfulResponse(t *testing.T) {
+	var expiration time.Time
+	expiration.UnmarshalText([]byte("2015-04-07T04:40:23.044979686Z"))
+
 	tests := []struct {
 		hdr     string
 		body    string
@@ -518,7 +521,7 @@ func TestUnmarshalSuccessfulResponse(t *testing.T) {
 		// Node
 		{
 			hdr:  "15",
-			body: `{"action":"get", "node": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10}}`,
+			body: `{"action":"get", "node": {"key": "/foo", "value": "bar", "modifiedIndex": 12, "createdIndex": 10, "ttl": 10, "expiration": "2015-04-07T04:40:23.044979686Z"}}`,
 			wantRes: &Response{
 				Action: "get",
 				Index:  15,
@@ -527,6 +530,26 @@ func TestUnmarshalSuccessfulResponse(t *testing.T) {
 					Value:         "bar",
 					ModifiedIndex: 12,
 					CreatedIndex:  10,
+					TTL:           10,
+					Expiration:    &expiration,
+				},
+				PrevNode: nil,
+			},
+			wantErr: false,
+		},
+
+		// Node Dir
+		{
+			hdr:  "15",
+			body: `{"action":"get", "node": {"key": "/foo", "dir": true, "modifiedIndex": 12, "createdIndex": 10}}`,
+			wantRes: &Response{
+				Action: "get",
+				Index:  15,
+				Node: &Node{
+					Key:           "/foo",
+					Dir:           true,
+					ModifiedIndex: 12,
+					CreatedIndex:  10,
 				},
 				PrevNode: nil,
 			},