Simplify unmarshalling.

This commit is contained in:
Henrik, Sölver
2021-06-09 19:51:48 +02:00
committed by Henrik Sölver
parent 61abe8dbd4
commit 71fcac40f0
17 changed files with 452 additions and 179 deletions

View File

@@ -97,18 +97,18 @@ const (
Stopped printerState = 5
)
// jobstate defined in rfc8011 ch 5.3.7
type jobState uint16
// // jobstate defined in rfc8011 ch 5.3.7
// type jobState uint16
const (
pending jobState = 3
pendingHeld jobState = 4
processing jobState = 5
processingStoppped jobState = 6
cancelled jobState = 7
aborted jobState = 8
completed jobState = 9
)
// const (
// pending jobState = 3
// pendingHeld jobState = 4
// processing jobState = 5
// processingStoppped jobState = 6
// cancelled jobState = 7
// aborted jobState = 8
// completed jobState = 9
// )
type statusCode uint16
@@ -128,19 +128,8 @@ func (v versionNumber) String() string {
return fmt.Sprintf("%x.%x", vn&0xff00>>8, vn&0x00ff)
}
func unmarshalSingleAttribute(byteStream io.Reader) (string, []byte) {
var length uint16
binary.Read(byteStream, binary.BigEndian, &length)
attributeName := make([]byte, length)
if length > 0 {
binary.Read(byteStream, binary.BigEndian, attributeName)
}
binary.Read(byteStream, binary.BigEndian, &length)
attributeValue := make([]byte, length)
binary.Read(byteStream, binary.BigEndian, attributeValue)
return string(attributeName), attributeValue
}
// unmarshalSingleValue reads a single key value pair from the byte stream
// RFC 8010: Attribute-with-one-value
func unmarshalSingleValue(byteStream io.Reader) (string, string) {
var length uint16
binary.Read(byteStream, binary.BigEndian, &length)
@@ -165,11 +154,52 @@ func marshalNameValue(name, value string, b []byte) {
copy(b[p:], []byte(value))
}
// func readAttribute(byteStream *bufio.Reader) (string, [][]byte) {
// var length uint16
// values := make([][]byte, 0, 1)
// binary.Read(byteStream, binary.BigEndian, &length)
// attributeName := make([]byte, length)
// if length > 0 {
// binary.Read(byteStream, binary.BigEndian, attributeName)
// }
// binary.Read(byteStream, binary.BigEndian, attributeValue)
// values = append(values, string(attributeValue))
// next, err := byteStream.Peek(1)
// if err != nil {
// panic("Failed to peek")
// }
// if next[0] != byte(keyWordValueTag) {
// // No additional values
// return string(attributeName), values
// }
// binary.Read(byteStream, binary.BigEndian, &length)
// for length > 0 {
// attributeValue := make([]byte, length)
// binary.Read(byteStream, binary.BigEndian, &length)
// attributeValue := make([]byte, length)
// if length > 0 {
// binary.Read(byteStream, binary.BigEndian, attributeValue)
// }
// values = append(values, string(attributeValue))
// next, err := byteStream.Peek(1)
// if err != nil {
// panic("Failed to peek")
// }
// if next[0] != byte(keyWordValueTag) {
// // No additional values
// return string(attributeName), values
// }
// }
// }
type Attribute interface {
Name() string
valueTag() tag
marshal() []byte
//size() int
}
type Attributes struct {
@@ -230,11 +260,10 @@ func UnMarshalAttributes(bytestream *bufio.Reader) *Attributes {
}
currentAttributeGroup := t
var lastAddValuer AddValuer
for {
err = binary.Read(bytestream, binary.BigEndian, &t)
if err != nil {
log.Fatal("End of input before end of attributes tag (%v)", err.Error())
log.Fatalf("End of input before end of attributes tag (%v)", err.Error())
}
log.Debugf("Value tag - %v", t)
switch t {
@@ -260,15 +289,10 @@ func UnMarshalAttributes(bytestream *bufio.Reader) *Attributes {
a.addAttribute(currentAttributeGroup, n)
log.Debugf("%v %v", n.name, n.value)
case keyWordValueTag:
name, value := unmarshalSingleValue(bytestream)
if name == "" {
lastAddValuer.addValue(value)
} else {
k := NewKeyWord(name, value)
a.addAttribute(currentAttributeGroup, k)
lastAddValuer = k
}
log.Debugf("%v : %v", name, value)
k := NewKeyWord("", "")
k.unmarshal(bytestream)
a.addAttribute(currentAttributeGroup, k)
log.Debugf("%v : %v", k.Name(), k.String())
case nameWithoutLanguageValueTag:
n := NewNameWithoutLanguage("", "")
n.unmarshal(bytestream)
@@ -280,45 +304,25 @@ func UnMarshalAttributes(bytestream *bufio.Reader) *Attributes {
a.addAttribute(currentAttributeGroup, attr)
log.Debugf("%v %v", attr.name, attr.value)
case mimeMediaTypeValueTag:
name, value := unmarshalSingleValue(bytestream)
if name == "" {
lastAddValuer.addValue(value)
} else {
m := NewMimeMediaType(name, value)
a.addAttribute(currentAttributeGroup, m)
lastAddValuer = m
}
log.Debugf("%v : %v", name, value)
m := NewMimeMediaType("")
m.unmarshal(bytestream)
a.addAttribute(currentAttributeGroup, m)
log.Debugf("%v : %v", m.Name(), m.String())
case integerValueTag:
name, value := unmarshalSingleInteger(bytestream)
if name == "" {
lastAddValuer.addValue(value)
} else {
i := NewInteger(name, value)
a.addAttribute(currentAttributeGroup, i)
lastAddValuer = i
}
log.Debugf("%v : %v", name, value)
i := NewInteger("")
i.unmarshal(bytestream)
a.addAttribute(currentAttributeGroup, i)
log.Debugf("%v : %v", i.name, i.values)
case rangeOfIntegerValueTag:
name, value := unmarshalSingleRangeOfInteger(bytestream)
if name == "" {
lastAddValuer.addValue(value)
} else {
r := NewRangeOfInteger(name, value)
a.addAttribute(currentAttributeGroup, r)
lastAddValuer = r
}
log.Debugf("%v : %v", name, value)
r := NewRangeOfInteger("")
r.unmarshal(bytestream)
a.addAttribute(currentAttributeGroup, r)
log.Debugf("%v : %v", r.name, r.values)
case enumValueTag:
name, value := unmarshalSingleInteger(bytestream)
if name == "" {
lastAddValuer.addValue(value)
} else {
e := NewEnum(name, value)
a.addAttribute(currentAttributeGroup, e)
lastAddValuer = e
}
log.Debugf("%v : %v", name, value)
e := NewEnum("")
e.unmarshal(bytestream)
a.addAttribute(currentAttributeGroup, e)
log.Debugf("%v : %v", e.name, e.values)
case begCollectionValueTag:
// For now just consume the collection
consumeCollection(bytestream)
@@ -326,7 +330,6 @@ func UnMarshalAttributes(bytestream *bufio.Reader) *Attributes {
attr := NewUnsupportedValue()
attr.unmarshal(bytestream)
a.addAttribute(currentAttributeGroup, attr)
case jobAttributes:
log.Debug("Start job attributes")
currentAttributeGroup = jobAttributes