Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions cmd/generate-bindings/solana/anchor-go/generator/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (g *Generator) gen_IDLTypeDefTyStruct(

// TODO: optionality for complex enums is a nil interface.
uniqueFieldName := uniqueFieldNames[field.Name]
fieldsGroup.Add(genFieldWithName(field, uniqueFieldName, optionality)).
fieldsGroup.Add(g.genFieldWithName(field, uniqueFieldName, optionality)).
Add(func() Code {
tagMap := map[string]string{}
if IsOption(field.Ty) {
Expand All @@ -180,7 +180,7 @@ func (g *Generator) gen_IDLTypeDefTyStruct(
fieldsGroup.Line()
optionality := IsOption(field) || IsCOption(field)

fieldsGroup.Add(genFieldNamed(
fieldsGroup.Add(g.genFieldNamed(
FormatTupleItemName(fieldIndex),
field,
optionality,
Expand Down Expand Up @@ -223,7 +223,7 @@ func (g *Generator) gen_IDLTypeDefTyStruct(
// Declare MarshalWithEncoder:
// TODO:
code.Line().Line().Add(
gen_MarshalWithEncoder_struct(
g.gen_MarshalWithEncoder_struct(
g.idl,
withDiscriminator,
exportedAccountName,
Expand All @@ -234,7 +234,7 @@ func (g *Generator) gen_IDLTypeDefTyStruct(

// Declare UnmarshalWithDecoder
code.Line().Line().Add(
gen_UnmarshalWithDecoder_struct(
g.gen_UnmarshalWithDecoder_struct(
g.idl,
withDiscriminator,
exportedAccountName,
Expand Down Expand Up @@ -284,20 +284,19 @@ func generateUniqueFieldNames(fields []idl.IdlField) map[string]string {
return fieldNameMap
}

func genField(field idl.IdlField, pointer bool) Code {
return genFieldNamed(field.Name, field.Ty, pointer)
func (g *Generator) genField(field idl.IdlField, pointer bool) Code {
return g.genFieldNamed(field.Name, field.Ty, pointer)
}

// genFieldWithName generates a field with a custom field name (for handling duplicates)
func genFieldWithName(field idl.IdlField, fieldName string, pointer bool) Code {
return genFieldNamed(fieldName, field.Ty, pointer)
func (g *Generator) genFieldWithName(field idl.IdlField, fieldName string, pointer bool) Code {
return g.genFieldNamed(fieldName, field.Ty, pointer)
}

func genFieldNamed(name string, typ idltype.IdlType, pointer bool) Code {
func (g *Generator) genFieldNamed(name string, typ idltype.IdlType, pointer bool) Code {
st := newStatement()
st.Id(tools.ToCamelUpper(name)).
Add(func() Code {
if isComplexEnum(typ) {
if g.isComplexEnum(typ) {
return nil
}
if pointer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,30 @@ import (
"github.com/gagliardetto/anchor-go/idl/idltype"
)

// typeRegistryComplexEnum contains all types that are a complex enum (and thus implemented as an interface).
var typeRegistryComplexEnum = make(map[string]struct{})

func isComplexEnum(envel idltype.IdlType) bool {
func (g *Generator) isComplexEnum(envel idltype.IdlType) bool {
switch vv := envel.(type) {
case *idltype.Defined:
_, ok := typeRegistryComplexEnum[vv.Name]
_, ok := g.complexEnumRegistry[vv.Name]
return ok
}
return false
}

func register_TypeName_as_ComplexEnum(name string) {
typeRegistryComplexEnum[name] = struct{}{}
func (g *Generator) registerComplexEnumType(name string) {
g.complexEnumRegistry[name] = struct{}{}
}

func registerComplexEnums(def idl.IdlTypeDef) {
func (g *Generator) registerComplexEnums(def idl.IdlTypeDef) {
switch vv := def.Ty.(type) {
case *idl.IdlTypeDefTyEnum:
enumTypeName := def.Name
if !vv.IsAllSimple() {
register_TypeName_as_ComplexEnum(enumTypeName)
g.registerComplexEnumType(enumTypeName)
}
case idl.IdlTypeDefTyEnum:
enumTypeName := def.Name
if !vv.IsAllSimple() {
register_TypeName_as_ComplexEnum(enumTypeName)
g.registerComplexEnumType(enumTypeName)
}
}
}
10 changes: 6 additions & 4 deletions cmd/generate-bindings/solana/anchor-go/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
var Debug = false // Set to true to enable debug logging.

type Generator struct {
options *GeneratorOptions
idl *idl.Idl
options *GeneratorOptions
idl *idl.Idl
complexEnumRegistry map[string]struct{}
}

type GeneratorOptions struct {
Expand Down Expand Up @@ -62,13 +63,14 @@ func (g *Generator) Generate() (*Output, error) {
Files: make([]*OutputFile, 0),
}

g.complexEnumRegistry = make(map[string]struct{})

{
// Register complex enums.
{
// register complex enums:
// TODO: .types is the only place where we can find complex enums? (or enums in general?)
for _, typ := range g.idl.Types {
registerComplexEnums(typ)
g.registerComplexEnums(typ)
}
}
if len(g.idl.Docs) > 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ func (g *Generator) gen_instructions() (*OutputFile, error) {
// )
// }
checkNil := true
body.BlockFunc(func(g *Group) {
gen_marshal_DefinedFieldsNamed(
g,
body.BlockFunc(func(grp *Group) {
g.gen_marshal_DefinedFieldsNamed(
grp,
instruction.Args,
checkNil,
func(param idl.IdlField) *Statement {
Expand Down
10 changes: 5 additions & 5 deletions cmd/generate-bindings/solana/anchor-go/generator/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/gagliardetto/anchor-go/tools"
)

func gen_MarshalWithEncoder_struct(
func (g *Generator) gen_MarshalWithEncoder_struct(
idl_ *idl.Idl,
withDiscriminator bool,
receiverTypeName string,
Expand Down Expand Up @@ -45,7 +45,7 @@ func gen_MarshalWithEncoder_struct(
}
switch fields := fields.(type) {
case idl.IdlDefinedFieldsNamed:
gen_marshal_DefinedFieldsNamed(
g.gen_marshal_DefinedFieldsNamed(
body,
fields,
checkNil,
Expand All @@ -60,7 +60,7 @@ func gen_MarshalWithEncoder_struct(
)
case idl.IdlDefinedFieldsTuple:
convertedFields := tupleToFieldsNamed(fields)
gen_marshal_DefinedFieldsNamed(
g.gen_marshal_DefinedFieldsNamed(
body,
convertedFields,
checkNil,
Expand Down Expand Up @@ -135,7 +135,7 @@ func gen_MarshalWithEncoder_struct(
return code
}

func gen_marshal_DefinedFieldsNamed(
func (g *Generator) gen_marshal_DefinedFieldsNamed(
body *Group,
fields idl.IdlDefinedFieldsNamed,
checkNil bool,
Expand All @@ -152,7 +152,7 @@ func gen_marshal_DefinedFieldsNamed(
body.Commentf("Serialize `%s`:", exportedArgName)
}

if isComplexEnum(field.Ty) || (IsArray(field.Ty) && isComplexEnum(field.Ty.(*idltype.Array).Type)) || (IsVec(field.Ty) && isComplexEnum(field.Ty.(*idltype.Vec).Vec)) {
if g.isComplexEnum(field.Ty) || (IsArray(field.Ty) && g.isComplexEnum(field.Ty.(*idltype.Array).Type)) || (IsVec(field.Ty) && g.isComplexEnum(field.Ty.(*idltype.Vec).Vec)) {
switch field.Ty.(type) {
case *idltype.Defined:
enumTypeName := field.Ty.(*idltype.Defined).Name
Expand Down
14 changes: 7 additions & 7 deletions cmd/generate-bindings/solana/anchor-go/generator/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (g *Generator) gen_complexEnum(name string, docs []string, typ idl.IdlTypeD
// Add comments for the enum type:
addComments(code, docs)
{
register_TypeName_as_ComplexEnum(name)
g.registerComplexEnumType(name)
containerName := formatEnumContainerName(enumTypeName)
interfaceMethodName := formatInterfaceMethodName(enumTypeName)

Expand Down Expand Up @@ -259,7 +259,7 @@ func (g *Generator) gen_complexEnum(name string, docs []string, typ idl.IdlTypeD
case idl.IdlDefinedFieldsNamed:
for _, variantField := range fields {
optionality := IsOption(variantField.Ty) || IsCOption(variantField.Ty)
structGroup.Add(genField(variantField, optionality)).
structGroup.Add(g.genField(variantField, optionality)).
Add(func() Code {
tagMap := map[string]string{}
if IsOption(variantField.Ty) {
Expand All @@ -282,7 +282,7 @@ func (g *Generator) gen_complexEnum(name string, docs []string, typ idl.IdlTypeD
for itemIndex, tupleItem := range fields {
optionality := IsOption(tupleItem) || IsCOption(tupleItem)
tupleItemName := FormatTupleItemName(itemIndex)
structGroup.Add(genFieldNamed(tupleItemName, tupleItem, optionality)).
structGroup.Add(g.genFieldNamed(tupleItemName, tupleItem, optionality)).
Add(func() Code {
tagMap := map[string]string{}
if IsOption(tupleItem) {
Expand Down Expand Up @@ -351,7 +351,7 @@ func (g *Generator) gen_complexEnum(name string, docs []string, typ idl.IdlTypeD
case idl.IdlDefinedFieldsNamed:
// Declare MarshalWithEncoder:
code.Line().Line().Add(
gen_MarshalWithEncoder_struct(
g.gen_MarshalWithEncoder_struct(
g.idl,
false,
variantTypeNameComplex,
Expand All @@ -362,7 +362,7 @@ func (g *Generator) gen_complexEnum(name string, docs []string, typ idl.IdlTypeD

// Declare UnmarshalWithDecoder
code.Line().Line().Add(
gen_UnmarshalWithDecoder_struct(
g.gen_UnmarshalWithDecoder_struct(
g.idl,
false,
variantTypeNameComplex,
Expand All @@ -374,7 +374,7 @@ func (g *Generator) gen_complexEnum(name string, docs []string, typ idl.IdlTypeD
// TODO: handle tuples
// Declare MarshalWithEncoder:
code.Line().Line().Add(
gen_MarshalWithEncoder_struct(
g.gen_MarshalWithEncoder_struct(
g.idl,
false,
variantTypeNameComplex,
Expand All @@ -385,7 +385,7 @@ func (g *Generator) gen_complexEnum(name string, docs []string, typ idl.IdlTypeD

// Declare UnmarshalWithDecoder
code.Line().Line().Add(
gen_UnmarshalWithDecoder_struct(
g.gen_UnmarshalWithDecoder_struct(
g.idl,
false,
variantTypeNameComplex,
Expand Down
10 changes: 5 additions & 5 deletions cmd/generate-bindings/solana/anchor-go/generator/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func formatEnumEncoderName(enumTypeName string) string {
return "Encode" + enumTypeName
}

func gen_UnmarshalWithDecoder_struct(
func (g *Generator) gen_UnmarshalWithDecoder_struct(
idl_ *idl.Idl,
withDiscriminator bool,
receiverTypeName string,
Expand Down Expand Up @@ -118,10 +118,10 @@ func gen_UnmarshalWithDecoder_struct(

switch fields := fields.(type) {
case idl.IdlDefinedFieldsNamed:
gen_unmarshal_DefinedFieldsNamed(body, fields)
g.gen_unmarshal_DefinedFieldsNamed(body, fields)
case idl.IdlDefinedFieldsTuple:
convertedFields := tupleToFieldsNamed(fields)
gen_unmarshal_DefinedFieldsNamed(body, convertedFields)
g.gen_unmarshal_DefinedFieldsNamed(body, convertedFields)
case nil:
// No fields, just an empty struct.
// TODO: should we panic here?
Expand Down Expand Up @@ -226,7 +226,7 @@ func tupleToFieldsNamed(
return fields
}

func gen_unmarshal_DefinedFieldsNamed(
func (g *Generator) gen_unmarshal_DefinedFieldsNamed(
body *Group,
fields idl.IdlDefinedFieldsNamed,
) {
Expand All @@ -238,7 +238,7 @@ func gen_unmarshal_DefinedFieldsNamed(
body.Commentf("Deserialize `%s`:", exportedArgName)
}

if isComplexEnum(field.Ty) || (IsArray(field.Ty) && isComplexEnum(field.Ty.(*idltype.Array).Type)) || (IsVec(field.Ty) && isComplexEnum(field.Ty.(*idltype.Vec).Vec)) {
if g.isComplexEnum(field.Ty) || (IsArray(field.Ty) && g.isComplexEnum(field.Ty.(*idltype.Array).Type)) || (IsVec(field.Ty) && g.isComplexEnum(field.Ty.(*idltype.Vec).Vec)) {
// TODO: this assumes this cannot be an option;
// - check whether this is an option?
switch field.Ty.(type) {
Expand Down