Skip to content

Commit 25cf0d7

Browse files
committed
Fix CSV/TSV tables.
1 parent 913238f commit 25cf0d7

File tree

4 files changed

+51
-59
lines changed

4 files changed

+51
-59
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ HOSTNAME=registry.terraform.io
33
NAMESPACE=snellerinc
44
NAME=sneller
55
BINARY=terraform-provider-${NAME}
6-
VERSION=0.0.3
6+
VERSION=0.0.6
77
OS_ARCH=linux_amd64
88

99
default: install

sneller/datasource/table.go

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -158,31 +158,27 @@ func (d *tableDataSource) Schema(ctx context.Context, req datasource.SchemaReque
158158
},
159159
},
160160
},
161-
"csv_hints": schema.MapNestedAttribute{
161+
"csv_hints": schema.SingleNestedAttribute{
162162
Description: "Ingestion hints for CSV input.",
163163
Computed: true,
164-
NestedObject: schema.NestedAttributeObject{
165-
Attributes: map[string]schema.Attribute{
166-
"separator": schema.StringAttribute{
167-
Description: "specify a custom separator (defaults to ',').",
168-
MarkdownDescription: "specify a custom separator (defaults to `,`).",
169-
Computed: true,
170-
},
171-
"skip_records": skipRecords,
172-
"missing_values": missingValues,
173-
"fields": fields,
164+
Attributes: map[string]schema.Attribute{
165+
"separator": schema.StringAttribute{
166+
Description: "specify a custom separator (defaults to ',').",
167+
MarkdownDescription: "specify a custom separator (defaults to `,`).",
168+
Computed: true,
174169
},
170+
"skip_records": skipRecords,
171+
"missing_values": missingValues,
172+
"fields": fields,
175173
},
176174
},
177-
"tsv_hints": schema.MapNestedAttribute{
175+
"tsv_hints": schema.SingleNestedAttribute{
178176
Description: "Ingestion hints for TSV input.",
179177
Computed: true,
180-
NestedObject: schema.NestedAttributeObject{
181-
Attributes: map[string]schema.Attribute{
182-
"skip_records": skipRecords,
183-
"missing_values": missingValues,
184-
"fields": fields,
185-
},
178+
Attributes: map[string]schema.Attribute{
179+
"skip_records": skipRecords,
180+
"missing_values": missingValues,
181+
"fields": fields,
186182
},
187183
},
188184
},

sneller/model/table.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ type TableInputModel struct {
1818
Pattern string `tfsdk:"pattern"`
1919
Format *string `tfsdk:"format"`
2020
JSONHints []TableInputJSONHintModel `tfsdk:"json_hints"`
21-
CSVHints []TableInputCSVHintModel `tfsdk:"csv_hints"`
22-
TSVHints []TableInputTSVHintModel `tfsdk:"tsv_hints"`
21+
CSVHints *TableInputCSVHintModel `tfsdk:"csv_hints"`
22+
TSVHints *TableInputTSVHintModel `tfsdk:"tsv_hints"`
2323
}
2424

2525
func (m *TableInputModel) MarshalJSON() ([]byte, error) {
@@ -46,15 +46,15 @@ func (m *TableInputModel) MarshalJSON() ([]byte, error) {
4646
}
4747
sb.Write(hintsValue)
4848
}
49-
if len(m.CSVHints) > 0 {
49+
if m.CSVHints != nil {
5050
sb.Write([]byte(`,"hints":`))
5151
hintsValue, err := json.Marshal(m.CSVHints)
5252
if err != nil {
5353
return nil, err
5454
}
5555
sb.Write(hintsValue)
5656
}
57-
if len(m.TSVHints) > 0 {
57+
if m.TSVHints != nil {
5858
sb.Write([]byte(`,"hints":`))
5959
hintsValue, err := json.Marshal(m.TSVHints)
6060
if err != nil {
@@ -102,26 +102,26 @@ func (m *TableInputModel) UnmarshalJSON(data []byte) error {
102102

103103
case "csv", "csv.gz", "csv.zst":
104104
var shadow struct {
105-
Hints *[]TableInputCSVHintModel `json:"hints,omitempty"`
105+
Hints *TableInputCSVHintModel `json:"hints,omitempty"`
106106
}
107107
err := json.Unmarshal(data, &shadow)
108108
if err != nil {
109109
return err
110110
}
111111
if shadow.Hints != nil {
112-
m.CSVHints = *shadow.Hints
112+
m.CSVHints = shadow.Hints
113113
}
114114

115115
case "tsv", "tsv.gz", "tsv.zst":
116116
var shadow struct {
117-
Hints *[]TableInputTSVHintModel `json:"hints,omitempty"`
117+
Hints *TableInputTSVHintModel `json:"hints,omitempty"`
118118
}
119119
err := json.Unmarshal(data, &shadow)
120120
if err != nil {
121121
return err
122122
}
123123
if shadow.Hints != nil {
124-
m.TSVHints = *shadow.Hints
124+
m.TSVHints = shadow.Hints
125125
}
126126
}
127127

@@ -345,16 +345,16 @@ type TableInputJSONHintModel struct {
345345
Hints Hints `tfsdk:"hints" json:"hints"`
346346
}
347347

348-
type TableInputCSVHintModel []struct {
348+
type TableInputCSVHintModel struct {
349349
Separator *string `tfsdk:"separator" json:"separator,omitempty"`
350-
SkipRecords *int64 `tfsdk:"skip_records" json:"skipRecords,omitempty"`
351-
MissingValues []string `tfsdk:"missing_values" json:"missingValues,omitempty"`
350+
SkipRecords *int64 `tfsdk:"skip_records" json:"skip_records,omitempty"`
351+
MissingValues []string `tfsdk:"missing_values" json:"missing_values,omitempty"`
352352
Fields []TableInputXSVHintsFieldModel `tfsdk:"fields" json:"fields,omitempty"`
353353
}
354354

355-
type TableInputTSVHintModel []struct {
356-
SkipRecords *int64 `tfsdk:"skip_records" json:"skipRecords,omitempty"`
357-
MissingValues []string `tfsdk:"missing_values" json:"missingValues,omitempty"`
355+
type TableInputTSVHintModel struct {
356+
SkipRecords *int64 `tfsdk:"skip_records" json:"skip_records,omitempty"`
357+
MissingValues []string `tfsdk:"missing_values" json:"missing_values,omitempty"`
358358
Fields []TableInputXSVHintsFieldModel `tfsdk:"fields" json:"fields,omitempty"`
359359
}
360360

@@ -365,9 +365,9 @@ type TableInputXSVHintsFieldModel struct {
365365
Format *string `tfsdk:"format" json:"format,omitempty"`
366366
AllowEmpty *bool `tfsdk:"allow_empty" json:"allowEmpty,omitempty"`
367367
NoIndex *bool `tfsdk:"no_index" json:"noIndex,omitempty"`
368-
TrueValues []string `tfsdk:"true_values" json:"trueValues"`
369-
FalseValues []string `tfsdk:"false_values" json:"falseValues"`
370-
MissingValues []string `tfsdk:"missing_values" json:"missingValues"`
368+
TrueValues []string `tfsdk:"true_values" json:"trueValues,omitempty"`
369+
FalseValues []string `tfsdk:"false_values" json:"falseValues,omitempty"`
370+
MissingValues []string `tfsdk:"missing_values" json:"missingValues,omitempty"`
371371
}
372372

373373
type TablePartitionModel struct {

sneller/resource/table.go

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -184,35 +184,31 @@ func (r *tableResource) Schema(ctx context.Context, req resource.SchemaRequest,
184184
},
185185
Validators: []validator.List{tableSupportedFormatsValidator{filterFormats("json")}},
186186
},
187-
"csv_hints": schema.MapNestedAttribute{
187+
"csv_hints": schema.SingleNestedAttribute{
188188
Description: "Ingestion hints for CSV input.",
189189
Optional: true,
190-
NestedObject: schema.NestedAttributeObject{
191-
Attributes: map[string]schema.Attribute{
192-
"separator": schema.StringAttribute{
193-
Description: "specify a custom separator (defaults to ',').",
194-
MarkdownDescription: "specify a custom separator (defaults to `,`).",
195-
Optional: true,
196-
Validators: []validator.String{stringvalidator.LengthBetween(1, 1)},
197-
},
198-
"skip_records": skipRecords,
199-
"missing_values": missingValues,
200-
"fields": fields,
190+
Attributes: map[string]schema.Attribute{
191+
"separator": schema.StringAttribute{
192+
Description: "specify a custom separator (defaults to ',').",
193+
MarkdownDescription: "specify a custom separator (defaults to `,`).",
194+
Optional: true,
195+
Validators: []validator.String{stringvalidator.LengthBetween(1, 1)},
201196
},
197+
"skip_records": skipRecords,
198+
"missing_values": missingValues,
199+
"fields": fields,
202200
},
203-
Validators: []validator.Map{tableSupportedFormatsValidator{filterFormats("csv")}},
201+
Validators: []validator.Object{tableSupportedFormatsValidator{filterFormats("csv")}},
204202
},
205-
"tsv_hints": schema.MapNestedAttribute{
203+
"tsv_hints": schema.SingleNestedAttribute{
206204
Description: "Ingestion hints for TSV input.",
207205
Optional: true,
208-
NestedObject: schema.NestedAttributeObject{
209-
Attributes: map[string]schema.Attribute{
210-
"skip_records": skipRecords,
211-
"missing_values": missingValues,
212-
"fields": fields,
213-
},
206+
Attributes: map[string]schema.Attribute{
207+
"skip_records": skipRecords,
208+
"missing_values": missingValues,
209+
"fields": fields,
214210
},
215-
Validators: []validator.Map{tableSupportedFormatsValidator{filterFormats("tsv")}},
211+
Validators: []validator.Object{tableSupportedFormatsValidator{filterFormats("tsv")}},
216212
},
217213
},
218214
},
@@ -520,7 +516,7 @@ func (r *tableResource) writeTable(ctx context.Context, data tableResourceModel,
520516
}
521517

522518
var _ validator.List = &tableSupportedFormatsValidator{}
523-
var _ validator.Map = &tableSupportedFormatsValidator{}
519+
var _ validator.Object = &tableSupportedFormatsValidator{}
524520

525521
type tableSupportedFormatsValidator struct {
526522
formats []string
@@ -543,7 +539,7 @@ func (v tableSupportedFormatsValidator) ValidateList(ctx context.Context, req va
543539
resp.Diagnostics.Append(diags...)
544540
}
545541

546-
func (v tableSupportedFormatsValidator) ValidateMap(ctx context.Context, req validator.MapRequest, resp *validator.MapResponse) {
542+
func (v tableSupportedFormatsValidator) ValidateObject(ctx context.Context, req validator.ObjectRequest, resp *validator.ObjectResponse) {
547543
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
548544
return
549545
}

0 commit comments

Comments
 (0)