# HG changeset patch # User Chris Jones # Date 1324749954 25200 # Node ID 6121aa2f21b163cbd485ed9964d3e82488e3f4e5 # Parent 4dabfef08c8c9138a3a49a5fb7b44955111d619c Made JID implement flag.Value. diff -r 4dabfef08c8c -r 6121aa2f21b1 structs.go --- a/structs.go Sat Dec 24 09:55:26 2011 -0700 +++ b/structs.go Sat Dec 24 11:05:54 2011 -0700 @@ -8,9 +8,11 @@ import ( "bytes" + "flag" "fmt" "io" "os" + "regexp" "xml" ) @@ -31,6 +33,7 @@ Resource *string } var _ fmt.Stringer = &JID{} +var _ flag.Value = &JID{} // XMPP's XML element type Stream struct { @@ -70,6 +73,26 @@ return result } +func (jid *JID) Set(val string) bool { + r := regexp.MustCompile("^(([^@/]+)@)?([^@/]+)(/([^@/]+))?$") + parts := r.FindStringSubmatch(val) + if parts == nil { + return false + } + if parts[2] == "" { + jid.Node = nil + } else { + jid.Node = &parts[2] + } + jid.Domain = parts[3] + if parts[5] == "" { + jid.Resource = nil + } else { + jid.Resource = &parts[5] + } + return true +} + func (s *Stream) MarshalXML() ([]byte, os.Error) { buf := bytes.NewBuffer(nil) buf.WriteString("` @@ -38,13 +77,3 @@ `" xml:lang="pt">things happen` assertMarshal(t, exp, e) } - -func assertMarshal(t *testing.T, expected string, marshal interface{}) { - buf := bytes.NewBuffer(nil) - xml.Marshal(buf, marshal) - observed := string(buf.Bytes()) - if expected != observed { - t.Errorf("Expected:\n%s\nObserved:\n%s\n", expected, - observed) - } -}