[bindings] Support aliases in package imports

This commit is contained in:
Lea Anthony 2024-01-10 21:51:54 +11:00
commit 2d97776caa
No known key found for this signature in database
GPG key ID: 33DAF7BB90A58405
9 changed files with 36 additions and 25 deletions

View file

@ -131,7 +131,7 @@ type ExternalStruct struct {
Name string
}
func GenerateBinding(thisStructName string, method *BoundMethod, useIDs bool) (string, []string, map[packagePath]map[string]*ExternalStruct) {
func (p *Project) GenerateBinding(thisStructName string, method *BoundMethod, useIDs bool) (string, []string, map[packagePath]map[string]*ExternalStruct) {
var externalStructs = make(map[packagePath]map[string]*ExternalStruct)
var models []string
template := bindingTemplate
@ -156,6 +156,7 @@ func GenerateBinding(thisStructName string, method *BoundMethod, useIDs bool) (s
result = strings.ReplaceAll(result, "Comments", comments)
var params string
for _, input := range method.Inputs {
input.project = p
inputName := sanitiseJSVarName(input.Name)
pkgName := getPackageName(input)
if pkgName != "" {
@ -202,11 +203,12 @@ func GenerateBinding(thisStructName string, method *BoundMethod, useIDs bool) (s
} else {
returns = " * @returns {Promise<"
for _, output := range method.Outputs {
output.project = p
pkgName := getPackageName(output)
if pkgName != "" {
models = append(models, pkgName)
}
jsType := output.JSType(packageName)
jsType := output.JSType(pkgName)
if jsType == "error" {
jsType = "void"
}
@ -230,7 +232,7 @@ func GenerateBinding(thisStructName string, method *BoundMethod, useIDs bool) (s
return result, lo.Uniq(models), externalStructs
}
func GenerateBindingTypescript(thisStructName string, method *BoundMethod, useIDs bool) (string, []string, map[packagePath]map[string]*ExternalStruct) {
func (p *Project) GenerateBindingTypescript(thisStructName string, method *BoundMethod, useIDs bool) (string, []string, map[packagePath]map[string]*ExternalStruct) {
var externalStructs = make(map[packagePath]map[string]*ExternalStruct)
var models []string
template := bindingTemplateTypescript
@ -255,6 +257,7 @@ func GenerateBindingTypescript(thisStructName string, method *BoundMethod, useID
result = strings.ReplaceAll(result, "Comments", comments)
var params string
for _, input := range method.Inputs {
input.project = p
inputName := sanitiseJSVarName(input.Name)
pkgName := getPackageName(input)
if pkgName != "" {
@ -298,11 +301,12 @@ func GenerateBindingTypescript(thisStructName string, method *BoundMethod, useID
} else {
returns = "Promise<"
for _, output := range method.Outputs {
output.project = p
pkgName := getPackageName(output)
if pkgName != "" {
models = append(models, pkgName)
}
jsType := output.JSType(packageName)
jsType := output.JSType(pkgName)
if jsType == "error" {
jsType = "void"
}
@ -370,9 +374,9 @@ func (p *Project) GenerateBindings(bindings map[string]map[string][]*BoundMethod
}
for _, method := range methods {
if useTypescript {
thisBinding, models, namespacedStructs = GenerateBindingTypescript(structName, method, useIDs)
thisBinding, models, namespacedStructs = p.GenerateBindingTypescript(structName, method, useIDs)
} else {
thisBinding, models, namespacedStructs = GenerateBinding(structName, method, useIDs)
thisBinding, models, namespacedStructs = p.GenerateBinding(structName, method, useIDs)
}
// Merge the namespaced structs
allNamespacedStructs = mergeNamespacedStructs(allNamespacedStructs, namespacedStructs)

View file

@ -146,7 +146,7 @@ func TestGenerateBindings(t *testing.T) {
useTypescript: true,
},
{
name: "function_from_imported_package",
name: "function_from_imported_package - CallByName",
dir: "testdata/function_from_imported_package",
want: map[string]map[string]string{
"main": {
@ -159,7 +159,7 @@ func TestGenerateBindings(t *testing.T) {
useIDs: false,
},
{
name: "function_from_imported_package",
name: "function_from_imported_package - CallById",
dir: "testdata/function_from_imported_package",
want: map[string]map[string]string{
"main": {

View file

@ -66,15 +66,22 @@ type EnumValue struct {
}
type Parameter struct {
Name string
Type *ParameterType
Name string
Type *ParameterType
project *Project
}
func (p *Parameter) NamespacedStructType(pkgName string) string {
var typeName string
if p.Type.Package != "" && p.Type.Package != pkgName {
parts := strings.Split(p.Type.Package, "/")
typeName = parts[len(parts)-1] + "."
thisPkg := p.project.packageCache[pkgName]
pkgInfo := p.project.packageCache[p.Type.Package]
if pkgInfo.Name != "" && pkgInfo.Path != thisPkg.Path {
typeName = pkgInfo.Name
} else {
if p.Type.Package != "" && p.Type.Package != pkgName {
parts := strings.Split(p.Type.Package, "/")
typeName = parts[len(parts)-1] + "."
}
}
return typeName + p.Type.Name
}

View file

@ -165,7 +165,7 @@ func TestParseEnum(t *testing.T) {
for packageName, packageData := range tt.wantBoundMethods {
for structName, wantBoundMethods := range packageData {
gotBoundMethods := got.BoundMethods[packageName][structName]
if diff := cmp.Diff(wantBoundMethods, gotBoundMethods); diff != "" {
if diff := cmp.Diff(wantBoundMethods, gotBoundMethods, cmp.AllowUnexported(Parameter{})); diff != "" {
t.Errorf("ParseDirectory() failed:\n" + diff)
}
}
@ -181,7 +181,7 @@ func TestParseEnum(t *testing.T) {
}
}
if diff := cmp.Diff(tt.wantBoundMethods, got.BoundMethods); diff != "" {
if diff := cmp.Diff(tt.wantBoundMethods, got.BoundMethods, cmp.AllowUnexported(Parameter{})); diff != "" {
t.Errorf("ParseDirectory() failed:\n" + diff)
}
if !reflect.DeepEqual(tt.wantModels, got.Models) {

View file

@ -209,7 +209,7 @@ func TestParseFunction(t *testing.T) {
for packageName, packageData := range tt.wantBoundMethods {
for structName, wantBoundMethods := range packageData {
gotBoundMethods := got.BoundMethods[packageName][structName]
if diff := cmp.Diff(wantBoundMethods, gotBoundMethods); diff != "" {
if diff := cmp.Diff(wantBoundMethods, gotBoundMethods, cmp.AllowUnexported(Parameter{})); diff != "" {
t.Errorf("ParseDirectory() failed:\n" + diff)
}
}
@ -226,7 +226,7 @@ func TestParseFunction(t *testing.T) {
}
if !reflect.DeepEqual(tt.wantBoundMethods, got.BoundMethods) {
t.Errorf("ParseDirectory() failed:\n" + cmp.Diff(tt.wantBoundMethods, got.BoundMethods))
t.Errorf("ParseDirectory() failed:\n" + cmp.Diff(tt.wantBoundMethods, got.BoundMethods, cmp.AllowUnexported(Parameter{})))
//spew.Dump(tt.wantBoundMethods)
//spew.Dump(got.BoundMethods)
}

View file

@ -257,7 +257,7 @@ func TestParseStructLiteralMultiple(t *testing.T) {
for packageName, packageData := range tt.wantBoundMethods {
for structName, wantBoundMethods := range packageData {
gotBoundMethods := got.BoundMethods[packageName][structName]
if diff := cmp.Diff(wantBoundMethods, gotBoundMethods); diff != "" {
if diff := cmp.Diff(wantBoundMethods, gotBoundMethods, cmp.AllowUnexported(Parameter{})); diff != "" {
t.Errorf("ParseDirectory() failed:\n" + diff)
}
}
@ -273,7 +273,7 @@ func TestParseStructLiteralMultiple(t *testing.T) {
}
}
if diff := cmp.Diff(tt.wantBoundMethods, got.BoundMethods); diff != "" {
if diff := cmp.Diff(tt.wantBoundMethods, got.BoundMethods, cmp.AllowUnexported(Parameter{})); diff != "" {
t.Errorf("ParseDirectory() failed:\n" + diff)
}
if !reflect.DeepEqual(tt.wantModels, got.Models) {

View file

@ -1133,7 +1133,7 @@ func TestParseStructLiteralNonPointerSingle(t *testing.T) {
for packageName, packageData := range tt.wantBoundMethods {
for structName, wantBoundMethods := range packageData {
gotBoundMethods := got.BoundMethods[packageName][structName]
if diff := cmp.Diff(wantBoundMethods, gotBoundMethods); diff != "" {
if diff := cmp.Diff(wantBoundMethods, gotBoundMethods, cmp.AllowUnexported(Parameter{})); diff != "" {
t.Errorf("ParseDirectory() failed:\n" + diff)
}
}
@ -1149,7 +1149,7 @@ func TestParseStructLiteralNonPointerSingle(t *testing.T) {
}
}
if diff := cmp.Diff(tt.wantBoundMethods, got.BoundMethods); diff != "" {
if diff := cmp.Diff(tt.wantBoundMethods, got.BoundMethods, cmp.AllowUnexported(Parameter{})); diff != "" {
t.Errorf("ParseDirectory() failed:\n" + diff)
}
if !reflect.DeepEqual(tt.wantModels, got.Models) {

View file

@ -1134,7 +1134,7 @@ func TestParseStructLiteralSingle(t *testing.T) {
for packageName, packageData := range tt.wantBoundMethods {
for structName, wantBoundMethods := range packageData {
gotBoundMethods := got.BoundMethods[packageName][structName]
if diff := cmp.Diff(wantBoundMethods, gotBoundMethods); diff != "" {
if diff := cmp.Diff(wantBoundMethods, gotBoundMethods, cmp.AllowUnexported(Parameter{})); diff != "" {
t.Errorf("ParseDirectory() failed:\n" + diff)
}
}
@ -1150,7 +1150,7 @@ func TestParseStructLiteralSingle(t *testing.T) {
}
}
if diff := cmp.Diff(tt.wantBoundMethods, got.BoundMethods); diff != "" {
if diff := cmp.Diff(tt.wantBoundMethods, got.BoundMethods, cmp.AllowUnexported(Parameter{})); diff != "" {
t.Errorf("ParseDirectory() failed:\n" + diff)
}
if !reflect.DeepEqual(tt.wantModels, got.Models) {

View file

@ -244,7 +244,7 @@ func TestParseVariableSingle(t *testing.T) {
for packageName, packageData := range tt.wantBoundMethods {
for structName, wantBoundMethods := range packageData {
gotBoundMethods := got.BoundMethods[packageName][structName]
if diff := cmp.Diff(wantBoundMethods, gotBoundMethods); diff != "" {
if diff := cmp.Diff(wantBoundMethods, gotBoundMethods, cmp.AllowUnexported(Parameter{})); diff != "" {
t.Errorf("ParseDirectory() failed:\n" + diff)
}
}
@ -260,7 +260,7 @@ func TestParseVariableSingle(t *testing.T) {
}
}
if diff := cmp.Diff(tt.wantBoundMethods, got.BoundMethods); diff != "" {
if diff := cmp.Diff(tt.wantBoundMethods, got.BoundMethods, cmp.AllowUnexported(Parameter{})); diff != "" {
t.Errorf("ParseDirectory() failed:\n" + diff)
}
if !reflect.DeepEqual(tt.wantModels, got.Models) {