structs.go
changeset 25 7437d6eed227
parent 24 fff79efe06f6
child 26 4d0a369079ce
equal deleted inserted replaced
24:fff79efe06f6 25:7437d6eed227
    19 
    19 
    20 // JID represents an entity that can communicate with other
    20 // JID represents an entity that can communicate with other
    21 // entities. It looks like node@domain/resource. Node and resource are
    21 // entities. It looks like node@domain/resource. Node and resource are
    22 // sometimes optional.
    22 // sometimes optional.
    23 type JID struct {
    23 type JID struct {
    24 	// BUG(cjyar) Make this not a pointer.
    24 	Node string
    25 	Node *string
       
    26 	Domain string
    25 	Domain string
    27 	Resource string
    26 	Resource string
    28 }
    27 }
    29 var _ fmt.Stringer = &JID{}
    28 var _ fmt.Stringer = &JID{}
    30 var _ flag.Value = &JID{}
    29 var _ flag.Value = &JID{}
   154 }
   153 }
   155 var _ fmt.Stringer = &Generic{}
   154 var _ fmt.Stringer = &Generic{}
   156 
   155 
   157 func (jid *JID) String() string {
   156 func (jid *JID) String() string {
   158 	result := jid.Domain
   157 	result := jid.Domain
   159 	if jid.Node != nil {
   158 	if jid.Node != "" {
   160 		result = *jid.Node + "@" + result
   159 		result = jid.Node + "@" + result
   161 	}
   160 	}
   162 	if jid.Resource != "" {
   161 	if jid.Resource != "" {
   163 		result = result + "/" + jid.Resource
   162 		result = result + "/" + jid.Resource
   164 	}
   163 	}
   165 	return result
   164 	return result
   169 	r := regexp.MustCompile("^(([^@/]+)@)?([^@/]+)(/([^@/]+))?$")
   168 	r := regexp.MustCompile("^(([^@/]+)@)?([^@/]+)(/([^@/]+))?$")
   170 	parts := r.FindStringSubmatch(val)
   169 	parts := r.FindStringSubmatch(val)
   171 	if parts == nil {
   170 	if parts == nil {
   172 		return false
   171 		return false
   173 	}
   172 	}
   174 	if parts[2] == "" {
   173 	jid.Node = parts[2]
   175 		jid.Node = nil
       
   176 	} else {
       
   177 		jid.Node = &parts[2]
       
   178 	}
       
   179 	jid.Domain = parts[3]
   174 	jid.Domain = parts[3]
   180 	jid.Resource = parts[5]
   175 	jid.Resource = parts[5]
   181 	return true
   176 	return true
   182 }
   177 }
   183 
   178