Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiwara committed May 8, 2024
1 parent 62a46ee commit 83cc3aa
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ aws-sdk-client-go: go.* *.go gen

clean:
rm -rf *_gen.go aws-sdk-client-go dist/ cmd/aws-sdk-client-gen/gen.go
go mod tidy

gen:
go generate ./cmd/aws-sdk-client-gen .
go fmt

test:
go test -v ./...
Expand Down
9 changes: 3 additions & 6 deletions cmd/aws-sdk-client-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func main() {
func gen(pkgName string, clientType reflect.Type, genNames []string) error {
log.Printf("generating %s_gen.go", pkgName)
buf := &strings.Builder{}
fmt.Fprintln(buf, "// Code generated by cmd/aws-sdk-client-gen/main.go; DO NOT EDIT.")
fmt.Fprintln(buf, "package sdkclient")
fmt.Fprintln(buf)
fmt.Fprintf(buf, `import (
Expand All @@ -43,7 +44,7 @@ func gen(pkgName string, clientType reflect.Type, genNames []string) error {
}
methodNames = append(methodNames, method.Name)
log.Printf("generating %s_%s", pkgName, method.Name)
fmt.Fprintf(buf, `func %s_%s(ctx context.Context, b json.RawMessage) (json.RawMessage, error) {
fmt.Fprintf(buf, `func %s_%s(ctx context.Context, b json.RawMessage) (any, error) {
svc := %s.NewFromConfig(awsCfg)
var in %s
if err := json.Unmarshal(b, &in); err != nil {
Expand All @@ -52,11 +53,7 @@ func gen(pkgName string, clientType reflect.Type, genNames []string) error {
if out, err := svc.%s(ctx, &in); err != nil {
return nil, fmt.Errorf("failed to call %s: %%w", err)
} else {
o, err := json.Marshal(out)
if err != nil {
return nil, fmt.Errorf("failed to marshal response: %%w", err)
}
return o, nil
return out, nil
}
}
`,
Expand Down
5 changes: 5 additions & 0 deletions gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ services:
- ListDeliveryStreams
kinesis:
# all methods of the service
ec2:
ecr:
s3:
sns:
sqs:
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

var awsCfg aws.Config

var clientMethods = make(map[string]func(context.Context, json.RawMessage) (json.RawMessage, error))
var clientMethods = make(map[string]func(context.Context, json.RawMessage) (any, error))

func Run(ctx context.Context) error {
var err error
Expand Down Expand Up @@ -53,8 +53,12 @@ func dispatchMethod(ctx context.Context, pkgName, methodName string, in json.Raw
if err != nil {
return err
}
b, err := json.Marshal(out)
if err != nil {
return fmt.Errorf("failed to marshal response: %w", err)
}
var buf bytes.Buffer
json.Indent(&buf, out, "", " ")
json.Indent(&buf, b, "", " ")
buf.WriteTo(os.Stdout)
fmt.Fprintln(os.Stdout)
return nil
Expand Down

0 comments on commit 83cc3aa

Please sign in to comment.