Files
threshold/api/internal/genapi/update.go
T

125 lines
2.8 KiB
Go
Raw Normal View History

2020-05-19 12:10:39 -07:00
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"text/template"
)
type updateInfo struct {
baseType string
targetType string
path string
}
var updateFuncs = map[string][]*updateInfo{
"scopes": {
{
2020-07-22 00:14:28 -04:00
baseType: "Scope",
targetType: "Scope",
path: "scopes",
2020-06-05 13:02:58 -07:00
},
2020-07-22 00:14:28 -04:00
},
"groups": {
2020-06-15 15:22:41 -07:00
{
2020-07-22 00:14:28 -04:00
baseType: "Group",
targetType: "Group",
2020-06-15 15:22:41 -07:00
path: "groups",
},
2020-07-22 00:14:28 -04:00
},
"users": {
2020-06-22 06:56:27 -07:00
{
2020-07-22 00:14:28 -04:00
baseType: "User",
targetType: "User",
2020-06-09 11:20:59 -07:00
path: "users",
},
2020-07-22 00:14:28 -04:00
},
"roles": {
2020-06-05 13:02:58 -07:00
{
2020-07-22 00:14:28 -04:00
baseType: "Role",
targetType: "Role",
path: "roles",
},
2020-05-19 12:10:39 -07:00
},
}
func writeUpdateFuncs() {
for outPkg, funcs := range updateFuncs {
outFile := os.Getenv("GEN_BASEPATH") + fmt.Sprintf("/api/%s/update.gen.go", outPkg)
outBuf := bytes.NewBuffer([]byte(fmt.Sprintf(
`// Code generated by "make api"; DO NOT EDIT.
package %s
`, outPkg)))
for _, updateInfo := range funcs {
updateFuncTemplate.Execute(outBuf, struct {
2020-06-05 13:02:58 -07:00
BaseType string
TargetType string
TargetName string
Path string
2020-05-19 12:10:39 -07:00
}{
2020-06-05 13:02:58 -07:00
BaseType: updateInfo.baseType,
TargetType: updateInfo.targetType,
TargetName: strings.Split(updateInfo.targetType, ".")[strings.Count(updateInfo.targetType, ".")],
Path: updateInfo.path,
2020-05-19 12:10:39 -07:00
})
}
if err := ioutil.WriteFile(outFile, outBuf.Bytes(), 0644); err != nil {
fmt.Printf("error writing file %q: %v\n", outFile, err)
os.Exit(1)
}
}
}
var updateFuncTemplate = template.Must(template.New("").Parse(
`
2020-06-05 13:02:58 -07:00
func (s {{ .BaseType }}) Update{{ .TargetName }}(ctx context.Context, r *{{ .TargetType }}) (*{{ .TargetType }}, *api.Error, error) {
2020-07-22 00:14:28 -04:00
if r == nil {
}
if r.Id == "" {
return nil, nil, fmt.Errorf("empty {{ .TargetType }} ID field in Create{{ .TargetName }} request")
}
2020-05-19 12:10:39 -07:00
if s.Client == nil {
2020-06-05 13:02:58 -07:00
return nil, nil, fmt.Errorf("nil client in Create{{ .TargetName }} request")
2020-05-19 12:10:39 -07:00
}
2020-07-22 00:14:28 -04:00
var opts []api.Option
if s.Scope.Id != "" {
2020-05-19 12:10:39 -07:00
// If it's explicitly set here, override anything that might be in the
// client
2020-07-22 00:14:28 -04:00
opts = append(opts, api.WithScopeId(s.Scope.Id))
2020-05-19 12:10:39 -07:00
}
2020-06-05 13:02:58 -07:00
id := r.Id
r.Id = ""
2020-05-19 12:10:39 -07:00
2020-07-22 00:14:28 -04:00
req, err := s.Client.NewRequest(ctx, "PATCH", fmt.Sprintf("%s/%s", "{{ .Path }}", id), r, opts...)
2020-05-19 12:10:39 -07:00
if err != nil {
2020-06-05 13:02:58 -07:00
return nil, nil, fmt.Errorf("error creating Create{{ .TargetName }} request: %w", err)
2020-05-19 12:10:39 -07:00
}
resp, err := s.Client.Do(req)
if err != nil {
2020-06-05 13:02:58 -07:00
return nil, nil, fmt.Errorf("error performing client request during Update{{ .TargetName }} call: %w", err)
2020-05-19 12:10:39 -07:00
}
target := new({{ .TargetType }})
apiErr, err := resp.Decode(target)
if err != nil {
2020-06-05 13:02:58 -07:00
return nil, nil, fmt.Errorf("error decoding Update{{ .TargetName }} repsonse: %w", err)
2020-05-19 12:10:39 -07:00
}
2020-07-22 00:14:28 -04:00
{{ if (eq .TargetType "Scope") }}
2020-05-19 12:10:39 -07:00
target.Client = s.Client.Clone()
2020-07-22 00:14:28 -04:00
target.Client.SetScopeId(target.Id)
2020-05-19 12:10:39 -07:00
{{ else }}
target.Client = s.Client
{{ end }}
return target, apiErr, nil
}
`))