Skip to content

Commit

Permalink
feat(sfn): make sfn init function optional (#630)
Browse files Browse the repository at this point in the history
  • Loading branch information
woorui authored Oct 6, 2023
1 parent 1ee0f1b commit 49c81dd
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
28 changes: 22 additions & 6 deletions cli/serverless/golang/serverless.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package golang

import (
"bufio"
"bytes"
"errors"
"fmt"
Expand Down Expand Up @@ -49,18 +50,21 @@ func (s *GolangServerless) Init(opts *serverless.Options) error {

// append main function
ctx := Context{
Name: s.opts.Name,
ZipperAddr: s.opts.ZipperAddr,
Credential: s.opts.Credential,
UseEnv: s.opts.UseEnv,
Name: s.opts.Name,
ZipperAddr: s.opts.ZipperAddr,
Credential: s.opts.Credential,
UseEnv: s.opts.UseEnv,
WithInitFunc: containsInitWithoutComment(source),
}

// determine: rx stream serverless or raw bytes serverless.
isRx := strings.Contains(string(source), "rx.Stream")
isWasm := true
mainFuncTmpl := ""
mainFunc := WasmMainFuncTmpl
var err error
mainFunc, err := RenderTmpl(string(WasmMainFuncTmpl), &ctx)
if err != nil {
return fmt.Errorf("Init: %s", err)
}
if isRx {
if isWasm {
return errors.New("wasm does not support rx.Stream")
Expand Down Expand Up @@ -251,6 +255,18 @@ func generateCode(fset *token.FileSet, file *ast.File) ([]byte, error) {
return buffer.Bytes(), nil
}

func containsInitWithoutComment(source []byte) bool {
scanner := bufio.NewScanner(bytes.NewReader(source))
for scanner.Scan() {
line := strings.TrimLeft(scanner.Text(), " ")

if strings.Contains(line, "Init()") && !strings.HasPrefix(line, "//") {
return true
}
}
return false
}

func init() {
serverless.Register(&GolangServerless{}, ".go")
}
43 changes: 43 additions & 0 deletions cli/serverless/golang/serverless_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package golang

import "testing"

func TestContainsInitWithoutComment(t *testing.T) {
type args struct {
source []byte
}
tests := []struct {
name string
args args
want bool
}{
{
name: "init function with comment",
args: args{
source: []byte(`// func Init() error {`),
},
want: false,
},
{
name: "init function without comment",
args: args{
source: []byte(`func Init() error {`),
},
want: true,
},
{
name: "no init function",
args: args{
source: []byte(``),
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := containsInitWithoutComment(tt.args.source); got != tt.want {
t.Errorf("containInitFunc() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 2 additions & 0 deletions cli/serverless/golang/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type Context struct {
Credential string
// use environment variables
UseEnv bool
// WithInitFunc determines whether to work with init function
WithInitFunc bool
}

// RenderTmpl renders the template with the given context
Expand Down
2 changes: 2 additions & 0 deletions cli/serverless/golang/templates/wasm_main.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
func main() {
guest.DataTags = DataTags
guest.Handler = Handler
{{if .WithInitFunc}}
guest.Init = Init
{{end}}
}

0 comments on commit 49c81dd

Please sign in to comment.