zeroshade commented on code in PR #10:
URL: https://github.com/apache/iceberg-terraform/pull/10#discussion_r2843095982


##########
internal/provider/resource_table.go:
##########
@@ -308,6 +192,30 @@ func (r *icebergTableResource) Schema(_ context.Context, _ 
resource.SchemaReques
        }
 }
 
+func copyAttributes(attrs map[string]rscschema.Attribute) 
map[string]rscschema.Attribute {
+       newAttrs := make(map[string]rscschema.Attribute, len(attrs))
+       for k, v := range attrs {
+               newAttrs[k] = v
+       }
+       return newAttrs
+}

Review Comment:
   `maps.Copy`?



##########
internal/provider/table_schema.go:
##########
@@ -43,86 +39,159 @@ func (icebergTableSchema) AttrTypes() map[string]attr.Type 
{
        }
 }
 
+func (s icebergTableSchema) MarshalJSON() ([]byte, error) {
+       type Alias struct {
+               ID     int64                     `json:"schema-id"`
+               Fields []icebergTableSchemaField `json:"fields"`
+       }
+       var id int64
+       if !s.ID.IsNull() && !s.ID.IsUnknown() {
+               id = s.ID.ValueInt64()
+       }
+       return json.Marshal(&struct {
+               Type string `json:"type"`
+               Alias
+       }{
+               Type: "struct",
+               Alias: Alias{
+                       ID:     id,
+                       Fields: s.Fields,
+               },
+       })
+}
+
+func (s *icebergTableSchema) UnmarshalJSON(b []byte) error {
+       var raw struct {
+               ID     int64                     `json:"schema-id"`
+               Fields []icebergTableSchemaField `json:"fields"`
+       }
+       if err := json.Unmarshal(b, &raw); err != nil {
+               return err
+       }
+       s.ID = types.Int64Value(raw.ID)
+       s.Fields = raw.Fields
+       return nil
+}
+
+func (s *icebergTableSchema) ToIceberg() (*iceberg.Schema, error) {
+       b, err := json.Marshal(s)
+       if err != nil {
+               return nil, err
+       }
+       var icebergSchema iceberg.Schema
+       if err := json.Unmarshal(b, &icebergSchema); err != nil {
+               return nil, err
+       }
+       return &icebergSchema, nil
+}
+
+func (s *icebergTableSchema) FromIceberg(icebergSchema *iceberg.Schema) error {
+       b, err := json.Marshal(icebergSchema)
+       if err != nil {
+               return err
+       }
+       return json.Unmarshal(b, s)
+}
+
 type icebergTableSchemaField struct {
-       ID                types.Int64  `tfsdk:"id"`
-       Name              types.String `tfsdk:"name"`
-       Type              types.String `tfsdk:"type"`
-       Required          types.Bool   `tfsdk:"required"`
-       Doc               types.String `tfsdk:"doc"`
-       DecimalProperties types.Object `tfsdk:"decimal_properties"`
-       FixedProperties   types.Object `tfsdk:"fixed_properties"`
-       ListProperties    types.Object `tfsdk:"list_properties"`
-       MapProperties     types.Object `tfsdk:"map_properties"`
-       StructProperties  types.Object `tfsdk:"struct_properties"`
+       ID               types.Int64                              `tfsdk:"id" 
json:"id"`
+       Name             string                                   `tfsdk:"name" 
json:"name"`
+       Type             string                                   `tfsdk:"type" 
json:"-"`
+       Required         bool                                     
`tfsdk:"required" json:"required"`
+       Doc              *string                                  `tfsdk:"doc" 
json:"doc,omitempty"`
+       ListProperties   *icebergTableSchemaFieldListProperties   
`tfsdk:"list_properties" json:"-"`
+       MapProperties    *icebergTableSchemaFieldMapProperties    
`tfsdk:"map_properties" json:"-"`
+       StructProperties *icebergTableSchemaFieldStructProperties 
`tfsdk:"struct_properties" json:"-"`
 }
 
 func (icebergTableSchemaField) AttrTypes() map[string]attr.Type {
        return map[string]attr.Type{
-               "id":                 types.Int64Type,
-               "name":               types.StringType,
-               "type":               types.StringType,
-               "required":           types.BoolType,
-               "doc":                types.StringType,
-               "decimal_properties": types.ObjectType{AttrTypes: 
icebergTableSchemaFieldDecimalProperties{}.AttrTypes()},
-               "fixed_properties":   types.ObjectType{AttrTypes: 
icebergTableSchemaFieldFixedProperties{}.AttrTypes()},
-               "list_properties":    types.ObjectType{AttrTypes: 
icebergTableSchemaFieldListProperties{}.AttrTypes()},
-               "map_properties":     types.ObjectType{AttrTypes: 
icebergTableSchemaFieldMapProperties{}.AttrTypes()},
-               "struct_properties":  types.ObjectType{AttrTypes: 
icebergTableSchemaFieldStructProperties{}.AttrTypes()},
+               "id":                types.Int64Type,
+               "name":              types.StringType,
+               "type":              types.StringType,
+               "required":          types.BoolType,
+               "doc":               types.StringType,
+               "list_properties":   types.ObjectType{AttrTypes: 
icebergTableSchemaFieldListProperties{}.AttrTypes()},
+               "map_properties":    types.ObjectType{AttrTypes: 
icebergTableSchemaFieldMapProperties{}.AttrTypes()},
+               "struct_properties": types.ObjectType{AttrTypes: 
icebergTableSchemaFieldStructProperties{}.AttrTypes()},
        }
 }
 
+func (f icebergTableSchemaField) MarshalJSON() ([]byte, error) {
+       return marshalFieldJSON(f.ID, f.Name, f.Type, f.Required, f.Doc, 
f.ListProperties, f.MapProperties, f.StructProperties)
+}
+
+func (f *icebergTableSchemaField) UnmarshalJSON(b []byte) error {
+       return unmarshalFieldJSON(b, &f.ID, &f.Name, &f.Type, &f.Required, 
&f.Doc, &f.ListProperties, &f.MapProperties, &f.StructProperties)
+}
+
 type icebergTableSchemaInnerField struct {

Review Comment:
   do we actually need this `InnerField` / `LeafField` pattern anymore? Can't 
we just use `SchemaField` which may or may not have children?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to