Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
ilija42 committed Dec 11, 2024
1 parent 5b636b9 commit 88206dc
Show file tree
Hide file tree
Showing 15 changed files with 943 additions and 586 deletions.
38 changes: 32 additions & 6 deletions pkg/solana/codec/codec_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,26 @@ func NewEntry(idl IDL, idlTypeDef IdlTypeDef, includeDiscriminator bool, mod cod
mod = codec.MultiModifier{}
}

_, accCodec, err := createCodecType(idlTypeDef, refs, includeDiscriminator)
_, accCodec, err := createCodecType(idlTypeDef, refs, false)
if err != nil {
return nil, err
}

return &codecEntry{codecType: accCodec, typ: accCodec.GetType(), mod: mod}, nil
entry := &codecEntry{name: idlTypeDef.Name, includeDiscriminator: includeDiscriminator, codecType: accCodec, typ: accCodec.GetType(), mod: mod}
if entry.includeDiscriminator {
entry.Discriminator = commonencodings.NamedTypeCodec{Name: "Discriminator" + idlTypeDef.Name, Codec: NewDiscriminator(idlTypeDef.Name)}
}

return entry, nil
}

type codecEntry struct {
typ reflect.Type
codecType commonencodings.TypeCodec
mod codec.Modifier
name string
includeDiscriminator bool
Discriminator commonencodings.NamedTypeCodec
typ reflect.Type
codecType commonencodings.TypeCodec
mod codec.Modifier
}

func (entry *codecEntry) GetType() reflect.Type {
Expand All @@ -52,10 +60,28 @@ func (entry *codecEntry) GetCodecType() commonencodings.TypeCodec {
}

func (entry *codecEntry) Encode(value any, into []byte) ([]byte, error) {
return entry.codecType.Encode(value, into)
encodedVal, err := entry.codecType.Encode(value, into)
if err != nil {
return nil, err
}

if entry.includeDiscriminator {
var byt []byte
disc := NewDiscriminator(entry.name)
encodedDisc, err := disc.Encode(&disc.hashPrefix, byt)
if err != nil {
return nil, err
}
return append(encodedDisc, encodedVal...), nil
}

return encodedVal, nil
}

func (entry *codecEntry) Decode(encoded []byte) (any, []byte, error) {
if entry.includeDiscriminator {
encoded = encoded[discriminatorLength:]
}
return entry.codecType.Decode(encoded)
}

Expand Down
Loading

0 comments on commit 88206dc

Please sign in to comment.