lostluck commented on a change in pull request #11257: [BEAM-3301] Create runtime invokers for SDF methods. URL: https://github.com/apache/beam/pull/11257#discussion_r400396738
########## File path: sdks/go/pkg/beam/core/runtime/exec/sdf_invokers.go ########## @@ -0,0 +1,131 @@ +// 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 exec + +import ( + "github.com/apache/beam/sdks/go/pkg/beam/core/funcx" + "github.com/apache/beam/sdks/go/pkg/beam/core/sdf" + "github.com/apache/beam/sdks/go/pkg/beam/core/util/reflectx" + "github.com/apache/beam/sdks/go/pkg/beam/internal/errors" + "reflect" +) + +func invokeCreateInitialRestriction(fn *funcx.Fn, elms *FullValue) (rest interface{}, err error) { + if fn == nil { + return nil, nil + } + + switch fnT := fn.Fn.(type) { + case reflectx.Func1x1: + return fnT.Call1x1(elms.Elm), nil + case reflectx.Func2x1: + return fnT.Call2x1(elms.Elm, elms.Elm2), nil + default: + var inputs []interface{} + switch fn.Fn.Type().NumIn() { + case 1: + inputs = []interface{}{elms.Elm} + case 2: + inputs = []interface{}{elms.Elm, elms.Elm2} + default: + err := errors.Errorf("fn %v has unexpected number of inputs: %v", fn.Fn.Name(), fn.Fn.Type().NumIn()) + return nil, errors.WithContext(err, "sdf CreateInitialRestriction invoker") + } + rets := fn.Fn.Call(inputs) + return rets[0], nil + } +} + +func invokeSplitRestriction(fn *funcx.Fn, elms *FullValue, rest interface{}) ([]interface{}, error) { + if fn == nil { + return nil, nil + } + + var ret interface{} + switch fnT := fn.Fn.(type) { + case reflectx.Func2x1: + ret = fnT.Call2x1(elms.Elm, rest) + case reflectx.Func3x1: + ret = fnT.Call3x1(elms.Elm, elms.Elm2, rest) + default: + var inputs []interface{} + switch fn.Fn.Type().NumIn() { + case 2: + inputs = []interface{}{elms.Elm, rest} + case 3: + inputs = []interface{}{elms.Elm, elms.Elm2, rest} + default: + err := errors.Errorf("fn %v has unexpected number of inputs: %v", fn.Fn.Name(), fn.Fn.Type().NumIn()) + return nil, errors.WithContext(err, "sdf SplitRestriction invoker") + } + ret = fn.Fn.Call(inputs)[0] + } + + // Return value is an interface{}, but we need to convert it to a []interface{}. + val := reflect.ValueOf(ret) + s := make([]interface{}, 0, val.Len()) + for i := 0; i < val.Len(); i++ { + s = append(s, val.Index(i).Interface()) + } Review comment: I won't worry about it until after optimization, where it might be clearer what we can re-use. This is presently nicely factored, so unless we can make it shorter/clearer with a reuse of DoFn invoker (possibly by adding other helpers/generated methods, etc or otherwise), this is fine. ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services
