Skip to content
Open
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
66 changes: 41 additions & 25 deletions codegen/astgen/v3/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,37 +310,25 @@ func (c *codeGenAst) getTemplates(
if interfaceStruct, exists := interfaces[ifcModuleName][interfaceName]; exists {
log.Debug().Msgf("found interface %s in map with %d choices", interfaceName, len(interfaceStruct.Choices))
for _, ifaceChoice := range interfaceStruct.Choices {
found := false
var existingChoice *model.TmplChoice
for _, tmplChoice := range tmplStruct.Choices {
if tmplChoice.Name == ifaceChoice.Name {
found = true
existingChoice = tmplChoice
break
}
}
if !found {
log.Debug().Msgf("adding interface choice %s to template %s", ifaceChoice.Name, templateName)
if extPkg != (model.ExternalPackage{}) {
log.Debug().Msg("Interface choice is from an external package, adding using imports")
tmplStruct.Choices = append(tmplStruct.Choices, &model.TmplChoice{
Name: ifaceChoice.Name,
ArgType: model.Imported{
Underlying: ifaceChoice.ArgType,
ExternalPackage: extPkg,
},
// ReturnType: ifaceChoice.ReturnType,
InterfaceName: interfaceName,
InterfaceDAMLName: interfaceStruct.DAMLName,
})
} else {
tmplStruct.Choices = append(tmplStruct.Choices, &model.TmplChoice{
Name: ifaceChoice.Name,
ArgType: ifaceChoice.ArgType,
// ReturnType: ifaceChoice.ReturnType,
InterfaceName: interfaceName,
InterfaceDAMLName: interfaceStruct.DAMLName,
})
}
if existingChoice != nil {
c.mergeInterfaceChoice(existingChoice, ifaceChoice, interfaceName, interfaceStruct.DAMLName, extPkg)
continue
}

log.Debug().Msgf("adding interface choice %s to template %s", ifaceChoice.Name, templateName)
tmplStruct.Choices = append(tmplStruct.Choices, &model.TmplChoice{
Name: ifaceChoice.Name,
ArgType: interfaceChoiceArgType(ifaceChoice, extPkg),
InterfaceName: interfaceName,
InterfaceDAMLName: interfaceStruct.DAMLName,
})
}
}
}
Expand All @@ -353,6 +341,34 @@ func (c *codeGenAst) getTemplates(
return structs, nil
}

func (c *codeGenAst) mergeInterfaceChoice(
existingChoice *model.TmplChoice,
ifaceChoice *model.TmplChoice,
interfaceName string,
interfaceDAMLName string,
extPkg model.ExternalPackage,
) {
existingChoice.InterfaceName = interfaceName
existingChoice.InterfaceDAMLName = interfaceDAMLName
if ifaceChoice.ArgType != nil {
existingChoice.ArgType = interfaceChoiceArgType(ifaceChoice, extPkg)
}
}

func interfaceChoiceArgType(ifaceChoice *model.TmplChoice, extPkg model.ExternalPackage) model.DamlType {
if ifaceChoice == nil || ifaceChoice.ArgType == nil {
return nil
}
if extPkg != (model.ExternalPackage{}) {
log.Debug().Msg("Interface choice is from an external package, adding using imports")
return model.Imported{
Underlying: ifaceChoice.ArgType,
ExternalPackage: extPkg,
}
}
return ifaceChoice.ArgType
}

func (c *codeGenAst) getChoices(pkg *daml.Package, choices []*daml.TemplateChoice) []*model.TmplChoice {
res := make([]*model.TmplChoice, 0)
for _, choice := range choices {
Expand Down
41 changes: 41 additions & 0 deletions codegen/astgen/v3/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

daml "github.com/digital-asset/dazl-client/v8/go/api/com/digitalasset/daml/lf/archive/daml_lf_2"
"github.com/smartcontractkit/go-daml/codegen/model"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -103,3 +104,43 @@ func TestParseKeyExpressionV3(t *testing.T) {
require.Len(t, fieldNames, 0)
})
}

func TestMergeInterfaceChoiceUsesExternalSignature(t *testing.T) {
codeGen := &codeGenAst{}
existing := &model.TmplChoice{
Name: "CalculateFee",
ArgType: model.Unknown{String: "ExecutorCalculateFee2"},
}
ifaceChoice := &model.TmplChoice{
Name: "CalculateFee",
ArgType: model.Unknown{String: "ExecutorCalculateFee"},
}
extPkg := model.ExternalPackage{
Import: "github.com/smartcontractkit/chainlink-canton/bindings/generated/common",
Alias: "common",
}

codeGen.mergeInterfaceChoice(existing, ifaceChoice, "IExecutor", "Executor", extPkg)

require.Equal(t, "IExecutor", existing.InterfaceName)
require.Equal(t, "Executor", existing.InterfaceDAMLName)
require.Equal(t, "common.ExecutorCalculateFee", existing.ArgType.GoType())
}

func TestMergeInterfaceChoiceUsesLocalSignature(t *testing.T) {
codeGen := &codeGenAst{}
existing := &model.TmplChoice{
Name: "CalculateFee",
ArgType: model.Unknown{String: "ExecutorCalculateFee2"},
}
ifaceChoice := &model.TmplChoice{
Name: "CalculateFee",
ArgType: model.Unknown{String: "ExecutorCalculateFee"},
}

codeGen.mergeInterfaceChoice(existing, ifaceChoice, "IExecutor", "Executor", model.ExternalPackage{})

require.Equal(t, "IExecutor", existing.InterfaceName)
require.Equal(t, "Executor", existing.InterfaceDAMLName)
require.Equal(t, "ExecutorCalculateFee", existing.ArgType.GoType())
}
Loading