Skip to content

Commit

Permalink
fix tests, update generate to keep model internal
Browse files Browse the repository at this point in the history
  • Loading branch information
bthaile committed Nov 29, 2023
1 parent f9ff459 commit f95e52d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 38 deletions.
42 changes: 24 additions & 18 deletions flixkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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,
Expand All @@ -171,6 +168,7 @@ const (
flixPath flixQueryTypes = "path"
flixId flixQueryTypes = "id"
flixUrl flixQueryTypes = "url"
flixJson flixQueryTypes = "json"
)

func isHex(str string) bool {
Expand All @@ -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):
Expand All @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions flixkit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
Expand All @@ -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")
}
Expand All @@ -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")
Expand Down
22 changes: 14 additions & 8 deletions generator/flixkitv1_0_0/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package flixkitv1_0_0

import (
"context"
"encoding/json"
"fmt"

"github.com/onflow/cadence/runtime/ast"
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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 {
Expand Down
12 changes: 4 additions & 8 deletions generator/flixkitv1_0_0/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ func TestGenerateWithPrefill(t *testing.T) {
}
}
}`
prefill, _ := flixkit.ParseFlix(templatePreFill)

code := `
import FungibleToken from 0xFungibleTokenAddress
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -134,7 +131,6 @@ func TestSimpleScriptGen(t *testing.T) {
"arguments": {}
}
}`
prefill, _ := flixkit.ParseFlix(templatePreFill)
contracts := []flixkit.Contracts{
{
"HelloWorld": flixkit.Networks{
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
9 changes: 9 additions & 0 deletions generator/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
}
Expand Down Expand Up @@ -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))
}
Expand Down

0 comments on commit f95e52d

Please sign in to comment.