From da44524d0e2ae94837710768810de4dc50ef0005 Mon Sep 17 00:00:00 2001 From: "zev.ac" Date: Tue, 19 Nov 2024 04:33:58 +0530 Subject: [PATCH] Expose public methods to convert function and variable decl to v1 Decl (#1069) --- common/decls/decls.go | 10 ++++++++++ common/decls/decls_test.go | 6 +++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/common/decls/decls.go b/common/decls/decls.go index f67808fe..bfeb52c5 100644 --- a/common/decls/decls.go +++ b/common/decls/decls.go @@ -782,6 +782,11 @@ func TypeVariable(t *types.Type) *VariableDecl { return NewVariable(t.TypeName(), types.NewTypeTypeWithParam(t)) } +// VariableDeclToExprDecl converts a go-native variable declaration into a protobuf-type variable declaration. +func VariableDeclToExprDecl(v *VariableDecl) (*exprpb.Decl, error) { + return variableDeclToExprDecl(v) +} + // variableDeclToExprDecl converts a go-native variable declaration into a protobuf-type variable declaration. func variableDeclToExprDecl(v *VariableDecl) (*exprpb.Decl, error) { varType, err := types.TypeToExprType(v.Type()) @@ -791,6 +796,11 @@ func variableDeclToExprDecl(v *VariableDecl) (*exprpb.Decl, error) { return chkdecls.NewVar(v.Name(), varType), nil } +// FunctionDeclToExprDecl converts a go-native function declaration into a protobuf-typed function declaration. +func FunctionDeclToExprDecl(f *FunctionDecl) (*exprpb.Decl, error) { + return functionDeclToExprDecl(f) +} + // functionDeclToExprDecl converts a go-native function declaration into a protobuf-typed function declaration. func functionDeclToExprDecl(f *FunctionDecl) (*exprpb.Decl, error) { overloads := make([]*exprpb.Decl_FunctionDecl_Overload, len(f.overloads)) diff --git a/common/decls/decls_test.go b/common/decls/decls_test.go index 4bcf7baa..e9396600 100644 --- a/common/decls/decls_test.go +++ b/common/decls/decls_test.go @@ -984,7 +984,7 @@ func TestFunctionDeclToExprDecl(t *testing.T) { }, } for _, tst := range tests { - exDecl, err := functionDeclToExprDecl(tst.fn) + exDecl, err := FunctionDeclToExprDecl(tst.fn) if err != nil { t.Fatalf("FunctionDeclToExprDecl(%v) failed: %v", tst.fn, err) } @@ -1048,7 +1048,7 @@ func TestTypeVariable(t *testing.T) { } func TestVariableDeclToExprDecl(t *testing.T) { - a, err := variableDeclToExprDecl(NewVariable("a", types.BoolType)) + a, err := VariableDeclToExprDecl(NewVariable("a", types.BoolType)) if err != nil { t.Fatalf("VariableDeclToExprDecl() failed: %v", err) } @@ -1059,7 +1059,7 @@ func TestVariableDeclToExprDecl(t *testing.T) { } func TestVariableDeclToExprDeclInvalid(t *testing.T) { - out, err := variableDeclToExprDecl(NewVariable("bad", &types.Type{})) + out, err := VariableDeclToExprDecl(NewVariable("bad", &types.Type{})) if err == nil { t.Fatalf("VariableDeclToExprDecl() succeeded: %v, wanted error", out) }