-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace generator code with text/template
- Loading branch information
Showing
2 changed files
with
87 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"bytes" | ||
"log" | ||
"os" | ||
"text/template" | ||
|
||
"github.com/goccy/go-yaml" | ||
) | ||
|
||
const templateStr = `// Code generated by cmd/aws-sdk-client-gen-gen/main.go; DO NOT EDIT. | ||
package main | ||
import ( | ||
"reflect" | ||
{{ range $key, $value := .Services }} | ||
"github.com/aws/aws-sdk-go-v2/service/{{ $key }}" | ||
{{- end }} | ||
) | ||
func generateAll() { | ||
{{ range $key, $value := .Services }} | ||
{{- if eq (len $value) 0 }} | ||
gen("{{ $key }}", reflect.TypeOf({{ $key }}.New({{ $key }}.Options{})), nil) | ||
{{- else }} | ||
gen("{{ $key }}", reflect.TypeOf({{ $key }}.New({{ $key }}.Options{})), {{ printf "%#v" $value }}) | ||
{{- end }} | ||
{{ end }} | ||
} | ||
` | ||
|
||
type GenerateConfig struct { | ||
Services map[string][]string `json:"services"` | ||
} | ||
|
||
func main() { | ||
log.Println("generating gen.go") | ||
o, err := os.Create("gen.go") | ||
if err != nil { | ||
log.Fatalf("failed to create gen.go: %v", err) | ||
} | ||
|
||
f, err := os.Open("../../gen.yaml") | ||
if err != nil { | ||
log.Fatalf("failed to open gen.yaml: %v", err) | ||
} | ||
defer f.Close() | ||
|
||
cfg := GenerateConfig{} | ||
if err := yaml.NewDecoder(f).Decode(&cfg); err != nil { | ||
log.Fatalf("failed to decode gen.yaml: %v", err) | ||
} | ||
|
||
fmt.Fprintln(o, "// Code generated by cmd/aws-sdk-client-gen-gen/main.go; DO NOT EDIT.") | ||
fmt.Fprintln(o, "package main") | ||
fmt.Fprintln(o, "import \"reflect\"") | ||
for k := range cfg.Services { | ||
fmt.Fprintf(o, "import \"github.com/aws/aws-sdk-go-v2/service/%s\"\n", k) | ||
tmpl, err := template.New("gen").Parse(templateStr) | ||
if err != nil { | ||
log.Fatalf("failed to parse template: %v", err) | ||
} | ||
buf := &bytes.Buffer{} | ||
if err := tmpl.Execute(buf, cfg); err != nil { | ||
log.Fatalf("failed to execute template: %v", err) | ||
} | ||
fmt.Fprintln(o, "func generateAll() {") | ||
for k, v := range cfg.Services { | ||
if len(v) == 0 { | ||
fmt.Fprintf(o, " gen(\"%s\", reflect.TypeOf(%s.New(%s.Options{})), nil)\n", k, k, k) | ||
} else { | ||
fmt.Fprintf(o, " gen(\"%s\", reflect.TypeOf(%s.New(%s.Options{})), %#v)\n", k, k, k, v) | ||
} | ||
|
||
if err := os.WriteFile("gen.go", buf.Bytes(), 0644); err != nil { | ||
log.Fatalf("failed to write gen.go: %v", err) | ||
} | ||
fmt.Fprintln(o, "}") | ||
log.Println("generated gen.go") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters