Skip to content

Commit

Permalink
Merge pull request phin1x#32 from pennyblack-io/ordered-operation-att…
Browse files Browse the repository at this point in the history
…ributes

Encode required operation attributes first
  • Loading branch information
phin1x authored Jun 25, 2023
2 parents 3cb709b + 5e57e14 commit 83741db
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,8 @@ func (r *Request) Encode() ([]byte, error) {
}

if len(r.OperationAttributes) > 0 {
for attr, value := range r.OperationAttributes {
if err := enc.Encode(attr, value); err != nil {
return nil, err
}
if err := r.encodeOperationAttributes(enc); err != nil {
return nil, err
}
}

Expand Down Expand Up @@ -107,6 +105,31 @@ func (r *Request) Encode() ([]byte, error) {
return buf.Bytes(), nil
}

func (r *Request) encodeOperationAttributes(enc *AttributeEncoder) error {
ordered := []string{
AttributeCharset,
AttributeNaturalLanguage,
AttributePrinterURI,
AttributeJobID,
}

for _, attr := range ordered {
if value, ok := r.OperationAttributes[attr]; ok {
delete(r.OperationAttributes, attr)
if err := enc.Encode(attr, value); err != nil {
return err
}
}
}

for attr, value := range r.OperationAttributes {
if err := enc.Encode(attr, value); err != nil {
return err
}
}
return nil
}

// RequestDecoder reads and decodes a request from a stream
type RequestDecoder struct {
reader io.Reader
Expand Down

0 comments on commit 83741db

Please sign in to comment.