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

feat: Support uncommon profile structures #124

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions pkg/types/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
v2 "github.com/conductorone/baton-sdk/pb/c1/connector/v2"
"github.com/conductorone/baton-sdk/pkg/annotations"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/structpb"
)

type ResourceOption func(*v2.Resource) error
Expand Down Expand Up @@ -337,3 +338,20 @@ func NewSecretResource(

return ret, nil
}

func ToProtoValue(val interface{}) (*structpb.Value, error) {
switch v := val.(type) {
case string, bool, float64:
return structpb.NewValue(val)
case int:
return structpb.NewNumberValue(float64(v)), nil
case *structpb.Value:
return v, nil
case *structpb.Struct:
return structpb.NewStructValue(v), nil
case *structpb.ListValue:
return structpb.NewListValue(v), nil
default:
return nil, fmt.Errorf("type %T is not supported", val)
}
}
12 changes: 9 additions & 3 deletions pkg/types/resource/role_trait.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ type RoleTraitOption func(gt *v2.RoleTrait) error

func WithRoleProfile(profile map[string]interface{}) RoleTraitOption {
return func(rt *v2.RoleTrait) error {
p, err := structpb.NewStruct(profile)
if err != nil {
return err
p := &structpb.Struct{Fields: make(map[string]*structpb.Value, len(profile))}

for key, val := range profile {
pv, err := ToProtoValue(val)
if err != nil {
return fmt.Errorf("error converting profile data: %w", err)
}

p.Fields[key] = pv
}

rt.Profile = p
Expand Down
57 changes: 57 additions & 0 deletions pkg/types/resource/traits.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,60 @@ func GetProfileInt64Value(profile *structpb.Struct, k string) (int64, bool) {

return int64(s.NumberValue), true
}

// GetProfileBoolValue returns a bool and true if the value is found.
func GetProfileBoolValue(profile *structpb.Struct, k string) (bool, bool) {
if profile == nil {
return false, false
}

v, ok := profile.Fields[k]
if !ok {
return false, false
}

s, ok := v.Kind.(*structpb.Value_BoolValue)
if !ok {
return false, false
}

return s.BoolValue, true
}

// GetProfileListValue returns a list of pointers to structpb.Value and true if the value is found.
func GetProfileListValue(profile *structpb.Struct, k string) ([]*structpb.Value, bool) {
if profile == nil {
return nil, false
}

v, ok := profile.Fields[k]
if !ok {
return nil, false
}

s, ok := v.Kind.(*structpb.Value_ListValue)
if !ok {
return nil, false
}

return s.ListValue.Values, true
}

// GetProfileStructValue returns a pointer to structpb.Struct and true if the value is found.
func GetProfileStructValue(profile *structpb.Struct, k string) (*structpb.Struct, bool) {
if profile == nil {
return nil, false
}

v, ok := profile.Fields[k]
if !ok {
return nil, false
}

s, ok := v.Kind.(*structpb.Value_StructValue)
if !ok {
return nil, false
}

return s.StructValue, true
}
Loading