Add integer attribute type.
Merges some common code between integer and enum.
Enum can now bw a set.
Add valuer must take a interface{} in addValue since it can be different types.
This commit is contained in:
@@ -1,23 +1,18 @@
|
||||
package ipp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type enum struct {
|
||||
name string
|
||||
value int32
|
||||
name string
|
||||
values []int32
|
||||
}
|
||||
|
||||
func NewEnum(name string, value int32) *enum {
|
||||
func NewEnum(name string, values ...int32) *enum {
|
||||
e := new(enum)
|
||||
e.name = name
|
||||
e.value = value
|
||||
e.values = values
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -26,32 +21,21 @@ func (e enum) Name() string {
|
||||
}
|
||||
|
||||
func (e enum) String() string {
|
||||
return e.name + ":" + fmt.Sprint(e.value)
|
||||
return e.name + ":" + fmt.Sprint(e.values)
|
||||
}
|
||||
|
||||
func (e *enum) valueTag() tag {
|
||||
return enumValueTag
|
||||
}
|
||||
|
||||
func (e *enum) unmarshal(byteStream io.Reader) {
|
||||
log.Warn("Unmarshal of enum is not implemented yet")
|
||||
func (e *enum) size() int {
|
||||
return 9 + len(e.name)
|
||||
}
|
||||
|
||||
func (i *enum) addValue(v interface{}) {
|
||||
i.values = append(i.values, v.(int32))
|
||||
}
|
||||
|
||||
func (e *enum) marshal() []byte {
|
||||
l := 3 + len(e.name) + 6
|
||||
b := make([]byte, 0, l)
|
||||
buf := bytes.NewBuffer(b)
|
||||
buf.WriteByte(byte(enumValueTag))
|
||||
binary.Write(buf, binary.BigEndian, uint16(len(e.name)))
|
||||
buf.WriteString(e.name)
|
||||
binary.Write(buf, binary.BigEndian, uint16(4))
|
||||
binary.Write(buf, binary.BigEndian, e.value)
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
func (e *enum) size() int {
|
||||
l := 1 + 4 // The attribute tag + 2 lengths
|
||||
l += len(e.name)
|
||||
l += 4
|
||||
return l
|
||||
return marshalInteger(enumValueTag, e.name, e.values)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user