Skip to content

Commit 49c81dd

Browse files
authored
feat(sfn): make sfn init function optional (#630)
1 parent 1ee0f1b commit 49c81dd

4 files changed

Lines changed: 69 additions & 6 deletions

File tree

cli/serverless/golang/serverless.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package golang
22

33
import (
4+
"bufio"
45
"bytes"
56
"errors"
67
"fmt"
@@ -49,18 +50,21 @@ func (s *GolangServerless) Init(opts *serverless.Options) error {
4950

5051
// append main function
5152
ctx := Context{
52-
Name: s.opts.Name,
53-
ZipperAddr: s.opts.ZipperAddr,
54-
Credential: s.opts.Credential,
55-
UseEnv: s.opts.UseEnv,
53+
Name: s.opts.Name,
54+
ZipperAddr: s.opts.ZipperAddr,
55+
Credential: s.opts.Credential,
56+
UseEnv: s.opts.UseEnv,
57+
WithInitFunc: containsInitWithoutComment(source),
5658
}
5759

5860
// determine: rx stream serverless or raw bytes serverless.
5961
isRx := strings.Contains(string(source), "rx.Stream")
6062
isWasm := true
6163
mainFuncTmpl := ""
62-
mainFunc := WasmMainFuncTmpl
63-
var err error
64+
mainFunc, err := RenderTmpl(string(WasmMainFuncTmpl), &ctx)
65+
if err != nil {
66+
return fmt.Errorf("Init: %s", err)
67+
}
6468
if isRx {
6569
if isWasm {
6670
return errors.New("wasm does not support rx.Stream")
@@ -251,6 +255,18 @@ func generateCode(fset *token.FileSet, file *ast.File) ([]byte, error) {
251255
return buffer.Bytes(), nil
252256
}
253257

258+
func containsInitWithoutComment(source []byte) bool {
259+
scanner := bufio.NewScanner(bytes.NewReader(source))
260+
for scanner.Scan() {
261+
line := strings.TrimLeft(scanner.Text(), " ")
262+
263+
if strings.Contains(line, "Init()") && !strings.HasPrefix(line, "//") {
264+
return true
265+
}
266+
}
267+
return false
268+
}
269+
254270
func init() {
255271
serverless.Register(&GolangServerless{}, ".go")
256272
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package golang
2+
3+
import "testing"
4+
5+
func TestContainsInitWithoutComment(t *testing.T) {
6+
type args struct {
7+
source []byte
8+
}
9+
tests := []struct {
10+
name string
11+
args args
12+
want bool
13+
}{
14+
{
15+
name: "init function with comment",
16+
args: args{
17+
source: []byte(`// func Init() error {`),
18+
},
19+
want: false,
20+
},
21+
{
22+
name: "init function without comment",
23+
args: args{
24+
source: []byte(`func Init() error {`),
25+
},
26+
want: true,
27+
},
28+
{
29+
name: "no init function",
30+
args: args{
31+
source: []byte(``),
32+
},
33+
want: false,
34+
},
35+
}
36+
for _, tt := range tests {
37+
t.Run(tt.name, func(t *testing.T) {
38+
if got := containsInitWithoutComment(tt.args.source); got != tt.want {
39+
t.Errorf("containInitFunc() = %v, want %v", got, tt.want)
40+
}
41+
})
42+
}
43+
}

cli/serverless/golang/template.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ type Context struct {
4040
Credential string
4141
// use environment variables
4242
UseEnv bool
43+
// WithInitFunc determines whether to work with init function
44+
WithInitFunc bool
4345
}
4446

4547
// RenderTmpl renders the template with the given context

cli/serverless/golang/templates/wasm_main.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,7 @@
22
func main() {
33
guest.DataTags = DataTags
44
guest.Handler = Handler
5+
{{if .WithInitFunc}}
56
guest.Init = Init
7+
{{end}}
68
}

0 commit comments

Comments
 (0)