summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2016-04-19 04:20:15 (GMT)
committerChristian Pointner <equinox@helsinki.at>2016-04-19 04:20:15 (GMT)
commitf1a97d8e034bc6d38fc386388e733f90fe480ed7 (patch)
treec6aefa10b0278dd05f17f3cf58bb5d11a3c588fe /src
parent3b24e2e452178475284aa0a8240eb8b76974a153 (diff)
create marshaller/unmarshaller for text,json,toml for all special types
Diffstat (limited to 'src')
-rw-r--r--src/rhctl/audio_switch.go4
-rw-r--r--src/rhctl/audio_switch_command.go217
-rw-r--r--src/rhctl/serial_port.go27
3 files changed, 232 insertions, 16 deletions
diff --git a/src/rhctl/audio_switch.go b/src/rhctl/audio_switch.go
index f3cb5df..41db086 100644
--- a/src/rhctl/audio_switch.go
+++ b/src/rhctl/audio_switch.go
@@ -113,7 +113,7 @@ func (sw *AudioSwitch) updateStateAudio(data string) {
return
}
var out SwitchOutputNum
- if err := out.FromString(data[3:4]); err != nil {
+ if err := out.fromString(data[3:4]); err != nil {
rhl.Printf("Audioswitch: invalid audio status update (%s)", err)
return
}
@@ -144,7 +144,7 @@ func (sw *AudioSwitch) updateStateGPI(data string) {
if data[4] != 'A' {
// 'SuP,ii,x'
var in SwitchGPINum
- if err := in.FromString(data[4:6]); err != nil {
+ if err := in.fromString(data[4:6]); err != nil {
rhl.Printf("Audioswitch: invalid gpi status update: %v", err)
return
}
diff --git a/src/rhctl/audio_switch_command.go b/src/rhctl/audio_switch_command.go
index 84b7052..53da812 100644
--- a/src/rhctl/audio_switch_command.go
+++ b/src/rhctl/audio_switch_command.go
@@ -36,13 +36,32 @@ const (
SwitchOCNumMax SwitchOCNum = 8
)
+//
+// Unit ID
+//
+
type SwitchUnitID uint
func (u SwitchUnitID) String() string {
return strconv.FormatUint(uint64(u), 10)
}
-func (u *SwitchUnitID) FromString(str string) error {
+func (u SwitchUnitID) MarshalText() (data []byte, err error) {
+ data = []byte(u.String())
+ return
+}
+
+func (u SwitchUnitID) MarshalJSON() (data []byte, err error) {
+ data = []byte(u.String())
+ return
+}
+
+func (u SwitchUnitID) MarshalTOML() (data []byte, err error) {
+ data = []byte(u.String())
+ return
+}
+
+func (u *SwitchUnitID) fromString(str string) error {
vuint, err := strconv.ParseUint(str, 10, 32)
if err != nil {
return err
@@ -54,17 +73,44 @@ func (u *SwitchUnitID) FromString(str string) error {
return nil
}
+func (u *SwitchUnitID) UnmarshalText(data []byte) error {
+ return u.fromString(string(data))
+}
+
+func (u *SwitchUnitID) UnmarshalJSON(data []byte) error {
+ return u.fromString(string(data))
+}
+
func (u *SwitchUnitID) UnmarshalTOML(data []byte) error {
- return u.FromString(string(data))
+ return u.fromString(string(data))
}
+//
+// Audio Input
+//
+
type SwitchInputNum uint
func (i SwitchInputNum) String() string {
return strconv.FormatUint(uint64(i), 10)
}
-func (i *SwitchInputNum) FromString(str string) error {
+func (i SwitchInputNum) MarshalText() (data []byte, err error) {
+ data = []byte(i.String())
+ return
+}
+
+func (i SwitchInputNum) MarshalJSON() (data []byte, err error) {
+ data = []byte(i.String())
+ return
+}
+
+func (i SwitchInputNum) MarshalTOML() (data []byte, err error) {
+ data = []byte(i.String())
+ return
+}
+
+func (i *SwitchInputNum) fromString(str string) error {
vuint, err := strconv.ParseUint(str, 10, 32)
if err != nil {
return err
@@ -76,17 +122,44 @@ func (i *SwitchInputNum) FromString(str string) error {
return nil
}
+func (i *SwitchInputNum) UnmarshalText(data []byte) error {
+ return i.fromString(string(data))
+}
+
+func (i *SwitchInputNum) UnmarshalJSON(data []byte) error {
+ return i.fromString(string(data))
+}
+
func (i *SwitchInputNum) UnmarshalTOML(data []byte) error {
- return i.FromString(string(data))
+ return i.fromString(string(data))
}
+//
+// Audio Output
+//
+
type SwitchOutputNum uint
func (o SwitchOutputNum) String() string {
return strconv.FormatUint(uint64(o), 10)
}
-func (o *SwitchOutputNum) FromString(str string) error {
+func (o SwitchOutputNum) MarshalText() (data []byte, err error) {
+ data = []byte(o.String())
+ return
+}
+
+func (o SwitchOutputNum) MarshalJSON() (data []byte, err error) {
+ data = []byte(o.String())
+ return
+}
+
+func (o SwitchOutputNum) MarshalTOML() (data []byte, err error) {
+ data = []byte(o.String())
+ return
+}
+
+func (o *SwitchOutputNum) fromString(str string) error {
vuint, err := strconv.ParseUint(str, 10, 32)
if err != nil {
return err
@@ -98,13 +171,44 @@ func (o *SwitchOutputNum) FromString(str string) error {
return nil
}
+func (o *SwitchOutputNum) UnmarshalText(data []byte) error {
+ return o.fromString(string(data))
+}
+
+func (o *SwitchOutputNum) UnmarshalJSON(data []byte) error {
+ return o.fromString(string(data))
+}
+
+func (o *SwitchOutputNum) UnmarshalTOML(data []byte) error {
+ return o.fromString(string(data))
+}
+
+//
+// General-Purpose Input
+//
+
type SwitchGPINum uint
func (g SwitchGPINum) String() string {
return strconv.FormatUint(uint64(g), 10)
}
-func (g *SwitchGPINum) FromString(str string) error {
+func (g SwitchGPINum) MarshalText() (data []byte, err error) {
+ data = []byte(g.String())
+ return
+}
+
+func (g SwitchGPINum) MarshalJSON() (data []byte, err error) {
+ data = []byte(g.String())
+ return
+}
+
+func (g SwitchGPINum) MarshalTOML() (data []byte, err error) {
+ data = []byte(g.String())
+ return
+}
+
+func (g *SwitchGPINum) fromString(str string) error {
vuint, err := strconv.ParseUint(str, 10, 32)
if err != nil {
return err
@@ -116,13 +220,44 @@ func (g *SwitchGPINum) FromString(str string) error {
return nil
}
+func (g *SwitchGPINum) UnmarshalText(data []byte) error {
+ return g.fromString(string(data))
+}
+
+func (g *SwitchGPINum) UnmarshalJSON(data []byte) error {
+ return g.fromString(string(data))
+}
+
+func (g *SwitchGPINum) UnmarshalTOML(data []byte) error {
+ return g.fromString(string(data))
+}
+
+//
+// Relay Output
+//
+
type SwitchRelayNum uint
func (r SwitchRelayNum) String() string {
return strconv.FormatUint(uint64(r), 10)
}
-func (r *SwitchRelayNum) FromString(str string) error {
+func (r SwitchRelayNum) MarshalText() (data []byte, err error) {
+ data = []byte(r.String())
+ return
+}
+
+func (r SwitchRelayNum) MarshalJSON() (data []byte, err error) {
+ data = []byte(r.String())
+ return
+}
+
+func (r SwitchRelayNum) MarshalTOML() (data []byte, err error) {
+ data = []byte(r.String())
+ return
+}
+
+func (r *SwitchRelayNum) fromString(str string) error {
vuint, err := strconv.ParseUint(str, 10, 32)
if err != nil {
return err
@@ -134,13 +269,44 @@ func (r *SwitchRelayNum) FromString(str string) error {
return nil
}
+func (r *SwitchRelayNum) UnmarshalText(data []byte) error {
+ return r.fromString(string(data))
+}
+
+func (r *SwitchRelayNum) UnmarshalJSON(data []byte) error {
+ return r.fromString(string(data))
+}
+
+func (r *SwitchRelayNum) UnmarshalTOML(data []byte) error {
+ return r.fromString(string(data))
+}
+
+//
+// Open-Collector Output
+//
+
type SwitchOCNum uint
func (o SwitchOCNum) String() string {
return strconv.FormatUint(uint64(o), 10)
}
-func (o *SwitchOCNum) FromString(str string) error {
+func (o SwitchOCNum) MarshalText() (data []byte, err error) {
+ data = []byte(o.String())
+ return
+}
+
+func (o SwitchOCNum) MarshalJSON() (data []byte, err error) {
+ data = []byte(o.String())
+ return
+}
+
+func (o SwitchOCNum) MarshalTOML() (data []byte, err error) {
+ data = []byte(o.String())
+ return
+}
+
+func (o *SwitchOCNum) fromString(str string) error {
vuint, err := strconv.ParseUint(str, 10, 32)
if err != nil {
return err
@@ -152,6 +318,29 @@ func (o *SwitchOCNum) FromString(str string) error {
return nil
}
+func (o *SwitchOCNum) UnmarshalText(data []byte) error {
+ return o.fromString(string(data))
+}
+
+func (o *SwitchOCNum) UnmarshalJSON(data []byte) error {
+ return o.fromString(string(data))
+}
+
+func (o *SwitchOCNum) UnmarshalTOML(data []byte) error {
+ return o.fromString(string(data))
+}
+
+//
+// Command Mnemonic
+// '*' and Caps are untouched, smaller letters will be replaced:
+// u ... Unit ID
+// ii ... Audio Input
+// o ... Audio Output
+// gg ... General-Purpose Input
+// r ... Relay Output
+// c ... Open-Collector Output
+//
+
type SwitchCmdString string
const (
@@ -223,6 +412,10 @@ func (c SwitchCmdString) Generate(args ...interface{}) (string, error) {
return s, nil
}
+//
+// Command
+//
+
type SwitchCommand struct {
Cmd SwitchCmdString
Args []interface{}
@@ -260,7 +453,7 @@ func SwitchCommandParseAudio(args []string) (cmdstr SwitchCmdString, cmdargs []i
onum := SwitchOutputNum(0)
if strings.ToLower(args[0]) != "all" {
- if err = onum.FromString(args[0]); err != nil {
+ if err = onum.fromString(args[0]); err != nil {
return
}
cmdargs = append(cmdargs, onum)
@@ -268,7 +461,7 @@ func SwitchCommandParseAudio(args []string) (cmdstr SwitchCmdString, cmdargs []i
inum := SwitchInputNum(0)
if len(args) == 3 {
- if err = inum.FromString(args[2]); err != nil {
+ if err = inum.fromString(args[2]); err != nil {
return
}
cmdargs = append(cmdargs, inum)
@@ -360,7 +553,7 @@ func SwitchCommandParseRelay(args []string) (cmdstr SwitchCmdString, cmdargs []i
return
}
var num SwitchRelayNum
- if err = num.FromString(args[0]); err != nil {
+ if err = num.fromString(args[0]); err != nil {
return
}
cmdargs = append(cmdargs, num)
@@ -384,7 +577,7 @@ func SwitchCommandParseOC(args []string) (cmdstr SwitchCmdString, cmdargs []inte
return
}
var num SwitchOCNum
- if err = num.FromString(args[0]); err != nil {
+ if err = num.fromString(args[0]); err != nil {
return
}
cmdargs = append(cmdargs, num)
diff --git a/src/rhctl/serial_port.go b/src/rhctl/serial_port.go
index 7a0af2c..5440f99 100644
--- a/src/rhctl/serial_port.go
+++ b/src/rhctl/serial_port.go
@@ -45,6 +45,21 @@ func (b Baudrate) String() string {
return strconv.FormatUint(uint64(b.value), 10)
}
+func (b Baudrate) MarshalText() (data []byte, err error) {
+ data = []byte(b.String())
+ return
+}
+
+func (b Baudrate) MarshalJSON() (data []byte, err error) {
+ data = []byte(b.String())
+ return
+}
+
+func (b Baudrate) MarshalTOML() (data []byte, err error) {
+ data = []byte(b.String())
+ return
+}
+
func (b *Baudrate) FromUint(value uint32) error {
b.value = value
switch b.value {
@@ -114,7 +129,7 @@ func (b *Baudrate) FromUint(value uint32) error {
return nil
}
-func (b *Baudrate) FromString(str string) error {
+func (b *Baudrate) fromString(str string) error {
vuint, err := strconv.ParseUint(str, 10, 32)
if err != nil {
return err
@@ -122,8 +137,16 @@ func (b *Baudrate) FromString(str string) error {
return b.FromUint(uint32(vuint))
}
+func (b *Baudrate) UnmarshalText(data []byte) error {
+ return b.fromString(string(data))
+}
+
+func (b *Baudrate) UnmarshalJSON(data []byte) error {
+ return b.fromString(string(data))
+}
+
func (b *Baudrate) UnmarshalTOML(data []byte) error {
- return b.FromString(string(data))
+ return b.fromString(string(data))
}
type SerialPort struct {