Skip to content

Commit

Permalink
Expose public methods to convert function and variable decl to v1 Decl (
Browse files Browse the repository at this point in the history
  • Loading branch information
aakash070 authored Nov 18, 2024
1 parent f8ecaa2 commit da44524
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 10 additions & 0 deletions common/decls/decls.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions common/decls/decls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit da44524

Please sign in to comment.