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


##########
internal/provider/table_schema.go:
##########
@@ -0,0 +1,404 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package provider
+
+import (
+       "context"
+       "errors"
+
+       "github.com/apache/iceberg-go"
+       "github.com/hashicorp/terraform-plugin-framework/attr"
+       "github.com/hashicorp/terraform-plugin-framework/types"
+       "github.com/hashicorp/terraform-plugin-framework/types/basetypes"
+)
+
+type icebergTableSchema struct {
+       ID     types.Int64    `tfsdk:"id"`
+       Fields []types.Object `tfsdk:"fields"`
+}
+
+func (icebergTableSchema) AttrTypes() map[string]attr.Type {
+       return map[string]attr.Type{
+               "id": types.Int64Type,
+               "fields": types.ListType{
+                       ElemType: types.ObjectType{
+                               AttrTypes: 
icebergTableSchemaField{}.AttrTypes(),
+                       },
+               },
+       }
+}
+
+type icebergTableSchemaField struct {
+       ID       types.Int64                 `tfsdk:"id"`
+       Name     types.String                `tfsdk:"name"`
+       Type     icebergTableSchemaFieldType `tfsdk:"type"`
+       Required types.Bool                  `tfsdk:"required"`
+       Doc      types.String                `tfsdk:"doc"`
+}
+
+func (icebergTableSchemaField) AttrTypes() map[string]attr.Type {
+       return map[string]attr.Type{
+               "id":       types.Int64Type,
+               "name":     types.StringType,
+               "type":     types.ObjectType{AttrTypes: 
icebergTableSchemaFieldType{}.AttrTypes()},
+               "required": types.BoolType,
+               "doc":      types.StringType,
+       }
+}
+
+type icebergTableSchemaFieldType struct {
+       Primitive types.String `tfsdk:"primitive"`
+       List      types.Object `tfsdk:"list"`
+       Map       types.Object `tfsdk:"map"`
+}
+
+func (icebergTableSchemaFieldType) AttrTypes() map[string]attr.Type {
+       return map[string]attr.Type{
+               "primitive": types.StringType,
+               "list":      types.ObjectType{AttrTypes: 
icebergTableSchemaFieldTypeList{}.AttrTypes()},
+               "map":       types.ObjectType{AttrTypes: 
icebergTableSchemaFieldTypeMap{}.AttrTypes()},
+       }
+}
+
+type icebergTableSchemaFieldTypeList struct {
+       ElementID       types.Int64  `tfsdk:"element_id"`
+       ElementType     types.String `tfsdk:"element_type"`
+       ElementRequired types.Bool   `tfsdk:"element_required"`
+}
+
+func (icebergTableSchemaFieldTypeList) AttrTypes() map[string]attr.Type {
+       return map[string]attr.Type{
+               "element_id":       types.Int64Type,
+               "element_type":     types.StringType,
+               "element_required": types.BoolType,
+       }
+}
+
+type icebergTableSchemaFieldTypeMap struct {
+       KeyID         types.Int64  `tfsdk:"key_id"`
+       KeyType       types.String `tfsdk:"key_type"`
+       ValueID       types.Int64  `tfsdk:"value_id"`
+       ValueType     types.String `tfsdk:"value_type"`
+       ValueRequired types.Bool   `tfsdk:"value_required"`
+}
+
+func (icebergTableSchemaFieldTypeMap) AttrTypes() map[string]attr.Type {
+       return map[string]attr.Type{
+               "key_id":         types.Int64Type,
+               "key_type":       types.StringType,
+               "value_id":       types.Int64Type,
+               "value_type":     types.StringType,
+               "value_required": types.BoolType,
+       }
+}
+
+func terraformTypeToIcebergType(typ icebergTableSchemaFieldType) 
(iceberg.Type, error) {
+       if !typ.Primitive.IsNull() {
+               return stringToType(typ.Primitive.ValueString())
+       }
+
+       if !typ.List.IsNull() {
+               var list icebergTableSchemaFieldTypeList
+               if err := typ.List.As(context.Background(), &list, 
basetypes.ObjectAsOptions{}); err.HasError() {
+                       return nil, errors.New("failed to parse list type")
+               }
+
+               elemType, err := stringToType(list.ElementType.ValueString())
+               if err != nil {
+                       return nil, err
+               }
+
+               return &iceberg.ListType{
+                       ElementID:       int(list.ElementID.ValueInt64()),
+                       Element:         elemType,
+                       ElementRequired: list.ElementRequired.ValueBool(),
+               }, nil
+       }
+
+       if !typ.Map.IsNull() {
+               var m icebergTableSchemaFieldTypeMap
+               if err := typ.Map.As(context.Background(), &m, 
basetypes.ObjectAsOptions{}); err.HasError() {
+                       return nil, errors.New("failed to parse map type")
+               }
+
+               keyType, err := stringToType(m.KeyType.ValueString())
+               if err != nil {
+                       return nil, err
+               }
+
+               valueType, err := stringToType(m.ValueType.ValueString())
+               if err != nil {
+                       return nil, err
+               }
+
+               return &iceberg.MapType{
+                       KeyID:         int(m.KeyID.ValueInt64()),
+                       KeyType:       keyType,
+                       ValueID:       int(m.ValueID.ValueInt64()),
+                       ValueType:     valueType,
+                       ValueRequired: m.ValueRequired.ValueBool(),
+               }, nil
+       }
+
+       return nil, errors.New("unsupported type")
+}
+
+func stringToType(s string) (iceberg.Type, error) {
+       switch s {
+       case "boolean":
+               return iceberg.BooleanType{}, nil
+       case "int":
+               return iceberg.Int32Type{}, nil
+       case "long":
+               return iceberg.Int64Type{}, nil
+       case "float":
+               return iceberg.Float32Type{}, nil
+       case "double":
+               return iceberg.Float64Type{}, nil
+       case "decimal":
+               return iceberg.DecimalType{}, nil

Review Comment:
   Yep, fixed with the new type system.



-- 
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