From 2d97776caac95e6ecb253fd949278548ae0c13da Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Wed, 10 Jan 2024 21:51:54 +1100 Subject: [PATCH] [bindings] Support aliases in package imports --- v3/internal/parser/bindings.go | 16 ++++++++++------ v3/internal/parser/bindings_test.go | 4 ++-- v3/internal/parser/parser.go | 17 ++++++++++++----- v3/internal/parser/parser_enum_test.go | 4 ++-- v3/internal/parser/parser_function_test.go | 4 ++-- .../parser_struct_literal_multiple_test.go | 4 ++-- ...er_struct_literal_non_pointer_single_test.go | 4 ++-- .../parser/parser_struct_literal_single_test.go | 4 ++-- .../parser/parser_variable_single_test.go | 4 ++-- 9 files changed, 36 insertions(+), 25 deletions(-) diff --git a/v3/internal/parser/bindings.go b/v3/internal/parser/bindings.go index 854238b01..a7adf4e6e 100644 --- a/v3/internal/parser/bindings.go +++ b/v3/internal/parser/bindings.go @@ -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) diff --git a/v3/internal/parser/bindings_test.go b/v3/internal/parser/bindings_test.go index 8a1447b03..6d91460be 100644 --- a/v3/internal/parser/bindings_test.go +++ b/v3/internal/parser/bindings_test.go @@ -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": { diff --git a/v3/internal/parser/parser.go b/v3/internal/parser/parser.go index 8025a3f90..8feacfc4c 100644 --- a/v3/internal/parser/parser.go +++ b/v3/internal/parser/parser.go @@ -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 } diff --git a/v3/internal/parser/parser_enum_test.go b/v3/internal/parser/parser_enum_test.go index bf5b3513b..e3a7e5bdf 100644 --- a/v3/internal/parser/parser_enum_test.go +++ b/v3/internal/parser/parser_enum_test.go @@ -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) { diff --git a/v3/internal/parser/parser_function_test.go b/v3/internal/parser/parser_function_test.go index bb979ef96..c11d3aa91 100644 --- a/v3/internal/parser/parser_function_test.go +++ b/v3/internal/parser/parser_function_test.go @@ -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) } diff --git a/v3/internal/parser/parser_struct_literal_multiple_test.go b/v3/internal/parser/parser_struct_literal_multiple_test.go index c15080f4a..09d962f37 100644 --- a/v3/internal/parser/parser_struct_literal_multiple_test.go +++ b/v3/internal/parser/parser_struct_literal_multiple_test.go @@ -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) { diff --git a/v3/internal/parser/parser_struct_literal_non_pointer_single_test.go b/v3/internal/parser/parser_struct_literal_non_pointer_single_test.go index 694c4e516..ad06666e1 100644 --- a/v3/internal/parser/parser_struct_literal_non_pointer_single_test.go +++ b/v3/internal/parser/parser_struct_literal_non_pointer_single_test.go @@ -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) { diff --git a/v3/internal/parser/parser_struct_literal_single_test.go b/v3/internal/parser/parser_struct_literal_single_test.go index 6f46e638a..4b7b25831 100644 --- a/v3/internal/parser/parser_struct_literal_single_test.go +++ b/v3/internal/parser/parser_struct_literal_single_test.go @@ -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) { diff --git a/v3/internal/parser/parser_variable_single_test.go b/v3/internal/parser/parser_variable_single_test.go index 4a8794e25..aeacf5f98 100644 --- a/v3/internal/parser/parser_variable_single_test.go +++ b/v3/internal/parser/parser_variable_single_test.go @@ -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) {