-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3.go
148 lines (122 loc) · 3.57 KB
/
s3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//go:build gen
package main
import (
"context"
"fmt"
"os"
"reflect"
"strings"
"text/template"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
const tmpl = `// Code generated by go generate; DO NOT EDIT.
package aws
import (
"context"
"github.com/grafana/sobek"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
type S3Client struct {
*AWS
sdk *s3.Client
}
func (a *AWS) newS3Client(call sobek.ConstructorCall) *sobek.Object {
awsCfg := a.constructorCallToConfig("S3Client", call)
sdk := s3.NewFromConfig(awsCfg, func(o *s3.Options) {
o.UsePathStyle = true // Ensure path-style is used
})
client := &S3Client{
AWS: a,
sdk: sdk,
}
return a.vu.Runtime().ToValue(client).ToObject(a.vu.Runtime())
}
{{ range . }}
func (c *S3Client) {{ .Name }}({{ .FunctionCall }}) sobek.Value {
in := &{{.InputType}}{}
if err := fromSobekObject(c.vu.Runtime(), obj, in); err != nil {
panic(err)
}
out, err := c.sdk.{{ .InnerFunctionCall }}
if err != nil {
panic(err)
}
val, err := toSobekObject(c.vu, out)
if err != nil {
panic(err)
}
return val
}
{{ end }}
`
type S3Client interface {
ListBuckets(ctx context.Context, params *s3.ListBucketsInput, optFns ...func(*s3.Options)) (*s3.ListBucketsOutput, error)
ListObjects(ctx context.Context, params *s3.ListObjectsInput, optFns ...func(*s3.Options)) (*s3.ListObjectsOutput, error)
GetObject(ctx context.Context, params *s3.GetObjectInput, optFns ...func(*s3.Options)) (*s3.GetObjectOutput, error)
}
func main() {
t := reflect.TypeOf((*S3Client)(nil)).Elem()
data := make([]map[string]string, 0)
for i := 0; i < t.NumMethod(); i++ {
// Parsing the method signature
var (
method = t.Method(i)
params []string
callArgs []string
functionCall string
inputType string
innerFunctionCall = fmt.Sprintf("%s(", method.Name)
)
for j := 0; j < method.Type.NumIn(); j++ {
isTheLastArgument := j == method.Type.NumIn()-1
p := method.Type.In(j)
paramName := fmt.Sprintf("param%d", j)
params = append(params, fmt.Sprintf("%s %s", paramName, p))
if j > 0 { // skip receiver
callArgs = append(callArgs, paramName)
}
switch {
// Is context.Context
case p.Implements(reflect.TypeOf((*context.Context)(nil)).Elem()):
innerFunctionCall += "context.Background(), "
// Variadic argument
case method.Type.IsVariadic() && isTheLastArgument:
// explicitly skip variadic parameters
// Pointer to
case p.Kind() == reflect.Ptr:
functionCall += "obj *sobek.Object,"
inputType = strings.ReplaceAll(p.String(), "*", "")
innerFunctionCall += "in, "
}
}
innerFunctionCall += ")" // close the function call
results := []string{}
for j := 0; j < method.Type.NumOut(); j++ {
r := method.Type.Out(j)
results = append(results, r.String())
}
data = append(data, map[string]string{
"Name": method.Name,
"Params": fmt.Sprintf("%s", strings.Join(params, ", ")),
"Results": fmt.Sprintf("%s", strings.Join(results, ", ")),
"CallArgs": fmt.Sprintf("%s", strings.Join(callArgs, ", ")),
"FunctionCall": functionCall,
"InnerFunctionCall": innerFunctionCall,
"InputType": inputType,
})
}
tmplParsed, err := template.New("wrapper").Parse(tmpl)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing template: %v\n", err)
return
}
file, err := os.Create("s3_gen.go")
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating file: %v\n", err)
return
}
defer file.Close()
if err := tmplParsed.Execute(file, data); err != nil {
fmt.Fprintf(os.Stderr, "Error executing template: %v\n", err)
}
}