From 696468ab892cff42610ce0df6c329a8f40db1abd Mon Sep 17 00:00:00 2001 From: Slavomir Date: Fri, 10 Sep 2021 17:09:04 +0200 Subject: [PATCH] Add NoTypeIDEncoding --- variant.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/variant.go b/variant.go index 6d00cb4..bbbc9c7 100644 --- a/variant.go +++ b/variant.go @@ -122,8 +122,12 @@ const ( // written using the anchor SDK. // The typeID is the sighash of the instruction. AnchorTypeIDEncoding + // No type ID; ONLY ONE VARIANT PER PROGRAM. + NoTypeIDEncoding ) +var NoTypeIDDefaultID = TypeIDFromUint8(0) + // NewVariantDefinition creates a variant definition based on the *ordered* provided types. // // - For anchor instructions, it's the name that defines the binary variant value. @@ -199,6 +203,22 @@ func NewVariantDefinition(typeIDEncoding TypeIDEncoding, types []VariantType) (o out.typeIDToName[typeID] = typeDef.Name out.typeNameToID[typeDef.Name] = typeID } + case NoTypeIDEncoding: + if len(types) != 1 { + panic(fmt.Sprintf("NoTypeIDEncoding can only have one variant type definition, got %v", len(types))) + } + typeDef := types[0] + + typeID := NoTypeIDDefaultID + + // FIXME: Check how the reflect.Type is used and cache all its usage in the definition. + // Right now, on each Unmarshal, we re-compute some expensive stuff that can be + // re-used like the `typeGo.Elem()` which is always the same. It would be preferable + // to have those already pre-defined here so we can actually speed up the + // Unmarshal code. + out.typeIDToType[typeID] = reflect.TypeOf(typeDef.Type) + out.typeIDToName[typeID] = typeDef.Name + out.typeNameToID[typeDef.Name] = typeID default: panic(fmt.Errorf("unsupported TypeIDEncoding: %v", typeIDEncoding)) @@ -336,6 +356,8 @@ func (a *BaseVariant) UnmarshalBinaryVariant(decoder *Decoder, def *VariantDefin if err != nil { return fmt.Errorf("anchor: unable to read variant type id: %s", err) } + case NoTypeIDEncoding: + typeID = NoTypeIDDefaultID } a.TypeID = typeID