Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --input-stream option. #7

Merged
merged 8 commits into from
May 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
inject length field
fujiwara committed May 20, 2024
commit b4b7212d9832c3cafb254fc11bca75783d27d4c3
34 changes: 27 additions & 7 deletions cmd/aws-sdk-client-gen/main.go
Original file line number Diff line number Diff line change
@@ -28,12 +28,16 @@ import (
func {{ $.PkgName }}_{{ .Name }}(ctx context.Context, p *clientMethodParam) (any, error) {
svc := {{ $.PkgName }}.NewFromConfig(p.awsCfg)
var in {{ .Input }}
{{ if .InputReaderLengthField }}
p.mustInject("{{ .InputReaderLengthField }}", p.InputReaderLength)
{{ end }}

if err := json.Unmarshal(p.InputBytes, &in); err != nil {
return nil, fmt.Errorf("failed to unmarshal request: %w", err)
}
{{- if .InputReaderKey }}
{{- if .InputReaderField }}
if p.InputReader != nil {
in.{{ .InputReaderKey }} = p.InputReader
in.{{ .InputReaderField }} = p.InputReader
}
{{- end }}
return svc.{{ .Name }}(ctx, &in)
@@ -70,18 +74,34 @@ func gen(pkgName string, clientType reflect.Type, genNames []string) error {
continue
}
inputParam := method.Type.In(2)
var inputReaderKey string
var inputReaderField, inputReaderLengthField string
for j := 0; j < inputParam.Elem().NumField(); j++ {
field := inputParam.Elem().Field(j)
if t := field.Type.String(); t == "io.Reader" {
log.Printf("found %s field in %s.%sInput %s %s", t, pkgName, method.Name, field.Name, t)
inputReaderKey = field.Name
if inputReaderField != "" {
return fmt.Errorf("found multiple io.Reader fields in %s.%sInput", pkgName, method.Name)
}
inputReaderField = field.Name
}
}
if inputReaderField != "" {
for j := 0; j < inputParam.Elem().NumField(); j++ {
field := inputParam.Elem().Field(j)
if t := field.Name; strings.Contains(t, "Length") {
log.Printf("found %s field in %s.%sInput %s %s", t, pkgName, method.Name, field.Name, t)
if inputReaderLengthField != "" {
return fmt.Errorf("found multiple Length fields in %s.%sInput", pkgName, method.Name)
}
inputReaderLengthField = field.Name
}
}
}
methods = append(methods, map[string]string{
"Name": method.Name,
"Input": strings.TrimPrefix(params[2], "*"),
"InputReaderKey": inputReaderKey,
"Name": method.Name,
"Input": strings.TrimPrefix(params[2], "*"),
"InputReaderField": inputReaderField,
"InputReaderLengthField": inputReaderLengthField,
})
/*
output := method.Type.Out(0)
1 change: 1 addition & 0 deletions gen.yaml
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ services:
- ListBuckets
- ListObjects
- ListObjectsV2
- HeadObject
- PutObject
- PutObjectAcl
- PutObjectTagging
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ require (
github.com/aws/aws-sdk-go-v2 v1.27.0
github.com/aws/aws-sdk-go-v2/config v1.27.14
github.com/goccy/go-yaml v1.11.3
github.com/itchyny/gojq v0.12.15
github.com/jmespath/go-jmespath v0.4.0
)

@@ -23,8 +24,9 @@ require (
github.com/aws/aws-sdk-go-v2/service/sts v1.28.8 // indirect
github.com/aws/smithy-go v1.20.2 // indirect
github.com/fatih/color v1.10.0 // indirect
github.com/itchyny/timefmt-go v0.1.5 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
golang.org/x/sys v0.6.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
)
10 changes: 8 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -46,6 +46,10 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/itchyny/gojq v0.12.15 h1:WC1Nxbx4Ifw5U2oQWACYz32JK8G9qxNtHzrvW4KEcqI=
github.com/itchyny/gojq v0.12.15/go.mod h1:uWAHCbCIla1jiNxmeT5/B5mOjSdfkCq6p8vxWg+BM10=
github.com/itchyny/timefmt-go v0.1.5 h1:G0INE2la8S6ru/ZI5JecgyzbbJNs5lG1RcBqa7Jm6GE=
github.com/itchyny/timefmt-go v0.1.5/go.mod h1:nEP7L+2YmAbT2kZ2HfSs1d8Xtw9LY8D2stDBckWakZ8=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
@@ -54,17 +58,19 @@ github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import (
"strings"

"github.com/alecthomas/kong"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/jmespath/go-jmespath"
)
@@ -125,13 +126,19 @@ func (c *CLI) clientMethodParam(ctx context.Context) (*clientMethodParam, error)
}
}
p.InputReader = buf
p.InputReaderLength = aws.Int64(int64(buf.Len()))
default:
f, err := os.Open(c.InputStream)
if err != nil {
return nil, fmt.Errorf("failed to open input file: %w", err)
}
p.InputReader = f
p.cleanup = append(p.cleanup, f.Close)
st, err := f.Stat()
if err != nil {
return nil, fmt.Errorf("failed to stat input file: %w", err)
}
p.InputReaderLength = aws.Int64(st.Size())
}
return p, nil
}
42 changes: 40 additions & 2 deletions param.go
Original file line number Diff line number Diff line change
@@ -2,15 +2,18 @@ package sdkclient

import (
"encoding/json"
"fmt"
"io"
"log"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/itchyny/gojq"
)

type clientMethodParam struct {
InputBytes json.RawMessage
InputReader io.Reader
InputBytes json.RawMessage
InputReader io.Reader
InputReaderLength *int64

awsCfg aws.Config
cleanup []func() error
@@ -23,3 +26,38 @@ func (p *clientMethodParam) Cleanup() {
}
}
}

func (p *clientMethodParam) mustInject(field string, value *int64) {
v := make(map[string]any)
if err := json.Unmarshal(p.InputBytes, &v); err != nil {
panic(fmt.Sprintf("failed to marshal %s:", err))
}
var q string
if value == nil {
q = fmt.Sprintf("del(.%s)", field)
} else {
q = fmt.Sprintf(".%s = %d", field, *value)
}
log.Printf("[debug] inject by %s", q)
query, err := gojq.Parse(q)
if err != nil {
panic(fmt.Sprintf("failed to parse query %s: %v", q, err))
}
iter := query.Run(v)
for {
if v, ok := iter.Next(); ok {
if !ok {
break
}
if err, ok := v.(error); ok {
if err, ok := err.(*gojq.HaltError); ok && err.Value() == nil {
break
}
panic(err)
}
p.InputBytes, _ = json.Marshal(v)
log.Println("[debug] injected", string(p.InputBytes))
return
}
}
}