From d21fadc9eb0d58212664cc134eb9188a07013832 Mon Sep 17 00:00:00 2001 From: Mickael Stanislas Date: Fri, 2 Feb 2024 17:01:31 +0100 Subject: [PATCH] fix: wrong get/set funcs in MapNestedValueOf --- .changelog/55.txt | 3 +++ base.go | 6 ++++++ list_nested_type.go | 2 -- list_nested_value.go | 4 ---- map_nested_type.go | 16 +++++++--------- map_nested_value.go | 20 ++++++++------------ set_nested_type.go | 2 -- set_nested_value.go | 4 ---- templates/nested_type.go.tmpl | 33 ++++++++++++++++++++++++++++++++- templates/nested_value.go.tmpl | 26 ++++++++++++++++++++++++++ 10 files changed, 82 insertions(+), 34 deletions(-) create mode 100644 .changelog/55.txt diff --git a/.changelog/55.txt b/.changelog/55.txt new file mode 100644 index 0000000..563fa4c --- /dev/null +++ b/.changelog/55.txt @@ -0,0 +1,3 @@ +```release-note:bug +`MapNestedValueOF` - Fix wrong parameter for the funcs `Get()` and `Set()`. +``` \ No newline at end of file diff --git a/base.go b/base.go index 95be597..b2e215e 100644 --- a/base.go +++ b/base.go @@ -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 diff --git a/list_nested_type.go b/list_nested_type.go index 9ab52a4..6c3c2b6 100644 --- a/list_nested_type.go +++ b/list_nested_type.go @@ -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 } diff --git a/list_nested_value.go b/list_nested_value.go index ccd0579..7784b28 100644 --- a/list_nested_value.go +++ b/list_nested_value.go @@ -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))} } @@ -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))} } diff --git a/map_nested_type.go b/map_nested_type.go index 32bd2a4..cf126e7 100644 --- a/map_nested_type.go +++ b/map_nested_type.go @@ -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) { @@ -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 } diff --git a/map_nested_value.go b/map_nested_value.go index 2d971c9..4d457a0 100644 --- a/map_nested_value.go +++ b/map_nested_value.go @@ -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))} } @@ -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))} } diff --git a/set_nested_type.go b/set_nested_type.go index c0327bd..9b591e5 100644 --- a/set_nested_type.go +++ b/set_nested_type.go @@ -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 } diff --git a/set_nested_value.go b/set_nested_value.go index 4766ffe..f7a4941 100644 --- a/set_nested_value.go +++ b/set_nested_value.go @@ -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))} } @@ -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))} } diff --git a/templates/nested_type.go.tmpl b/templates/nested_type.go.tmpl index 864a452..51f6b1c 100644 --- a/templates/nested_type.go.tmpl +++ b/templates/nested_type.go.tmpl @@ -190,9 +190,15 @@ 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 @@ -200,6 +206,18 @@ func (t {{ .TypeName }}NestedObjectTypeOf[T]) NullValue(ctx context.Context) (at 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 @@ -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 -}} } \ No newline at end of file diff --git a/templates/nested_value.go.tmpl b/templates/nested_value.go.tmpl index 023b4f9..6ad9b37 100644 --- a/templates/nested_value.go.tmpl +++ b/templates/nested_value.go.tmpl @@ -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))} @@ -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))}