Adds a more complete set of numeric type conversion methods to Node.

These now cover all the bit sizes for signed and unsigned numbers.
Methods have been implemented for Node values and attributes.

This addresses issue #8
This commit is contained in:
jimt 2012-12-11 12:36:39 +01:00
parent c085b35fb9
commit 79b9108a49
2 changed files with 177 additions and 25 deletions

View File

@ -7,10 +7,12 @@ as well as perform search queries (no xpath support).
Nodes now simply become collections and don't require you to read them in the Nodes now simply become collections and don't require you to read them in the
order in which the xml.Parser finds them. order in which the xml.Parser finds them.
### Dependencies ### Dependencies
None. None.
### API ### API
The Document currently implements 2 simple search functions which allow you to The Document currently implements 2 simple search functions which allow you to
@ -31,8 +33,14 @@ conversion to types: int, int64, uint, uint64, float32, float64:
*node.S(ns, name string) string *node.S(ns, name string) string
*node.I(ns, name string) int *node.I(ns, name string) int
*node.I8(ns, name string) int8
*node.I16(ns, name string) int16
*node.I32(ns, name string) int32
*node.I64(ns, name string) int64 *node.I64(ns, name string) int64
*node.U(ns, name string) uint *node.U(ns, name string) uint
*node.U8(ns, name string) uint8
*node.U16(ns, name string) uint16
*node.U32(ns, name string) uint32
*node.U64(ns, name string) uint64 *node.U64(ns, name string) uint64
*node.F32(ns, name string) float32 *node.F32(ns, name string) float32
*node.F64(ns, name string) float64 *node.F64(ns, name string) float64
@ -65,8 +73,14 @@ function is invoked on:
*node.As(ns, name string) string *node.As(ns, name string) string
*node.Ai(ns, name string) int *node.Ai(ns, name string) int
*node.Ai8(ns, name string) int8
*node.Ai16(ns, name string) int16
*node.Ai32(ns, name string) int32
*node.Ai64(ns, name string) int64 *node.Ai64(ns, name string) int64
*node.Au(ns, name string) uint *node.Au(ns, name string) uint
*node.Au8(ns, name string) uint8
*node.Au16(ns, name string) uint16
*node.Au32(ns, name string) uint32
*node.Au64(ns, name string) uint64 *node.Au64(ns, name string) uint64
*node.Af32(ns, name string) float32 *node.Af32(ns, name string) float32
*node.Af64(ns, name string) float64 *node.Af64(ns, name string) float64
@ -82,6 +96,9 @@ there is or if there is one at all. Node and attribute names as well, may
be supplied as the wildcard `*`. This allows us to fetch all child nodes for be supplied as the wildcard `*`. This allows us to fetch all child nodes for
a given namespace, regardless of their names. a given namespace, regardless of their names.
All numeric type-conversion methods assume base-10 numbers data.
### License ### License
This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication This work is subject to the CC0 1.0 Universal (CC0 1.0) Public Domain Dedication

185
node.go
View File

