Skip to content

Commit

Permalink
feat: Added support for object type
Browse files Browse the repository at this point in the history
  • Loading branch information
adanylenko committed Mar 19, 2024
1 parent 037b884 commit 4f6294a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/resources/collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ resource "typesense_collection" "my_collection" {

### Optional

- `enable_nested_fields` (Boolean) Enable nested fields, must be enabled to use object/object[] types
- `fields` (Block List) (see [below for nested schema](#nestedblock--fields))

### Read-Only
Expand Down
2 changes: 1 addition & 1 deletion examples/provider-install-verification/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ resource "typesense_collection" "test_collection" {
index = true
name = "test_field_2_updated"
optional = true
type = "string"
type = "object"
}

}
10 changes: 10 additions & 0 deletions internal/provider/resource_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type CollectionResourceModel struct {
Name types.String `tfsdk:"name"`
DefaultSortingField types.String `tfsdk:"default_sorting_field"`
Fields []CollectionResourceFieldModel `tfsdk:"fields"`
EnableNestedFields types.Bool `tfsdk:"enable_nested_fields"`
}

type CollectionResourceFieldModel struct {
Expand Down Expand Up @@ -76,6 +77,12 @@ func (r *CollectionResource) Schema(ctx context.Context, req resource.SchemaRequ
stringplanmodifier.RequiresReplace(),
},
},
"enable_nested_fields": schema.BoolAttribute{
Optional: true,
Computed: true,
MarkdownDescription: "Enable nested fields, must be enabled to use object/object[] types",
Default: booldefault.StaticBool(false),
},
},
Blocks: map[string]schema.Block{
"fields": schema.ListNestedBlock{
Expand Down Expand Up @@ -163,6 +170,7 @@ func (r *CollectionResource) Create(ctx context.Context, req resource.CreateRequ
schema := &api.CollectionSchema{}
schema.Name = data.Name.ValueString()
schema.DefaultSortingField = data.DefaultSortingField.ValueStringPointer()
schema.EnableNestedFields = data.EnableNestedFields.ValueBoolPointer()

fields := []api.Field{}

Expand All @@ -181,6 +189,7 @@ func (r *CollectionResource) Create(ctx context.Context, req resource.CreateRequ
data.Id = types.StringValue(collection.Name)
data.Name = types.StringValue(collection.Name)
data.DefaultSortingField = types.StringPointerValue(collection.DefaultSortingField)
data.EnableNestedFields = types.BoolPointerValue(collection.EnableNestedFields)
data.Fields = flattenCollectionFields(collection.Fields)

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
Expand Down Expand Up @@ -211,6 +220,7 @@ func (r *CollectionResource) Read(ctx context.Context, req resource.ReadRequest,
data.Id = types.StringValue(collection.Name)
data.Name = types.StringValue(collection.Name)
data.DefaultSortingField = types.StringPointerValue(collection.DefaultSortingField)
data.EnableNestedFields = types.BoolPointerValue(collection.EnableNestedFields)
data.Fields = flattenCollectionFields(collection.Fields)

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
Expand Down

0 comments on commit 4f6294a

Please sign in to comment.