File size: 2,437 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main

import (
	"flag"
	"fmt"
	"go/ast"
	"go/parser"
	"go/token"
	"os"
	"sort"
	"strconv"
	"strings"
	"text/template"

	"github.com/target/goalert/limit"
)

type SystemLimitInfo struct {
	ID          limit.ID
	Description string
	Value       int
}

type SystemLimitScanner struct {
	data []SystemLimitInfo
}

func (s *SystemLimitScanner) Visit(node ast.Node) ast.Visitor {
	v, ok := node.(*ast.ValueSpec)
	if !ok {
		return s
	}

	name, ok := v.Type.(*ast.Ident)
	if !ok || name.Name != "ID" {
		return s
	}

	var comments []string
	if v.Doc != nil {
		comments = make([]string, len(v.Doc.List))
		for index, val := range v.Doc.List {
			comments[index] = strings.TrimSpace(strings.TrimPrefix(val.Text, "//"))
		}
	}

	s.data = append(s.data, SystemLimitInfo{
		ID:          limit.ID(v.Names[0].Name),
		Description: strings.Join(comments, "\n"),
	})

	return s
}

var tmpl = template.Must(
	template.
		New("maplimit.go").
		Funcs(template.FuncMap{
			"quote": strconv.Quote,
		}).Parse(`// Code generated by devtools/limitapigen DO NOT EDIT.

package graphql2
import (
	"github.com/target/goalert/limit"
)
// MapLimitValues will map a Limit struct into a flat list of SystemLimit structs.
func MapLimitValues(l limit.Limits) []SystemLimit {
	return []SystemLimit {
		{{- range . }}
		{ID: "{{.ID}}", Description: {{quote .Description}}, Value: l[limit.{{.ID}}]},
		{{- end}}
	}
}
// ApplyLimitValues will apply a list of LimitValues to a Limit struct.
func ApplyLimitValues(l limit.Limits, vals []SystemLimitInput) (limit.Limits, error) {
	for _, v := range vals {
		switch v.ID {
			{{- range .}}
		case "{{.ID}}":
			l[limit.{{.ID}}] = v.Value
		{{- end}}
		default:
			return l, validation.NewFieldError("ID", fmt.Sprintf("unknown limit ID '%s'", v.ID))	
		}
	}
	return l, nil
}

`))

func main() {
	out := flag.String("out", "", "Output file.")
	flag.Parse()

	w := os.Stdout
	if *out != "" {
		fd, err := os.Create(*out)
		if err != nil {
			panic(err)
		}
		defer fd.Close()
		w = fd
	}

	fset := token.NewFileSet() // positions are relative to fset
	// Parse src but stop after processing the imports.
	f, err := parser.ParseFile(fset, "../limit/id.go", nil, parser.ParseComments)
	if err != nil {
		fmt.Println(err)
		return
	}

	s := SystemLimitScanner{}
	ast.Walk(&s, f)
	sort.Slice(s.data, func(i, j int) bool { return s.data[i].ID < s.data[j].ID })
	err = tmpl.Execute(w, s.data)
	if err != nil {
		panic(err)
	}
}