Skip to content

Commit

Permalink
Re-arrange encoding format to move types to the start
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Jun 30, 2021
1 parent dc72ec5 commit afe7ae8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
21 changes: 11 additions & 10 deletions runtime/interpreter/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1897,6 +1897,15 @@ func decodeDictionaryMetaInfo(v *DictionaryValue, content []byte) error {
)
}

// Decode type
// TODO: store dictionary type
// Option 1: convert to sema type. - Don't have the interpreter
// Option 2: Store static type in dictionary
_, err = d.decodeStaticType()
if err != nil {
return err
}

// Lazily decode keys

var keysContent []byte
Expand Down Expand Up @@ -1939,17 +1948,9 @@ func decodeDictionaryMetaInfo(v *DictionaryValue, content []byte) error {
return err
}

// Decode type
// TODO: store dictionary type
// Option 1: convert to sema type. - Don't have the interpreter
// Option 2: Store static type in array
_, err = d.decodeStaticType()
if err != nil {
return err
}
keysContent = append(keysContent, valuesContent...)

//nolint:gocritic
v.entriesContent = append(keysContent, valuesContent...)
v.entriesContent = keysContent
v.Type = nil

return nil
Expand Down
26 changes: 13 additions & 13 deletions runtime/interpreter/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,9 +853,9 @@ func (e *EncoderV5) encodeArray(

// NOTE: NEVER change, only add/increment; ensure uint64
const (
encodedDictionaryValueKeysFieldKey uint64 = 0
encodedDictionaryValueEntriesFieldKey uint64 = 1
encodedDictionaryValueTypeFieldKey uint64 = 2
encodedDictionaryValueTypeFieldKey uint64 = 0
encodedDictionaryValueKeysFieldKey uint64 = 1
encodedDictionaryValueEntriesFieldKey uint64 = 2

// !!! *WARNING* !!!
//
Expand All @@ -871,9 +871,9 @@ const dictionaryValuePathPrefix = "v"
// cbor.Tag{
// Number: cborTagDictionaryValue,
// Content: cborArray{
// encodedDictionaryValueTypeFieldKey: []interface{}(type),
// encodedDictionaryValueKeysFieldKey: []interface{}(keys),
// encodedDictionaryValueEntriesFieldKey: []interface{}(entries),
// encodedDictionaryValueTypeFieldKey: []interface{}(type),
// },
// }
func (e *EncoderV5) encodeDictionaryValue(
Expand Down Expand Up @@ -902,7 +902,7 @@ func (e *EncoderV5) encodeDictionaryValue(

// Encode array head
err = e.enc.EncodeRawBytes([]byte{
// array, 2 items follow
// array, 3 items follow
0x83,
})
if err != nil {
Expand All @@ -912,7 +912,13 @@ func (e *EncoderV5) encodeDictionaryValue(
//nolint:gocritic
keysPath := append(path, dictionaryKeyPathPrefix)

// Encode keys (as array) at array index encodedDictionaryValueKeysFieldKey
// (1) Encode dictionary static type at array index encodedDictionaryValueTypeFieldKey
err = e.encodeStaticType(v.StaticType())
if err != nil {
return err
}

// (2) Encode keys (as array) at array index encodedDictionaryValueKeysFieldKey
err = e.encodeArray(v.Keys(), keysPath, deferrals)
if err != nil {
return err
Expand Down Expand Up @@ -944,7 +950,7 @@ func (e *EncoderV5) encodeDictionaryValue(
entriesLength = 0
}

// Encode values (as array) at array index encodedDictionaryValueEntriesFieldKey
// (3) Encode values (as array) at array index encodedDictionaryValueEntriesFieldKey
err = e.enc.EncodeArrayHead(uint64(entriesLength))
if err != nil {
return err
Expand Down Expand Up @@ -1011,12 +1017,6 @@ func (e *EncoderV5) encodeDictionaryValue(
}
}

// Encode dictionary static type at array index encodedDictionaryValueTypeFieldKey
err = e.encodeStaticType(v.StaticType())
if err != nil {
return err
}

return nil
}

Expand Down

0 comments on commit afe7ae8

Please sign in to comment.