Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Style refactor: use contract.Assertf instead of panic #1527

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
30 changes: 12 additions & 18 deletions pf/internal/schemashim/block_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
package schemashim

import (
"fmt"

bridge "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"

"github.com/pulumi/pulumi-terraform-bridge/pf/internal/pfutils"
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
)

type blockSchema struct {
Expand All @@ -40,9 +39,7 @@ var _ shim.Schema = (*blockSchema)(nil)
func (s *blockSchema) Type() shim.ValueType {
ty := s.block.Type()
vt, err := convertType(ty)
if err != nil {
panic(err)
}
contract.AssertNoErrorf(err, "blockSchema.Type(): unexpected error from convertType()")
return vt
}

Expand Down Expand Up @@ -76,30 +73,27 @@ func (s *blockSchema) Elem() interface{} {
if _, ok := s.block.Type().(basetypes.ListTypable); ok {
if twet, ok := s.block.Type().(attr.TypeWithElementType); ok {
r, ok := asObjectType(twet.ElementType())
if !ok {
panic(fmt.Errorf("List-nested block expect an ObjectTypeable "+
"block.Type().ElemType, but got %v", s.block.Type()))
}
contract.Assertf(ok, "List-nested block expect an ObjectTypeable "+
"block.Type().ElemType, but got %v", s.block.Type())
return r
}
panic(fmt.Errorf("List-nested block has a ListTypeable type that does not implement "+
"TypeWithElementType: %v", s.block.Type()))
contract.Assertf(false, "List-nested block has a ListTypeable type that does not implement "+
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we have contract.Failef

Suggested change
contract.Assertf(false, "List-nested block has a ListTypeable type that does not implement "+
contract.Failf("List-nested block has a ListTypeable type that does not implement "+

"TypeWithElementType: %v", s.block.Type())
}

if _, ok := s.block.Type().(basetypes.SetTypable); ok {
if twet, ok := s.block.Type().(attr.TypeWithElementType); ok {
r, ok := asObjectType(twet.ElementType())
if !ok {
panic(fmt.Errorf("Set-nested block expect an ObjectTypeable "+
"block.Type().ElemType, but got %v", twet.ElementType()))
}
contract.Assertf(ok, "Set-nested block expect an ObjectTypeable "+
"block.Type().ElemType, but got %v", twet.ElementType())
return r
}
panic(fmt.Errorf("List-nested block has a SetTypeable type that does not implement "+
"TypeWithElementType: %v", s.block.Type()))
contract.Assertf(false, "List-nested block has a SetTypeable type that does not implement "+
"TypeWithElementType: %v", s.block.Type())
}

panic(fmt.Errorf("block.Type()==%v is not supported for blocks", t))
contract.Assertf(false, "block.Type()==%v is not supported for blocks", t)
return nil
}

func (s *blockSchema) Optional() bool {
Expand Down
Loading