From 475cf281e6e887f89cfd036dd6bbad65885199e5 Mon Sep 17 00:00:00 2001 From: Bart Date: Fri, 6 Sep 2024 00:06:07 +0200 Subject: [PATCH 1/2] support alternative import format --- internal/v1_1/generator.go | 5 +- internal/v1_1/generator_test.go | 75 +++++++++++++++ .../v1_1/testdata/TestValidImports.golden | 95 +++++++++++++++++++ internal/v1_1/types.go | 12 --- 4 files changed, 171 insertions(+), 16 deletions(-) create mode 100644 internal/v1_1/testdata/TestValidImports.golden diff --git a/internal/v1_1/generator.go b/internal/v1_1/generator.go index c74bd4e..0c28043 100644 --- a/internal/v1_1/generator.go +++ b/internal/v1_1/generator.go @@ -170,10 +170,7 @@ func (g Generator) processDependencies(ctx context.Context, program *ast.Program // fill in dependence information g.template.Data.Dependencies = make([]Dependency, 0) for _, imp := range imports { - contractName, err := ExtractContractName(imp.String()) - if err != nil { - return err - } + contractName := imp.Location.String() networks, err := g.generateDependenceInfo(ctx, contractName) if err != nil { return err diff --git a/internal/v1_1/generator_test.go b/internal/v1_1/generator_test.go index 5a82f34..24a0a30 100644 --- a/internal/v1_1/generator_test.go +++ b/internal/v1_1/generator_test.go @@ -61,6 +61,81 @@ func TestHelloScript(t *testing.T) { } +func TestValidImports(t *testing.T) { + contracts := []Contract{ + { + Contract: "Alice", + Networks: []Network{ + { + Network: "testnet", + Address: "0x0000000000000001", + }, + { + Network: "mainnet", + Address: "0x0000000000000001", + }, + { + Network: "emulator", + Address: "0x0000000000000001", + }, + }, + }, + { + Contract: "Bob", + Networks: []Network{ + { + Network: "testnet", + Address: "0x0000000000000002", + }, + { + Network: "mainnet", + Address: "0x0000000000000002", + }, + { + Network: "emulator", + Address: "0x0000000000000002", + }, + }, + }, + { + Contract: "Joe", + Networks: []Network{ + { + Network: "testnet", + Address: "0x0000000000000003", + }, + { + Network: "mainnet", + Address: "0x0000000000000003", + }, + { + Network: "emulator", + Address: "0x0000000000000003", + }, + }, + }, + } + + generator := Generator{ + deployedContracts: contracts, + } + + assert := assert.New(t) + code := ` + import Alice + import "Bob" + import Joe from 0x0000000000000003 + + access(all) + fun main(): Void {} +` + ctx := context.Background() + template, err := generator.CreateTemplate(ctx, code, "") + assert.NoError(err, "Generate should not return an error") + autogold.ExpectFile(t, template) + +} + func TestTransactionValue(t *testing.T) { contracts := []Contract{ { diff --git a/internal/v1_1/testdata/TestValidImports.golden b/internal/v1_1/testdata/TestValidImports.golden new file mode 100644 index 0000000..e121890 --- /dev/null +++ b/internal/v1_1/testdata/TestValidImports.golden @@ -0,0 +1,95 @@ +{ + "f_type": "InteractionTemplate", + "f_version": "1.1.0", + "id": "7a7046a0687195fa5a5dd3838fbb9a707e2b9f699e56eac9999b5cfea02b3d09", + "data": { + "type": "script", + "interface": "", + "messages": null, + "cadence": { + "body": "\n\timport Alice\n\timport \"Bob\"\n\timport \"Joe\"\n\n\taccess(all)\n\tfun main(): Void {}\n", + "network_pins": [] + }, + "dependencies": [ + { + "contracts": [ + { + "contract": "Alice", + "networks": [ + { + "network": "testnet", + "address": "0x0000000000000001", + "dependency_pin_block_height": 0 + }, + { + "network": "mainnet", + "address": "0x0000000000000001", + "dependency_pin_block_height": 0 + }, + { + "network": "emulator", + "address": "0x0000000000000001", + "dependency_pin_block_height": 0 + } + ] + } + ] + }, + { + "contracts": [ + { + "contract": "Bob", + "networks": [ + { + "network": "testnet", + "address": "0x0000000000000002", + "dependency_pin_block_height": 0 + }, + { + "network": "mainnet", + "address": "0x0000000000000002", + "dependency_pin_block_height": 0 + }, + { + "network": "emulator", + "address": "0x0000000000000002", + "dependency_pin_block_height": 0 + } + ] + } + ] + }, + { + "contracts": [ + { + "contract": "Joe", + "networks": [ + { + "network": "testnet", + "address": "0x0000000000000003", + "dependency_pin_block_height": 0 + }, + { + "network": "mainnet", + "address": "0x0000000000000003", + "dependency_pin_block_height": 0 + }, + { + "network": "emulator", + "address": "0x0000000000000003", + "dependency_pin_block_height": 0 + } + ] + } + ] + } + ], + "parameters": null, + "output": { + "label": "result", + "index": 0, + "type": "Void", + "messages": [] + } + } +} diff --git a/internal/v1_1/types.go b/internal/v1_1/types.go index 73782aa..90863e6 100644 --- a/internal/v1_1/types.go +++ b/internal/v1_1/types.go @@ -588,18 +588,6 @@ func convertToBytes(value interface{}) ([]byte, error) { } } -func ExtractContractName(importStr string) (string, error) { - // Create a regex pattern to find the contract name inside the quotes - pattern := regexp.MustCompile(`import "([^"]+)"`) - matches := pattern.FindStringSubmatch(importStr) - - if len(matches) >= 2 { - return matches[1], nil - } - - return "", fmt.Errorf("no contract name found in string") -} - func isItemInArray[T comparable](item T, slice []T) bool { for _, s := range slice { if s == item { From 6aebe72c1c3cd1c803b8f87d9ef462ec6f45369f Mon Sep 17 00:00:00 2001 From: Bart Date: Fri, 6 Sep 2024 00:16:20 +0200 Subject: [PATCH 2/2] skip built-in contract imports --- internal/v1_1/generator.go | 7 ++++ internal/v1_1/generator_test.go | 23 ++----------- .../v1_1/testdata/TestValidImports.golden | 32 +++---------------- 3 files changed, 14 insertions(+), 48 deletions(-) diff --git a/internal/v1_1/generator.go b/internal/v1_1/generator.go index 0c28043..c515f82 100644 --- a/internal/v1_1/generator.go +++ b/internal/v1_1/generator.go @@ -170,6 +170,13 @@ func (g Generator) processDependencies(ctx context.Context, program *ast.Program // fill in dependence information g.template.Data.Dependencies = make([]Dependency, 0) for _, imp := range imports { + + // Built-in contracts imports are represented with identifier location + _, isBuiltInContract := imp.Location.(common.IdentifierLocation) + if isBuiltInContract { + continue + } + contractName := imp.Location.String() networks, err := g.generateDependenceInfo(ctx, contractName) if err != nil { diff --git a/internal/v1_1/generator_test.go b/internal/v1_1/generator_test.go index 24a0a30..e834412 100644 --- a/internal/v1_1/generator_test.go +++ b/internal/v1_1/generator_test.go @@ -97,23 +97,6 @@ func TestValidImports(t *testing.T) { }, }, }, - { - Contract: "Joe", - Networks: []Network{ - { - Network: "testnet", - Address: "0x0000000000000003", - }, - { - Network: "mainnet", - Address: "0x0000000000000003", - }, - { - Network: "emulator", - Address: "0x0000000000000003", - }, - }, - }, } generator := Generator{ @@ -122,9 +105,9 @@ func TestValidImports(t *testing.T) { assert := assert.New(t) code := ` - import Alice - import "Bob" - import Joe from 0x0000000000000003 + import "Alice" + import Bob from 0x0000000000000002 + import Joe access(all) fun main(): Void {} diff --git a/internal/v1_1/testdata/TestValidImports.golden b/internal/v1_1/testdata/TestValidImports.golden index e121890..c148246 100644 --- a/internal/v1_1/testdata/TestValidImports.golden +++ b/internal/v1_1/testdata/TestValidImports.golden @@ -1,13 +1,13 @@ -{ +`{ "f_type": "InteractionTemplate", "f_version": "1.1.0", - "id": "7a7046a0687195fa5a5dd3838fbb9a707e2b9f699e56eac9999b5cfea02b3d09", + "id": "585d2dd4fc3523ba140fbe0f798c07f1d4c3792aaed72a3b0d7c32c23a6457e7", "data": { "type": "script", "interface": "", "messages": null, "cadence": { - "body": "\n\timport Alice\n\timport \"Bob\"\n\timport \"Joe\"\n\n\taccess(all)\n\tfun main(): Void {}\n", + "body": "\n\timport \"Alice\"\n\timport \"Bob\"\n\timport Joe\n\n\taccess(all)\n\tfun main(): Void {}\n", "network_pins": [] }, "dependencies": [ @@ -58,30 +58,6 @@ ] } ] - }, - { - "contracts": [ - { - "contract": "Joe", - "networks": [ - { - "network": "testnet", - "address": "0x0000000000000003", - "dependency_pin_block_height": 0 - }, - { - "network": "mainnet", - "address": "0x0000000000000003", - "dependency_pin_block_height": 0 - }, - { - "network": "emulator", - "address": "0x0000000000000003", - "dependency_pin_block_height": 0 - } - ] - } - ] } ], "parameters": null, @@ -92,4 +68,4 @@ "messages": [] } } -} +}`