sijie commented on a change in pull request #3854: [issue #3767] support go function for pulsar URL: https://github.com/apache/pulsar/pull/3854#discussion_r266421844
########## File path: pulsar-function-go/pf/function.go ########## @@ -0,0 +1,183 @@ +/** + * 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 wriinputg, + * 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. + */ + +// +// This file borrows some of the implementations from {@link https://github.com/aws/aws-lambda-go/blob/master/lambda/handler.go} +// - errorHandler +// - validateArguments +// - validateReturns +// - NewFunction +// - Process +// + +package pf + +import ( + "context" + "fmt" + "reflect" + + "github.com/apache/pulsar/pulsar-function-go/log" +) + +type Function interface { + Process(ctx context.Context, input []byte) ([]byte, error) +} + +type pulsarFunction func(ctx context.Context, input []byte) ([]byte, error) + +func (function pulsarFunction) Process(ctx context.Context, input []byte) ([]byte, error) { + output, err := function(ctx, input) + if err != nil { + log.Errorf("process function error:[%s]\n", err.Error()) + return nil, err + } + + //todo: add go schema + //outputBytes, err := json.Marshal(output) + //if err != nil { + // log.Errorf("json marshal failed:%s", err.Error()) + // return nil, err + //} + + return output, nil +} + +func errorHandler(e error) pulsarFunction { + return func(ctx context.Context, input []byte) ([]byte, error) { + return nil, e + } +} + +func validateArguments(handler reflect.Type) (bool, error) { + handlerTakesContext := false + if handler.NumIn() > 2 { + return false, fmt.Errorf("handlers may not take more than two arguments, but handler takes %d", handler.NumIn()) + } else if handler.NumIn() > 0 { + contextType := reflect.TypeOf((*context.Context)(nil)).Elem() + argumentType := handler.In(0) + handlerTakesContext = argumentType.Implements(contextType) + if handler.NumIn() > 1 && !handlerTakesContext { + return false, fmt.Errorf("handler takes two arguments, but the first is not Context. got %s", argumentType.Kind()) + } + } + + return handlerTakesContext, nil +} + +func validateReturns(handler reflect.Type) error { + errorType := reflect.TypeOf((*error)(nil)).Elem() + if handler.NumOut() > 2 { + return fmt.Errorf("handler may not return more than two values") + } else if handler.NumOut() > 1 { + if !handler.Out(1).Implements(errorType) { + return fmt.Errorf("handler returns two values, but the second does not implement error") + } + } else if handler.NumOut() == 1 { + if !handler.Out(0).Implements(errorType) { + return fmt.Errorf("handler returns a single value, but it does not implement error") + } + } + return nil +} + +func NewFunction(inputFunc interface{}) Function { + if inputFunc == nil { + return errorHandler(fmt.Errorf("handler is nil")) + } + handler := reflect.ValueOf(inputFunc) + handlerType := reflect.TypeOf(inputFunc) + if handlerType.Kind() != reflect.Func { + return errorHandler(fmt.Errorf("handler kind %s is not %s", handlerType.Kind(), reflect.Func)) + } + + takesContext, err := validateArguments(handlerType) + if err != nil { + return errorHandler(err) + } + + if err := validateReturns(handlerType); err != nil { + return errorHandler(err) + } + + return pulsarFunction(func(ctx context.Context, input []byte) ([]byte, error) { + // construct arguments + var args []reflect.Value + if takesContext { + args = append(args, reflect.ValueOf(ctx)) + } + if (handlerType.NumIn() == 1 && !takesContext) || handlerType.NumIn() == 2 { + eventType := handlerType.In(handlerType.NumIn() - 1) + event := reflect.New(eventType) + + //todo: add go schema Review comment: nit: create a github issue and link it here ---------------------------------------------------------------- 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
