sum7
/
yaja
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
yaja/xmpp/utils.go

59 lines
1.3 KiB
Go

package xmpp
import (
"crypto/rand"
"encoding/binary"
"encoding/xml"
"fmt"
"reflect"
)
func XMLStartElementToString(element *xml.StartElement) string {
if element == nil {
return "<nil>"
}
debug := fmt.Sprintf("<%s xmlns=\"%s\"", element.Name.Local, element.Name.Space)
for _, attr := range element.Attr {
debug = fmt.Sprintf("%s %s=\"%s\"", debug, attr.Name.Local, attr.Value)
}
debug += ">"
return debug
}
func XMLChildrenString(o interface{}) (result string) {
val := reflect.ValueOf(o)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
if val.Kind() == reflect.Struct {
first := true
for i := 0; i < val.NumField(); i++ {
valueField := val.Field(i)
if xmlElement, ok := valueField.Interface().(*xml.Name); ok && xmlElement != nil {
if first {
first = false
result += xmlElement.Local
} else {
result += ", " + xmlElement.Local
}
}
}
}
return
}
// Cookie is used to give a unique identifier to each request.
type Cookie uint64
func CreateCookie() Cookie {
var buf [8]byte
if _, err := rand.Reader.Read(buf[:]); err != nil {
panic("Failed to read random bytes: " + err.Error())
}
return Cookie(binary.LittleEndian.Uint64(buf[:]))
}
func CreateCookieString() string {
return fmt.Sprintf("%x", CreateCookie())
}