/
argument.go
138 lines (119 loc) · 3.32 KB
/
argument.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright 2015 Satoshi Konno. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package upnp
import (
"encoding/xml"
"fmt"
"strconv"
)
// A Argument represents a UPnP argument.
type Argument struct {
XMLName xml.Name `xml:"argument"`
Name string `xml:"name"`
Direction string `xml:"direction"`
RelatedStateVariable string `xml:"relatedStateVariable"`
Value string `xml:"-"`
ParentAction *Action `xml:"-"`
}
// A ArgumentList represents a UPnP argument list.
type ArgumentList struct {
XMLName xml.Name `xml:"argumentList"`
Arguments []Argument `xml:"argument"`
}
// NewArgument returns a new Argument.
func NewArgument() *Argument {
arg := &Argument{}
return arg
}
// SetString sets a string value into the specified argument.
func (arg *Argument) SetString(value string) error {
arg.Value = value
return nil
}
// GetString returns a string value into the specified argument.
func (arg *Argument) GetString() (string, error) {
return arg.Value, nil
}
// SetInt sets a integer value into the specified argument.
func (arg *Argument) SetInt(value int) error {
return arg.SetString(strconv.Itoa(value))
}
// GetInt return a integer value into the specified argument.
func (arg *Argument) GetInt() (int, error) {
value, err := arg.GetString()
if err != nil {
return 0, err
}
return strconv.Atoi(value)
}
// SetFloat sets a integer value into the specified argument.
func (arg *Argument) SetFloat(value float64) error {
return arg.SetString(fmt.Sprintf("%f", value))
}
// GetFloat return a integer value into the specified argument.
func (arg *Argument) GetFloat() (float64, error) {
value, err := arg.GetString()
if err != nil {
return 0, err
}
f, err := strconv.ParseFloat(value, 64)
if err != nil {
return 0, err
}
return f, nil
}
// SetBool sets a boolean value into the specified argument.
func (arg *Argument) SetBool(value bool) error {
ivalue := 0
if value {
ivalue = 1
}
return arg.SetInt(ivalue)
}
// GetBool return a boolean value into the specified argument.
func (arg *Argument) GetBool() (bool, error) {
value, err := arg.GetString()
if err != nil {
return false, err
}
b, err := strconv.ParseBool(value)
if err != nil {
return false, err
}
return b, nil
}
// isDirection returns true when the argument direction equals the specified value, otherwise false.
func (arg *Argument) isDirection(value string) bool {
return (arg.Direction == value)
}
// IsInDirection returns true when the argument direction is in, otherwise false.
func (arg *Argument) IsInDirection() bool {
return arg.isDirection(In)
}
// IsOutDirection returns true when the argument direction is out, otherwise false.
func (arg *Argument) IsOutDirection() bool {
return arg.isDirection(Out)
}
// SetDirection sets a directional string of the specified diractional integer.
func (arg *Argument) SetDirection(dir int) bool {
switch dir {
case InDirection:
arg.Direction = In
return true
case OutDirection:
arg.Direction = Out
return true
}
return false
}
// GetDirection returns a directional integer of the specified argument.
func (arg *Argument) GetDirection() int {
if arg.IsInDirection() {
return InDirection
}
if arg.IsOutDirection() {
return OutDirection
}
return UnknownDirection
}