/
generator.go
68 lines (53 loc) · 1.54 KB
/
generator.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
package main
import (
"fmt"
"io"
"sort"
"strings"
"github.com/fgrosse/gotility"
)
func generate(w io.Writer, c *client) error {
if err := c.Validate(); err != nil {
return fmt.Errorf("invalid client: %s", err)
}
template := loadTemplate("templates/client.tmpl")
generateTypeName(c)
generateImports(c)
generateTypeComment(c)
return template.Execute(w, c)
}
func generateTypeName(c *client) {
if len(c.Name) < 6 || c.Name[len(c.Name)-6:] != "Client" {
c.Name = c.Name + "Client"
}
}
func generateImports(c *client) {
stdImportsSet := gotility.NewStringSet(`"fmt"`, `"net/http"`, `"net/url"`)
otherImports := []string{`"github.com/fgrosse/tigs/tigshttp"`}
if c.containsJSONEndpoints() {
stdImportsSet.Set(`"encoding/json"`)
stdImportsSet.Set(`"bytes"`)
stdImportsSet.Set(`"io/ioutil"`)
}
if c.containsPostfieldEndpoints() {
stdImportsSet.Set(`"strings"`)
stdImportsSet.Set(`"io/ioutil"`)
}
stdImports := stdImportsSet.All()
sort.Strings(stdImports)
sort.Strings(otherImports)
c.Imports = strings.Join(stdImports, "\n\t") + "\n\n\t" + strings.Join(otherImports, "\n\t")
for i := range c.Endpoints {
c.Endpoints[i].ClientName = c.Name
}
}
func generateTypeComment(c *client) {
if c.Description == "" {
c.Description = c.Name + " is an automatically generated HTTP client."
}
if len(c.Description) < len(c.Name) || c.Description[:len(c.Name)] != c.Name {
c.Description = c.Name + ": " + c.Description
}
c.Description = strings.TrimSpace(c.Description)
c.Description = strings.Replace(c.Description, "\n", "\n// ", -1)
}