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
34 changes: 8 additions & 26 deletions cmd/generate-bindings/solana/anchor-go/generator/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,32 +276,14 @@ func (g *Generator) gen_constants() (*OutputFile, error) {
byteGroup.Lit(uint64(val.(float64)))
case *idltype.I64:
byteGroup.Lit(int64(val.(float64)))
case *idltype.F32:
// TODO: is this correct? Are they encoded as strings?
v, err := strconv.ParseFloat(val.(string), 32)
if err != nil {
panic(fmt.Errorf("failed to parse f32 in constants[%d] %s: %w", coi, spew.Sdump(co), err))
}
byteGroup.Lit(float32(v))
case *idltype.F64:
// TODO: is this correct? Are they encoded as strings?
v, err := strconv.ParseFloat(val.(string), 64)
if err != nil {
panic(fmt.Errorf("failed to parse f64 in constants[%d] %s: %w", coi, spew.Sdump(co), err))
}
byteGroup.Lit(v)
case *idltype.String:
v, err := strconv.Unquote(val.(string))
if err != nil {
panic(fmt.Errorf("failed to unquote string in constants[%d] %s: %w", coi, spew.Sdump(co), err))
}
byteGroup.Lit(v)
case *idltype.Bool:
v, err := strconv.ParseBool(val.(string))
if err != nil {
panic(fmt.Errorf("failed to parse bool in constants[%d] %s: %w", coi, spew.Sdump(co), err))
}
byteGroup.Lit(v)
case *idltype.F32:
byteGroup.Lit(float32(val.(float64)))
case *idltype.F64:
byteGroup.Lit(val.(float64))
case *idltype.String:
byteGroup.Lit(val.(string))
case *idltype.Bool:
byteGroup.Lit(val.(bool))
default:
panic(fmt.Errorf("unsupported array type for constants[%d] %s: %T", coi, spew.Sdump(co), ty.Type))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,94 @@ func TestGenConstantsWithArrays(t *testing.T) {
assert.Contains(t, generatedCode, "var BYTE_ARRAY = [3]byte{uint8(0x1), uint8(0x2), uint8(0x3)}")
}

func TestGenConstantsWithF32Array(t *testing.T) {
constants := []idl.IdlConst{
{
Name: "FLOAT_ARRAY",
Ty: &idltype.Array{
Type: &idltype.F32{},
Size: &idltype.IdlArrayLenValue{Value: 3},
},
Value: "[1.5, 2.5, 3.5]",
},
}

idlData := &idl.Idl{Constants: constants}
gen := &Generator{idl: idlData, options: &GeneratorOptions{Package: "test"}}

outputFile, err := gen.gen_constants()
require.NoError(t, err)

generatedCode := outputFile.File.GoString()
assert.Contains(t, generatedCode, "var FLOAT_ARRAY = [3]float32{float32(1.5), float32(2.5), float32(3.5)}")
}

func TestGenConstantsWithF64Array(t *testing.T) {
constants := []idl.IdlConst{
{
Name: "DOUBLE_ARRAY",
Ty: &idltype.Array{
Type: &idltype.F64{},
Size: &idltype.IdlArrayLenValue{Value: 2},
},
Value: "[3.14159, 2.71828]",
},
}

idlData := &idl.Idl{Constants: constants}
gen := &Generator{idl: idlData, options: &GeneratorOptions{Package: "test"}}

outputFile, err := gen.gen_constants()
require.NoError(t, err)

generatedCode := outputFile.File.GoString()
assert.Contains(t, generatedCode, "var DOUBLE_ARRAY = [2]float64{3.14159, 2.71828}")
}

func TestGenConstantsWithBoolArray(t *testing.T) {
constants := []idl.IdlConst{
{
Name: "BOOL_ARRAY",
Ty: &idltype.Array{
Type: &idltype.Bool{},
Size: &idltype.IdlArrayLenValue{Value: 3},
},
Value: "[true, false, true]",
},
}

idlData := &idl.Idl{Constants: constants}
gen := &Generator{idl: idlData, options: &GeneratorOptions{Package: "test"}}

outputFile, err := gen.gen_constants()
require.NoError(t, err)

generatedCode := outputFile.File.GoString()
assert.Contains(t, generatedCode, "var BOOL_ARRAY = [3]bool{true, false, true}")
}

func TestGenConstantsWithStringArray(t *testing.T) {
constants := []idl.IdlConst{
{
Name: "STRING_ARRAY",
Ty: &idltype.Array{
Type: &idltype.String{},
Size: &idltype.IdlArrayLenValue{Value: 2},
},
Value: `["hello", "world"]`,
},
}

idlData := &idl.Idl{Constants: constants}
gen := &Generator{idl: idlData, options: &GeneratorOptions{Package: "test"}}

outputFile, err := gen.gen_constants()
require.NoError(t, err)

generatedCode := outputFile.File.GoString()
assert.Contains(t, generatedCode, `var STRING_ARRAY = [2]string{"hello", "world"}`)
}

func TestGenConstantsEdgeCases(t *testing.T) {
t.Run("No constants", func(t *testing.T) {
idlData := &idl.Idl{
Expand Down