[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [goptlib/master] Args interface.



commit aab03a3774f2ca1b9b038e557fe8c660a6a0ae5c
Author: David Fifield <david@xxxxxxxxxxxxxxx>
Date:   Sat Dec 7 01:40:12 2013 -0800

    Args interface.
---
 args.go      |   25 +++++++++++++++++++++++++
 args_test.go |   57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 82 insertions(+)

diff --git a/args.go b/args.go
new file mode 100644
index 0000000..6c8fb86
--- /dev/null
+++ b/args.go
@@ -0,0 +1,25 @@
+package pt
+
+// Keyâ??value mappings for the representation of client and server options.
+
+// Args maps a string key to a list of values. It is similar to url.Values.
+type Args map[string][]string
+
+// Get the first value associated with the given key. If there are any value
+// associated with the key, the ok return value is true; otherwise it is false.
+// If you need access to multiple values, use the map directly.
+func (args Args) Get(key string) (value string, ok bool) {
+	if args == nil {
+		return "", false
+	}
+	vals, ok := args[key]
+	if !ok || len(vals) == 0 {
+		return "", false
+	}
+	return vals[0], true
+}
+
+// Append value to the list of values for key.
+func (args Args) Add(key, value string) {
+	args[key] = append(args[key], value)
+}
diff --git a/args_test.go b/args_test.go
new file mode 100644
index 0000000..c89cef3
--- /dev/null
+++ b/args_test.go
@@ -0,0 +1,57 @@
+package pt
+
+import (
+	"testing"
+)
+
+func TestArgsGet(t *testing.T) {
+	args := Args{
+		"a": []string{},
+		"b": []string{"value"},
+		"c": []string{"v1", "v2", "v3"},
+	}
+
+	var v string
+	var ok bool
+	v, ok = args.Get("a")
+	if ok {
+		t.Errorf("Unexpected Get success for %q", "a")
+	}
+	v, ok = args.Get("b")
+	if !ok {
+		t.Errorf("Unexpected Get failure for %q", "b")
+	}
+	if v != "value" {
+		t.Errorf("Get(%q) â?? %q (expected %q)", "b", v, "value")
+	}
+	v, ok = args.Get("c")
+	if !ok {
+		t.Errorf("Unexpected Get failure for %q", "c")
+	}
+	if v != "v1" {
+		t.Errorf("Get(%q) â?? %q (expected %q)", "c", v, "v1")
+	}
+	v, ok = args.Get("d")
+	if ok {
+		t.Errorf("Unexpected Get success for %q", "d")
+	}
+}
+
+func TestArgsAdd(t *testing.T) {
+	args := make(Args)
+	if !argsEqual(args, Args{}) {
+		t.Error()
+	}
+	args.Add("k1", "v1")
+	if !argsEqual(args, Args{"k1": []string{"v1"}}) {
+		t.Error()
+	}
+	args.Add("k2", "v2")
+	if !argsEqual(args, Args{"k1": []string{"v1"}, "k2": []string{"v2"}}) {
+		t.Error()
+	}
+	args.Add("k1", "v3")
+	if !argsEqual(args, Args{"k1": []string{"v1", "v3"}, "k2": []string{"v2"}}) {
+		t.Error()
+	}
+}



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits