package ipp import ( "bytes" "encoding/binary" "fmt" "io" ) // Boolean is the ipp attribute boolean type Boolean struct { name string value bool } // NewBoolean creates a nre boolean attribute func NewBoolean(name string, value bool) *Boolean { b := new(Boolean) b.name = name b.value = value return b } // Name gets tha name of the boolean attribute func (b Boolean) Name() string { return b.name } func (b Boolean) String() string { return b.name + ":" + fmt.Sprint(b.value) } func (b *Boolean) valueTag() tag { return booleanValueTag } func (b *Boolean) unmarshal(byteStream io.Reader) { name, data := unmarshalSingleAttribute(byteStream) b.name = name if data[0] == 0 { b.value = false return } b.value = true } func (b *Boolean) marshal() []byte { l := 5 + len(b.name) ba := make([]byte, 0, l) buf := bytes.NewBuffer(ba) buf.WriteByte(byte(booleanValueTag)) binary.Write(buf, binary.BigEndian, uint16(len(b.name))) buf.WriteString(b.name) binary.Write(buf, binary.BigEndian, uint16(1)) if b.value { buf.WriteByte(byte(1)) } else { buf.WriteByte(byte(0)) } return buf.Bytes() } func (b *Boolean) size() int { l := 5 + len(b.name) return l }