@ -56,7 +56,8 @@ func (this *Node) Unmarshal(obj interface{}) error {
// Get node value as string // Get node value as string
func (this *Node) S(namespace, name string) string { func (this *Node) S(namespace, name string) string {
if node := rec_SelectNode(this, namespace, name); node != nil { node := rec_SelectNode(this, namespace, name)
if node != nil {
return node.Value return node.Value
} }
return "" return ""
@ -64,16 +65,48 @@ func (this *Node) S(namespace, name string) string {
// Get node value as int // Get node value as int
func (this *Node) I(namespace, name string) int { func (this *Node) I(namespace, name string) int {
if node := rec_SelectNode(this, namespace, name); node != nil && node.Value != "" { node := rec_SelectNode(this, namespace, name)
n, _ := strconv.Atoi(node.Value) if node != nil && node.Value != "" {
return n n, _ := strconv.ParseInt(node.Value, 10, 0)
return int(n)
}
return 0
}
// Get node value as int8
func (this *Node) I8(namespace, name string) int8 {
node := rec_SelectNode(this, namespace, name)
if node != nil && node.Value != "" {
n, _ := strconv.ParseInt(node.Value, 10, 8)
return int8(n)
}
return 0
}
// Get node value as int16
func (this *Node) I16(namespace, name string) int16 {
node := rec_SelectNode(this, namespace, name)
if node != nil && node.Value != "" {
n, _ := strconv.ParseInt(node.Value, 10, 16)
return int16(n)
}
return 0
}
// Get node value as int32
func (this *Node) I32(namespace, name string) int32 {
node := rec_SelectNode(this, namespace, name)
if node != nil && node.Value != "" {
n, _ := strconv.ParseInt(node.Value, 10, 32)
return int32(n)
} }
return 0 return 0
} }
// Get node value as int64 // Get node value as int64
func (this *Node) I64(namespace, name string) int64 { func (this *Node) I64(namespace, name string) int64 {
if node := rec_SelectNode(this, namespace, name); node != nil && node.Value != "" { node := rec_SelectNode(this, namespace, name)
if node != nil && node.Value != "" {
n, _ := strconv.ParseInt(node.Value, 10, 64) n, _ := strconv.ParseInt(node.Value, 10, 64)
return n return n
} }
@ -82,16 +115,48 @@ func (this *Node) I64(namespace, name string) int64 {
// Get node value as uint // Get node value as uint
func (this *Node) U(namespace, name string) uint { func (this *Node) U(namespace, name string) uint {
if node := rec_SelectNode(this, namespace, name); node != nil && node.Value != "" { node := rec_SelectNode(this, namespace, name)
if node != nil && node.Value != "" {
n, _ := strconv.ParseUint(node.Value, 10, 0) n, _ := strconv.ParseUint(node.Value, 10, 0)
return uint(n) return uint(n)
} }
return 0 return 0
} }
// Get node value as uint8
func (this *Node) U8(namespace, name string) uint8 {
node := rec_SelectNode(this, namespace, name)
if node != nil && node.Value != "" {
n, _ := strconv.ParseUint(node.Value, 10, 8)
return uint8(n)
}
return 0
}
// Get node value as uint16
func (this *Node) U16(namespace, name string) uint16 {
node := rec_SelectNode(this, namespace, name)
if node != nil && node.Value != "" {
n, _ := strconv.ParseUint(node.Value, 10, 16)
return uint16(n)
}
return 0
}
// Get node value as uint32
func (this *Node) U32(namespace, name string) uint32 {
node := rec_SelectNode(this, namespace, name)
if node != nil && node.Value != "" {
n, _ := strconv.ParseUint(node.Value, 10, 32)
return uint32(n)
}
return 0
}
// Get node value as uint64 // Get node value as uint64
func (this *Node) U64(namespace, name string) uint64 { func (this *Node) U64(namespace, name string) uint64 {
if node := rec_SelectNode(this, namespace, name); node != nil && node.Value != "" { node := rec_SelectNode(this, namespace, name)
if node != nil && node.Value != "" {
n, _ := strconv.ParseUint(node.Value, 10, 64) n, _ := strconv.ParseUint(node.Value, 10, 64)
return n return n
} }
@ -100,7 +165,8 @@ func (this *Node) U64(namespace, name string) uint64 {
// Get node value as float32 // Get node value as float32
func (this *Node) F32(namespace, name string) float32 { func (this *Node) F32(namespace, name string) float32 {
if node := rec_SelectNode(this, namespace, name); node != nil && node.Value != "" { node := rec_SelectNode(this, namespace, name)
if node != nil && node.Value != "" {
n, _ := strconv.ParseFloat(node.Value, 32) n, _ := strconv.ParseFloat(node.Value, 32)
return float32(n) return float32(n)
} }
@ -109,7 +175,8 @@ func (this *Node) F32(namespace, name string) float32 {
// Get node value as float64 // Get node value as float64
func (this *Node) F64(namespace, name string) float64 { func (this *Node) F64(namespace, name string) float64 {
if node := rec_SelectNode(this, namespace, name); node != nil && node.Value != "" { node := rec_SelectNode(this, namespace, name)
if node != nil && node.Value != "" {
n, _ := strconv.ParseFloat(node.Value, 64) n, _ := strconv.ParseFloat(node.Value, 64)
return n return n
} }
@ -118,7 +185,8 @@ func (this *Node) F64(namespace, name string) float64 {
// Get node value as bool // Get node value as bool
func (this *Node) B(namespace, name string) bool { func (this *Node) B(namespace, name string) bool {
if node := rec_SelectNode(this, namespace, name); node != nil && node.Value != "" { node := rec_SelectNode(this, namespace, name)
if node != nil && node.Value != "" {
n, _ := strconv.ParseBool(node.Value) n, _ := strconv.ParseBool(node.Value)
return n return n
} }
@ -137,8 +205,49 @@ func (this *Node) As(namespace, name string) string {
// Get attribute value as int // Get attribute value as int
func (this *Node) Ai(namespace, name string) int { func (this *Node) Ai(namespace, name string) int {
if s := this.As(namespace, name); s != "" { s := this.As(namespace, name)
n, _ := strconv.Atoi(s) if s != "" {
n, _ := strconv.ParseInt(s, 10, 0)
return int(n)
}
return 0
}
// Get attribute value as int8
func (this *Node) Ai8(namespace, name string) int8 {
s := this.As(namespace, name)
if s != "" {
n, _ := strconv.ParseInt(s, 10, 8)
return int8(n)
}
return 0
}
// Get attribute value as int16
func (this *Node) Ai16(namespace, name string) int16 {
s := this.As(namespace, name)
if s != "" {
n, _ := strconv.ParseInt(s, 10, 16)
return int16(n)
}
return 0
}
// Get attribute value as int32
func (this *Node) Ai32(namespace, name string) int32 {
s := this.As(namespace, name)
if s != "" {
n, _ := strconv.ParseInt(s, 10, 32)
return int32(n)
}
return 0
}
// Get attribute value as int64
func (this *Node) Ai64(namespace, name string) int64 {
s := this.As(namespace, name)
if s != "" {
n, _ := strconv.ParseInt(s, 10, 64)
return n return n
} }
return 0 return 0
@ -146,26 +255,49 @@ func (this *Node) Ai(namespace, name string) int {
// Get attribute value as uint // Get attribute value as uint
func (this *Node) Au(namespace, name string) uint { func (this *Node) Au(namespace, name string) uint {
if s := this.As(namespace, name); s != "" { s := this.As(namespace, name)
if s != "" {
n, _ := strconv.ParseUint(s, 10, 0) n, _ := strconv.ParseUint(s, 10, 0)
return uint(n) return uint(n)
} }
return 0 return 0
} }
// Get attribute value as uint64 // Get attribute value as uint8
func (this *Node) Au64(namespace, name string) uint64 { func (this *Node) Au8(namespace, name string) uint8 {
if s := this.As(namespace, name); s != "" { s := this.As(namespace, name)
n, _ := strconv.ParseUint(s, 10, 64) if s != "" {
return n n, _ := strconv.ParseUint(s, 10, 8)
return uint8(n)
} }
return 0 return 0
} }
// Get attribute value as int64 // Get attribute value as uint16
func (this *Node) Ai64(namespace, name string) int64 { func (this *Node) Au16(namespace, name string) uint16 {
if s := this.As(namespace, name); s != "" { s := this.As(namespace, name)
n, _ := strconv.ParseInt(s, 10, 64) if s != "" {
n, _ := strconv.ParseUint(s, 10, 16)
return uint16(n)
}
return 0
}
// Get attribute value as uint32
func (this *Node) Au32(namespace, name string) uint32 {
s := this.As(namespace, name)
if s != "" {
n, _ := strconv.ParseUint(s, 10, 32)
return uint32(n)
}
return 0
}
// Get attribute value as uint64
func (this *Node) Au64(namespace, name string) uint64 {
s := this.As(namespace, name)
if s != "" {
n, _ := strconv.ParseUint(s, 10, 64)
return n return n
} }
return 0 return 0
@ -173,7 +305,8 @@ func (this *Node) Ai64(namespace, name string) int64 {
// Get attribute value as float32 // Get attribute value as float32
func (this *Node) Af32(namespace, name string) float32 { func (this *Node) Af32(namespace, name string) float32 {
if s := this.As(namespace, name); s != "" { s := this.As(namespace, name)
if s != "" {
n, _ := strconv.ParseFloat(s, 32) n, _ := strconv.ParseFloat(s, 32)
return float32(n) return float32(n)
} }
@ -182,7 +315,8 @@ func (this *Node) Af32(namespace, name string) float32 {
// Get attribute value as float64 // Get attribute value as float64
func (this *Node) Af64(namespace, name string) float64 { func (this *Node) Af64(namespace, name string) float64 {
if s := this.As(namespace, name); s != "" { s := this.As(namespace, name)
if s != "" {
n, _ := strconv.ParseFloat(s, 64) n, _ := strconv.ParseFloat(s, 64)
return n return n
} }
@ -191,7 +325,8 @@ func (this *Node) Af64(namespace, name string) float64 {
// Get attribute value as bool // Get attribute value as bool
func (this *Node) Ab(namespace, name string) bool { func (this *Node) Ab(namespace, name string) bool {
if s := this.As(namespace, name); s != "" { s := this.As(namespace, name)
if s != "" {
n, _ := strconv.ParseBool(s) n, _ := strconv.ParseBool(s)
return n return n
} }