Skip to content

Commit

Permalink
fix: wrong get/set funcs in MapNestedValueOf
Browse files Browse the repository at this point in the history
  • Loading branch information
azrod committed Feb 2, 2024
1 parent f03b82f commit d21fadc
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 34 deletions.
3 changes: 3 additions & 0 deletions .changelog/55.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
`MapNestedValueOF` - Fix wrong parameter for the funcs `Get()` and `Set()`.
```
6 changes: 6 additions & 0 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ func nestedObjectTypeNewObjectSlice[T any](_ context.Context, xlen, xcap int) ([
return make([]*T, xlen, xcap), diags
}

func nestedMapTypeNewMap[T any](_ context.Context) (map[string]*T, diag.Diagnostics) {
var diags diag.Diagnostics

return make(map[string]*T), diags
}

func nestedObjectValueSlice[T any](ctx context.Context, val valueWithElements) ([]*T, diag.Diagnostics) {
var diags diag.Diagnostics

Expand Down
2 changes: 0 additions & 2 deletions list_nested_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,12 @@ func (t ListNestedObjectTypeOf[T]) ValueFromObjectPtr(ctx context.Context, ptr a
diags.Append(diag.NewErrorDiagnostic("Invalid pointer value", fmt.Sprintf("incorrect type: want %T, got %T", (*T)(nil), ptr)))
return nil, diags
}

func (t ListNestedObjectTypeOf[T]) ValueFromObjectSlice(ctx context.Context, slice any) (attr.Value, diag.Diagnostics) {
var diags diag.Diagnostics

if v, ok := slice.([]*T); ok {
return NewListNestedObjectValueOfSlice(ctx, v), diags
}

diags.Append(diag.NewErrorDiagnostic("Invalid slice value", fmt.Sprintf("incorrect type: want %T, got %T", (*[]T)(nil), slice)))
return nil, diags
}
4 changes: 0 additions & 4 deletions list_nested_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ func (v *ListNestedObjectValueOf[T]) Set(ctx context.Context, slice []*T) diag.D
v.ListValue, diags = basetypes.NewListValueFrom(ctx, NewObjectTypeOf[T](ctx), slice)
return diags
}

func NewListNestedObjectValueOfNull[T any](ctx context.Context) ListNestedObjectValueOf[T] {
return ListNestedObjectValueOf[T]{ListValue: basetypes.NewListNull(NewObjectTypeOf[T](ctx))}
}
Expand All @@ -114,15 +113,12 @@ func NewListNestedObjectValueOfUnknown[T any](ctx context.Context) ListNestedObj
func NewListNestedObjectValueOfPtr[T any](ctx context.Context, t *T) ListNestedObjectValueOf[T] {
return NewListNestedObjectValueOfSlice(ctx, []*T{t})
}

func NewListNestedObjectValueOfSlice[T any](ctx context.Context, ts []*T) ListNestedObjectValueOf[T] {
return newListNestedObjectValueOf[T](ctx, ts)
}

func NewListNestedObjectValueOfValueSlice[T any](ctx context.Context, ts []T) ListNestedObjectValueOf[T] {
return newListNestedObjectValueOf[T](ctx, ts)
}

func newListNestedObjectValueOf[T any](ctx context.Context, elements any) ListNestedObjectValueOf[T] {
return ListNestedObjectValueOf[T]{ListValue: MustDiag(basetypes.NewListValueFrom(ctx, NewObjectTypeOf[T](ctx), elements))}
}
Expand Down
16 changes: 7 additions & 9 deletions map_nested_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ func (t MapNestedObjectTypeOf[T]) NewObjectPtr(ctx context.Context) (any, diag.D
return nestedObjectTypeNewObjectPtr[T](ctx)
}

func (t MapNestedObjectTypeOf[T]) NewObjectSlice(ctx context.Context, len, cap int) (any, diag.Diagnostics) {
return nestedObjectTypeNewObjectSlice[T](ctx, len, cap)
func (t MapNestedObjectTypeOf[T]) NewObjectSlice(ctx context.Context, _, _ int) (any, diag.Diagnostics) {
return nestedMapTypeNewMap[T](ctx)
}

func (t MapNestedObjectTypeOf[T]) NullValue(ctx context.Context) (attr.Value, diag.Diagnostics) {
Expand All @@ -204,21 +204,19 @@ func (t MapNestedObjectTypeOf[T]) NullValue(ctx context.Context) (attr.Value, di
func (t MapNestedObjectTypeOf[T]) ValueFromObjectPtr(ctx context.Context, ptr any) (attr.Value, diag.Diagnostics) {
var diags diag.Diagnostics

if v, ok := ptr.(*T); ok {
if v, ok := ptr.(map[string]*T); ok {
return NewMapNestedObjectValueOfPtr(ctx, v), diags
}

diags.Append(diag.NewErrorDiagnostic("Invalid pointer value", fmt.Sprintf("incorrect type: want %T, got %T", (*T)(nil), ptr)))
diags.Append(diag.NewErrorDiagnostic("Invalid map value", fmt.Sprintf("incorrect type: want %T, got %T", (map[string]*T)(nil), ptr)))
return nil, diags
}

func (t MapNestedObjectTypeOf[T]) ValueFromObjectSlice(ctx context.Context, slice any) (attr.Value, diag.Diagnostics) {
var diags diag.Diagnostics

if v, ok := slice.([]*T); ok {
return NewMapNestedObjectValueOfSlice(ctx, v), diags
if v, ok := slice.(map[string]*T); ok {
return NewMapNestedObjectValueOfMap(ctx, v), diags
}

diags.Append(diag.NewErrorDiagnostic("Invalid slice value", fmt.Sprintf("incorrect type: want %T, got %T", (*[]T)(nil), slice)))
diags.Append(diag.NewErrorDiagnostic("Invalid slice value", fmt.Sprintf("incorrect type: want %T, got %T", (map[string]*T)(nil), slice)))
return nil, diags
}
20 changes: 8 additions & 12 deletions map_nested_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,11 @@ func (v MapNestedObjectValueOf[T]) Get(ctx context.Context) (map[string]*T, diag
}

// Set returns a MapNestedObjectValueOf from a slice of pointers to the elements of a MapNestedObject.
func (v *MapNestedObjectValueOf[T]) Set(ctx context.Context, slice []*T) diag.Diagnostics {
func (v *MapNestedObjectValueOf[T]) Set(ctx context.Context, m map[string]*T) diag.Diagnostics {
var diags diag.Diagnostics
v.MapValue, diags = basetypes.NewMapValueFrom(ctx, NewObjectTypeOf[T](ctx), slice)
v.MapValue, diags = basetypes.NewMapValueFrom(ctx, NewObjectTypeOf[T](ctx), m)
return diags
}

func NewMapNestedObjectValueOfNull[T any](ctx context.Context) MapNestedObjectValueOf[T] {
return MapNestedObjectValueOf[T]{MapValue: basetypes.NewMapNull(NewObjectTypeOf[T](ctx))}
}
Expand All @@ -111,18 +110,15 @@ func NewMapNestedObjectValueOfUnknown[T any](ctx context.Context) MapNestedObjec
return MapNestedObjectValueOf[T]{MapValue: basetypes.NewMapUnknown(NewObjectTypeOf[T](ctx))}
}

func NewMapNestedObjectValueOfPtr[T any](ctx context.Context, t *T) MapNestedObjectValueOf[T] {
return NewMapNestedObjectValueOfSlice(ctx, []*T{t})
func NewMapNestedObjectValueOfPtr[T any](ctx context.Context, m map[string]*T) MapNestedObjectValueOf[T] {
return NewMapNestedObjectValueOfMap(ctx, m)
}

func NewMapNestedObjectValueOfSlice[T any](ctx context.Context, ts []*T) MapNestedObjectValueOf[T] {
return newMapNestedObjectValueOf[T](ctx, ts)
func NewMapNestedObjectValueOfMap[T any](ctx context.Context, m map[string]*T) MapNestedObjectValueOf[T] {
return newMapNestedObjectValueOf[T](ctx, m)
}

func NewMapNestedObjectValueOfValueSlice[T any](ctx context.Context, ts []T) MapNestedObjectValueOf[T] {
return newMapNestedObjectValueOf[T](ctx, ts)
func NewMapNestedObjectValueOfValueMap[T any](ctx context.Context, m map[string]T) MapNestedObjectValueOf[T] {
return newMapNestedObjectValueOf[T](ctx, m)
}

func newMapNestedObjectValueOf[T any](ctx context.Context, elements any) MapNestedObjectValueOf[T] {
return MapNestedObjectValueOf[T]{MapValue: MustDiag(basetypes.NewMapValueFrom(ctx, NewObjectTypeOf[T](ctx), elements))}
}
Expand Down
2 changes: 0 additions & 2 deletions set_nested_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,12 @@ func (t SetNestedObjectTypeOf[T]) ValueFromObjectPtr(ctx context.Context, ptr an
diags.Append(diag.NewErrorDiagnostic("Invalid pointer value", fmt.Sprintf("incorrect type: want %T, got %T", (*T)(nil), ptr)))
return nil, diags
}

func (t SetNestedObjectTypeOf[T]) ValueFromObjectSlice(ctx context.Context, slice any) (attr.Value, diag.Diagnostics) {
var diags diag.Diagnostics

if v, ok := slice.([]*T); ok {
return NewSetNestedObjectValueOfSlice(ctx, v), diags
}

diags.Append(diag.NewErrorDiagnostic("Invalid slice value", fmt.Sprintf("incorrect type: want %T, got %T", (*[]T)(nil), slice)))
return nil, diags
}
4 changes: 0 additions & 4 deletions set_nested_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ func (v *SetNestedObjectValueOf[T]) Set(ctx context.Context, slice []*T) diag.Di
v.SetValue, diags = basetypes.NewSetValueFrom(ctx, NewObjectTypeOf[T](ctx), slice)
return diags
}

func NewSetNestedObjectValueOfNull[T any](ctx context.Context) SetNestedObjectValueOf[T] {
return SetNestedObjectValueOf[T]{SetValue: basetypes.NewSetNull(NewObjectTypeOf[T](ctx))}
}
Expand All @@ -114,15 +113,12 @@ func NewSetNestedObjectValueOfUnknown[T any](ctx context.Context) SetNestedObjec
func NewSetNestedObjectValueOfPtr[T any](ctx context.Context, t *T) SetNestedObjectValueOf[T] {
return NewSetNestedObjectValueOfSlice(ctx, []*T{t})
}

func NewSetNestedObjectValueOfSlice[T any](ctx context.Context, ts []*T) SetNestedObjectValueOf[T] {
return newSetNestedObjectValueOf[T](ctx, ts)
}

func NewSetNestedObjectValueOfValueSlice[T any](ctx context.Context, ts []T) SetNestedObjectValueOf[T] {
return newSetNestedObjectValueOf[T](ctx, ts)
}

func newSetNestedObjectValueOf[T any](ctx context.Context, elements any) SetNestedObjectValueOf[T] {
return SetNestedObjectValueOf[T]{SetValue: MustDiag(basetypes.NewSetValueFrom(ctx, NewObjectTypeOf[T](ctx), elements))}
}
Expand Down
33 changes: 32 additions & 1 deletion templates/nested_type.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,34 @@ func (t {{ .TypeName }}NestedObjectTypeOf[T]) NewObjectPtr(ctx context.Context)
return nestedObjectTypeNewObjectPtr[T](ctx)
}

{{ if eq .TypeName "Map" -}}
func (t {{ .TypeName }}NestedObjectTypeOf[T]) NewObjectSlice(ctx context.Context, _, _ int) (any, diag.Diagnostics) {
return nestedMapTypeNewMap[T](ctx)
}
{{ else -}}
func (t {{ .TypeName }}NestedObjectTypeOf[T]) NewObjectSlice(ctx context.Context, len, cap int) (any, diag.Diagnostics) {
return nestedObjectTypeNewObjectSlice[T](ctx, len, cap)
}
{{ end -}}

func (t {{ .TypeName }}NestedObjectTypeOf[T]) NullValue(ctx context.Context) (attr.Value, diag.Diagnostics) {
var diags diag.Diagnostics

return New{{ .TypeName }}NestedObjectValueOfNull[T](ctx), diags
}

{{ if eq .TypeName "Map" -}}
func (t {{ .TypeName }}NestedObjectTypeOf[T]) ValueFromObjectPtr(ctx context.Context, ptr any) (attr.Value, diag.Diagnostics) {
var diags diag.Diagnostics

if v, ok := ptr.(map[string]*T); ok {
return New{{ .TypeName }}NestedObjectValueOfPtr(ctx, v), diags
}

diags.Append(diag.NewErrorDiagnostic("Invalid map value", fmt.Sprintf("incorrect type: want %T, got %T", (map[string]*T)(nil), ptr)))
return nil, diags
}
{{ else -}}
func (t {{ .TypeName }}NestedObjectTypeOf[T]) ValueFromObjectPtr(ctx context.Context, ptr any) (attr.Value, diag.Diagnostics) {
var diags diag.Diagnostics

Expand All @@ -210,14 +228,27 @@ func (t {{ .TypeName }}NestedObjectTypeOf[T]) ValueFromObjectPtr(ctx context.Con
diags.Append(diag.NewErrorDiagnostic("Invalid pointer value", fmt.Sprintf("incorrect type: want %T, got %T", (*T)(nil), ptr)))
return nil, diags
}
{{ end -}}

{{ if eq .TypeName "Map" -}}
func (t {{ .TypeName }}NestedObjectTypeOf[T]) ValueFromObjectSlice(ctx context.Context, slice any) (attr.Value, diag.Diagnostics) {
{{ else -}}
func (t {{ .TypeName }}NestedObjectTypeOf[T]) ValueFromObjectSlice(ctx context.Context, slice any) (attr.Value, diag.Diagnostics) {
{{ end -}}

var diags diag.Diagnostics

{{ if eq .TypeName "Map" -}}
if v, ok := slice.(map[string]*T); ok {
return New{{ .TypeName }}NestedObjectValueOfMap(ctx, v), diags
}
diags.Append(diag.NewErrorDiagnostic("Invalid slice value", fmt.Sprintf("incorrect type: want %T, got %T", (map[string]*T)(nil), slice)))
return nil, diags
{{ else -}}
if v, ok := slice.([]*T); ok {
return New{{ .TypeName }}NestedObjectValueOfSlice(ctx, v), diags
}

diags.Append(diag.NewErrorDiagnostic("Invalid slice value", fmt.Sprintf("incorrect type: want %T, got %T", (*[]T)(nil), slice)))
return nil, diags
{{ end -}}
}
26 changes: 26 additions & 0 deletions templates/nested_value.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,19 @@ func (v {{ .TypeName }}NestedObjectValueOf[T]) Get(ctx context.Context) ([]*T, d
{{ end -}}

// Set returns a {{ .TypeName }}NestedObjectValueOf from a slice of pointers to the elements of a {{ .TypeName }}NestedObject.
{{ if eq .TypeName "Map" -}}
func (v *{{ .TypeName }}NestedObjectValueOf[T]) Set(ctx context.Context, m map[string]*T) diag.Diagnostics {
var diags diag.Diagnostics
v.{{ .TypeName }}Value, diags = basetypes.New{{ .TypeName }}ValueFrom(ctx, NewObjectTypeOf[T](ctx), m)
return diags
}
{{ else -}}
func (v *{{ .TypeName }}NestedObjectValueOf[T]) Set(ctx context.Context, slice []*T) diag.Diagnostics {
var diags diag.Diagnostics
v.{{ .TypeName }}Value, diags = basetypes.New{{ .TypeName }}ValueFrom(ctx, NewObjectTypeOf[T](ctx), slice)
return diags
}
{{ end -}}

func New{{ .TypeName }}NestedObjectValueOfNull[T any](ctx context.Context) {{ .TypeName }}NestedObjectValueOf[T] {
return {{ .TypeName }}NestedObjectValueOf[T]{ {{ .TypeName }}Value: basetypes.New{{ .TypeName }}Null(NewObjectTypeOf[T](ctx))}
Expand All @@ -116,17 +124,35 @@ func New{{ .TypeName }}NestedObjectValueOfUnknown[T any](ctx context.Context) {{
return {{ .TypeName }}NestedObjectValueOf[T]{ {{ .TypeName }}Value: basetypes.New{{ .TypeName }}Unknown(NewObjectTypeOf[T](ctx))}
}

{{ if eq .TypeName "Map" -}}
func New{{ .TypeName }}NestedObjectValueOfPtr[T any](ctx context.Context, m map[string]*T) {{ .TypeName }}NestedObjectValueOf[T] {
return New{{ .TypeName }}NestedObjectValueOfMap(ctx, m)
}
{{ else -}}
func New{{ .TypeName }}NestedObjectValueOfPtr[T any](ctx context.Context, t *T) {{ .TypeName }}NestedObjectValueOf[T] {
return New{{ .TypeName }}NestedObjectValueOfSlice(ctx, []*T{t})
}
{{ end -}}

{{ if eq .TypeName "Map" -}}
func New{{ .TypeName }}NestedObjectValueOfMap[T any](ctx context.Context, m map[string]*T) {{ .TypeName }}NestedObjectValueOf[T] {
return new{{ .TypeName }}NestedObjectValueOf[T](ctx, m)
}
{{ else -}}
func New{{ .TypeName }}NestedObjectValueOfSlice[T any](ctx context.Context, ts []*T) {{ .TypeName }}NestedObjectValueOf[T] {
return new{{ .TypeName }}NestedObjectValueOf[T](ctx, ts)
}
{{ end -}}

{{ if eq .TypeName "Map" -}}
func New{{ .TypeName }}NestedObjectValueOfValueMap[T any](ctx context.Context, m map[string]T) {{ .TypeName }}NestedObjectValueOf[T] {
return new{{ .TypeName }}NestedObjectValueOf[T](ctx, m)
}
{{ else -}}
func New{{ .TypeName }}NestedObjectValueOfValueSlice[T any](ctx context.Context, ts []T) {{ .TypeName }}NestedObjectValueOf[T] {
return new{{ .TypeName }}NestedObjectValueOf[T](ctx, ts)
}
{{ end -}}

func new{{ .TypeName }}NestedObjectValueOf[T any](ctx context.Context, elements any) {{ .TypeName }}NestedObjectValueOf[T] {
return {{ .TypeName }}NestedObjectValueOf[T]{ {{ .TypeName }}Value: MustDiag(basetypes.New{{ .TypeName }}ValueFrom(ctx, NewObjectTypeOf[T](ctx), elements))}
Expand Down

0 comments on commit d21fadc

Please sign in to comment.