diff --git a/flixkit.go b/flixkit.go index 88f7930..0cf7649 100644 --- a/flixkit.go +++ b/flixkit.go @@ -27,20 +27,17 @@ type FlowInteractionTemplateVersion struct { } type Generator interface { - Generate(ctx context.Context, code string, preFill *FlowInteractionTemplate) (*FlowInteractionTemplate, error) + Generate(ctx context.Context, code string, preFill string) (string, error) } type FlowInteractionTemplateCadence interface { - GetAndReplaceCadenceImports(templateName string) (*FlowInteractionTemplateExecution, error) + GetAndReplaceCadenceImports(templateName string) (string, error) IsTransaction() bool IsScript() bool } type FlixService interface { - GetFlixRaw(ctx context.Context, templateName string) (string, error) - GetFlix(ctx context.Context, templateName string) (string, error) - GetFlixByIDRaw(ctx context.Context, templateID string) (string, error) - GetFlixByID(ctx context.Context, templateID string) (string, error) + GetTemplate(ctx context.Context, templateName string) (string, error) GetAndReplaceCadenceImports(ctx context.Context, templateName string, network string) (*FlowInteractionTemplateExecution, error) } @@ -132,29 +129,29 @@ func FetchFlixWithContext(ctx context.Context, url string) (string, error) { } func (s *flixServiceImpl) GetAndReplaceCadenceImports(ctx context.Context, templateName string, network string) (*FlowInteractionTemplateExecution, error) { - template, err := s.getTemplate(ctx, templateName) + template, err := s.GetTemplate(ctx, templateName) if err != nil { return nil, err } var cadenceCode string - var isScript bool - var isTransaction bool - if parsedTemplate, err := v1_1.ParseFlix(template); err == nil { - cadenceCode, err = parsedTemplate.GetAndReplaceCadenceImports(network) + var replaceableCadence FlowInteractionTemplateCadence + if replaceableCadence, err = v1_1.ParseFlix(template); err == nil { + cadenceCode, err = replaceableCadence.GetAndReplaceCadenceImports(network) if err != nil { return nil, err } - isScript = parsedTemplate.IsScript() - isTransaction = parsedTemplate.IsTransaction() } - if parsedTemplate, err := ParseFlix(template); err == nil { - cadenceCode, err = parsedTemplate.GetAndReplaceCadenceImports(network) + if replaceableCadence, err = ParseFlix(template); err == nil { + cadenceCode, err = replaceableCadence.GetAndReplaceCadenceImports(network) if err != nil { return nil, err } - isScript = parsedTemplate.IsScript() - isTransaction = parsedTemplate.IsTransaction() } + if err != nil { + return nil, err + } + isScript := replaceableCadence.IsScript() + isTransaction := replaceableCadence.IsTransaction() return &FlowInteractionTemplateExecution{ Network: network, @@ -171,6 +168,7 @@ const ( flixPath flixQueryTypes = "path" flixId flixQueryTypes = "id" flixUrl flixQueryTypes = "url" + flixJson flixQueryTypes = "json" ) func isHex(str string) bool { @@ -190,6 +188,12 @@ func isUrl(str string) bool { u, err := url.Parse(str) return err == nil && u.Scheme != "" && u.Host != "" } + +func isJson(str string) bool { + var js json.RawMessage + return json.Unmarshal([]byte(str), &js) == nil +} + func getType(s string) flixQueryTypes { switch { case isPath(s): @@ -198,12 +202,14 @@ func getType(s string) flixQueryTypes { return flixId case isUrl(s): return flixUrl + case isJson(s): + return flixJson default: return flixName } } -func (s *flixServiceImpl) getTemplate(ctx context.Context, flixQuery string) (string, error) { +func (s *flixServiceImpl) GetTemplate(ctx context.Context, flixQuery string) (string, error) { var template string var err error switch getType(flixQuery) { diff --git a/flixkit_test.go b/flixkit_test.go index 60aad7d..77a079f 100644 --- a/flixkit_test.go +++ b/flixkit_test.go @@ -263,7 +263,7 @@ func TestGetFlixRaw(t *testing.T) { flixService := NewFlixService(&Config{FlixServerURL: server.URL}) ctx := context.Background() - body, err := flixService.GetFlixRaw(ctx, "templateName") + body, err := flixService.GetTemplate(ctx, "templateName") assert.NoError(err, "GetFlixByName should not return an error") assert.Equal("Hello World", body, "GetFlixByName should return the correct body") } @@ -278,7 +278,7 @@ func TestGetFlix(t *testing.T) { flixService := NewFlixService(&Config{FlixServerURL: server.URL}) ctx := context.Background() - flix, err := flixService.GetFlix(ctx, "templateName") + flix, err := flixService.GetTemplate(ctx, "templateName") assert.NoError(err, "GetParsedFlixByName should not return an error") assert.NotNil(flix, "GetParsedFlixByName should not return a nil Flix") assert.Equal(flix_template, flix, "GetParsedFlixByName should return the correct Flix") @@ -295,7 +295,7 @@ func TestGetFlixByIDRaw(t *testing.T) { flixService := NewFlixService(&Config{FlixServerURL: server.URL}) ctx := context.Background() - body, err := flixService.GetFlixByIDRaw(ctx, "templateID") + body, err := flixService.GetTemplate(ctx, "templateID") assert.NoError(err, "GetFlixByID should not return an error") assert.Equal("Hello World", body, "GetFlixByID should return the correct body") } @@ -310,7 +310,7 @@ func TestGetFlixByID(t *testing.T) { flixService := NewFlixService(&Config{FlixServerURL: server.URL}) ctx := context.Background() - flix, err := flixService.GetFlixByID(ctx, "templateID") + flix, err := flixService.GetTemplate(ctx, "templateID") assert.NoError(err, "GetParsedFlixByID should not return an error") assert.NotNil(flix, "GetParsedFlixByID should not return a nil Flix") assert.Equal(flix_template, flix, "GetParsedFlixByID should return the correct Flix") diff --git a/generator/flixkitv1_0_0/generator.go b/generator/flixkitv1_0_0/generator.go index cc8e2ba..38e0d31 100644 --- a/generator/flixkitv1_0_0/generator.go +++ b/generator/flixkitv1_0_0/generator.go @@ -2,6 +2,7 @@ package flixkitv1_0_0 import ( "context" + "encoding/json" "fmt" "github.com/onflow/cadence/runtime/ast" @@ -56,10 +57,14 @@ func NewGenerator(deployedContracts []flixkit.Contracts, coreContracts flixkit.C }, nil } -func (g Generator) Generate(ctx context.Context, code string, preFill *flixkit.FlowInteractionTemplate) (*flixkit.FlowInteractionTemplate, error) { +func (g Generator) Generate(ctx context.Context, code string, preFill string) (string, error) { template := &flixkit.FlowInteractionTemplate{} - if preFill != nil { - template = preFill + if preFill != "" { + t, err := flixkit.ParseFlix(preFill) + if err != nil { + return "", err + } + template = t } // make sure imports use new import syntax "string import" @@ -68,19 +73,19 @@ func (g Generator) Generate(ctx context.Context, code string, preFill *flixkit.F codeBytes := []byte(normalizedCode) program, err := parser.ParseProgram(nil, codeBytes, parser.Config{}) if err != nil { - return nil, err + return "", err } err = generator.ProcessParameters(program, template) if err != nil { - return nil, err + return "", err } // save v1.0.0 cadence code to template, with placeholder imports template.Data.Cadence = generator.UnNormalizeImports(normalizedCode) err = g.processDependencies(ctx, program, template) if err != nil { - return nil, err + return "", err } // ignore interface type for now @@ -89,11 +94,12 @@ func (g Generator) Generate(ctx context.Context, code string, preFill *flixkit.F template.Data.Type = generator.DetermineCadenceType(program) id, err := flixkit.GenerateFlixID(template) if err != nil { - return nil, err + return "", err } template.ID = id + templateJson, err := json.MarshalIndent(template, "", " ") - return template, nil + return string(templateJson), err } func (g Generator) processDependencies(ctx context.Context, program *ast.Program, template *flixkit.FlowInteractionTemplate) error { diff --git a/generator/flixkitv1_0_0/generator_test.go b/generator/flixkitv1_0_0/generator_test.go index 36e8023..e4bd27f 100644 --- a/generator/flixkitv1_0_0/generator_test.go +++ b/generator/flixkitv1_0_0/generator_test.go @@ -72,7 +72,6 @@ func TestGenerateWithPrefill(t *testing.T) { } } }` - prefill, _ := flixkit.ParseFlix(templatePreFill) code := ` import FungibleToken from 0xFungibleTokenAddress @@ -102,11 +101,9 @@ func TestGenerateWithPrefill(t *testing.T) { mainnetClient: nil, } ctx := context.Background() - template, err := gen.Generate(ctx, code, prefill) + prettyJSON, err := gen.Generate(ctx, code, templatePreFill) assert.NoError(err, "Generate should not return an error") - prettyJSON, err := json.MarshalIndent(template, "", " ") - assert.NoError(err, "marshal template to json should not return an error") - autogold.ExpectFile(t, string(prettyJSON)) + autogold.ExpectFile(t, prettyJSON) } func TestSimpleScriptGen(t *testing.T) { @@ -134,7 +131,6 @@ func TestSimpleScriptGen(t *testing.T) { "arguments": {} } }` - prefill, _ := flixkit.ParseFlix(templatePreFill) contracts := []flixkit.Contracts{ { "HelloWorld": flixkit.Networks{ @@ -160,7 +156,7 @@ func TestSimpleScriptGen(t *testing.T) { } ` ctx := context.Background() - template, err := generator.Generate(ctx, code, prefill) + template, err := generator.Generate(ctx, code, templatePreFill) assert.NoError(err, "Generate should not return an error") prettyJSON, err := json.MarshalIndent(template, "", " ") assert.NoError(err, "marshal template to json should not return an error") @@ -196,7 +192,7 @@ func TestMinimumValues(t *testing.T) { } ` ctx := context.Background() - template, err := generator.Generate(ctx, code, nil) + template, err := generator.Generate(ctx, code, "") assert.NoError(err, "Generate should not return an error") prettyJSON, err := json.MarshalIndent(template, "", " ") assert.NoError(err, "marshal template to json should not return an error") diff --git a/generator/utils_test.go b/generator/utils_test.go index bc50652..91d2661 100644 --- a/generator/utils_test.go +++ b/generator/utils_test.go @@ -354,6 +354,9 @@ func TestGenerateParameters(t *testing.T) { ` codeBytes := []byte(cadence) program, err := parser.ParseProgram(nil, codeBytes, parser.Config{}) + if err != nil { + t.Errorf("process parameters err %v", err) + } template, err := flixkit.ParseFlix(templateString) if err != nil { @@ -364,6 +367,9 @@ func TestGenerateParameters(t *testing.T) { t.Errorf("process parameters err %v", err) } prettyJSON, err := json.MarshalIndent(template, "", " ") + if err != nil { + t.Errorf("process parameters err %v", err) + } autogold.ExpectFile(t, string(prettyJSON)) } @@ -453,6 +459,9 @@ func TestGenerateParametersScripts(t *testing.T) { t.Errorf("process parameters err %v", err) } prettyJSON, err := json.MarshalIndent(template, "", " ") + if err != nil { + t.Errorf("process parameters err %v", err) + } autogold.ExpectFile(t, string(prettyJSON)) }