You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2020/08/06 17:28:20 UTC

[GitHub] [beam] lostluck commented on a change in pull request #12471: [BEAM-9615] Add initial Schema to Go conversions.

lostluck commented on a change in pull request #12471:
URL: https://github.com/apache/beam/pull/12471#discussion_r466572230



##########
File path: sdks/go/pkg/beam/core/runtime/graphx/schema/schema.go
##########
@@ -0,0 +1,245 @@
+// 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 schema contains utility functions for relating Go types and Beam Schemas.
+//
+// Not all Go types can be converted to schemas. This is Go is more expressive than
+// Beam schemas. Just as not all Go types can be serialized, similarly,
+// not all Beam Schemas will have a conversion to Go types, until the correct
+// mechanism exists in the SDK to handle them.
+//
+// While efforts will be made to have conversions be reversable, this will not
+// be possible in all instances. Eg. Go arrays as fields will be converted to
+// Beam Arrays, but a Beam Array type will map by default to a Go slice.
+package schema
+
+import (
+	"fmt"
+	"reflect"
+	"strings"
+
+	"github.com/apache/beam/sdks/go/pkg/beam/core/util/reflectx"
+	"github.com/apache/beam/sdks/go/pkg/beam/internal/errors"
+	pipepb "github.com/apache/beam/sdks/go/pkg/beam/model/pipeline_v1"
+)
+
+// FromType returns a Beam Schema of the passed in type.
+// Returns an error if the type cannot be converted to a Schema.
+func FromType(ot reflect.Type) (*pipepb.Schema, error) {
+	t := ot // keep the original type for errors.
+	// The top level schema for a pointer to struct and the struct is the same.
+	if t.Kind() == reflect.Ptr {
+		t = t.Elem()
+	}
+	if t.Kind() != reflect.Struct {
+		return nil, errors.Errorf("cannot convert %v to schema. FromType only converts structs to schemas", ot)
+	}
+	return structToSchema(t), nil
+}
+
+func structToSchema(t reflect.Type) *pipepb.Schema {
+	fields := make([]*pipepb.Field, 0, t.NumField())
+	for i := 0; i < t.NumField(); i++ {
+		fields = append(fields, structFieldToField(t.Field(i)))
+	}
+	return &pipepb.Schema{
+		Fields: fields,
+	}
+}
+
+func structFieldToField(sf reflect.StructField) *pipepb.Field {
+	name := sf.Name
+	if tag := sf.Tag.Get("beam"); tag != "" {
+		name, _ = parseTag(tag)
+	}
+	ftype := reflectTypeToFieldType(sf.Type)
+
+	return &pipepb.Field{
+		Name: name,
+		Type: ftype,
+	}
+}
+
+func reflectTypeToFieldType(ot reflect.Type) *pipepb.FieldType {
+	var isPtr bool
+	t := ot
+	if t.Kind() == reflect.Ptr {
+		isPtr = true
+		t = t.Elem()
+	}
+	switch t.Kind() {
+	case reflect.Map:
+		kt := reflectTypeToFieldType(t.Key())
+		vt := reflectTypeToFieldType(t.Elem())
+		return &pipepb.FieldType{
+			Nullable: isPtr,
+			TypeInfo: &pipepb.FieldType_MapType{
+				MapType: &pipepb.MapType{
+					KeyType:   kt,
+					ValueType: vt,
+				},
+			},
+		}
+	case reflect.Struct:
+		sch := structToSchema(t)
+		return &pipepb.FieldType{
+			Nullable: isPtr,
+			TypeInfo: &pipepb.FieldType_RowType{
+				RowType: &pipepb.RowType{
+					Schema: sch,
+				},
+			},
+		}
+	case reflect.Slice, reflect.Array:
+		// Special handling for []byte
+		if t == reflectx.ByteSlice {
+			return &pipepb.FieldType{
+				Nullable: isPtr,
+				TypeInfo: &pipepb.FieldType_AtomicType{
+					AtomicType: pipepb.AtomicType_BYTES,
+				},
+			}
+		}
+		vt := reflectTypeToFieldType(t.Elem())
+		return &pipepb.FieldType{
+			Nullable: isPtr,
+			TypeInfo: &pipepb.FieldType_ArrayType{
+				ArrayType: &pipepb.ArrayType{
+					ElementType: vt,
+				},
+			},
+		}
+	case reflect.Interface, reflect.Chan, reflect.UnsafePointer, reflect.Complex128, reflect.Complex64, reflect.Int:
+		panic(fmt.Sprintf("Unsupported type to convert to schema: %v", ot))
+	default: // must be an atomic type
+		enum := reflectTypeToAtomicType(t)
+		return &pipepb.FieldType{
+			Nullable: isPtr,
+			TypeInfo: &pipepb.FieldType_AtomicType{
+				AtomicType: enum,
+			},
+		}
+	}
+}
+
+func reflectTypeToAtomicType(rt reflect.Type) pipepb.AtomicType {
+	switch rt {
+	case reflectx.Uint8:
+		return pipepb.AtomicType_BYTE
+	case reflectx.Int16:
+		return pipepb.AtomicType_INT16
+	case reflectx.Int32:
+		return pipepb.AtomicType_INT32
+	case reflectx.Int64, reflectx.Int:
+		return pipepb.AtomicType_INT64
+	case reflectx.Float32:
+		return pipepb.AtomicType_FLOAT
+	case reflectx.Float64:
+		return pipepb.AtomicType_DOUBLE
+	case reflectx.String:
+		return pipepb.AtomicType_STRING
+	case reflectx.Bool:
+		return pipepb.AtomicType_BOOLEAN
+	case reflectx.ByteSlice:
+		return pipepb.AtomicType_BYTES
+	default:
+		panic(fmt.Sprintf("non atomic reflect type: %v", rt))
+	}
+}
+
+// ToType returns a Go type of the passed in Schema.
+// Types returned by ToType are always of Struct kind.
+// Returns an error if the Schema cannot be converted to a type.
+func ToType(s *pipepb.Schema) (reflect.Type, error) {
+	fields := make([]reflect.StructField, 0, len(s.GetFields()))
+	for _, sf := range s.GetFields() {
+		rf := fieldToStructField(sf)
+		fields = append(fields, rf)
+	}
+	return reflect.StructOf(fields), nil
+}
+
+func fieldToStructField(sf *pipepb.Field) reflect.StructField {
+	name := sf.GetName()
+	return reflect.StructField{
+		Name: strings.ToUpper(name[:1]) + name[1:], // Go field name must be capitalized for export and encoding.
+		Type: fieldTypeToReflectType(sf.GetType()),
+		Tag:  reflect.StructTag(fmt.Sprintf("beam:\"%s\"", name)),
+	}
+}
+
+var atomicTypeToReflectType = map[pipepb.AtomicType]reflect.Type{

Review comment:
       Honestly, when I first wrote it, it complained that reflect.Type couldn't be used as a key, but I do that all the time so... very strange.




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

For queries about this service, please contact Infrastructure at:
users@infra.apache.org