Debian Codegraph @

compare_generate.go

raw
1 //go:build ignore
2
3 package main
4
5 import (
6 "bytes"
7 "flag"
8 "fmt"
9 "go/ast"
10 "html/template"
11 "log"
12 "strings"
13
14 "codeberg.org/miekg/dns/internal/generate"
15 )
16
17 var hdr = `
18 // Code generated by "go run compare_generate.go"; DO NOT EDIT.
19
20 package dns
21
22 import (
23 "strings"
24 "slices"
25 "cmp"
26 )
27
28 `
29
30 var compareFunc = template.Must(template.New("compareFunc").Parse(`
31 func compare(a, b RR) int {
32 switch x := a.(type) {
33 {{range .}} case *{{.}}:
34 return x.compare(b)
35 {{end}} }
36 if x, ok := a.(Comparer); ok {
37 return x.Compare(b)
38 }
39 return 0
40 }
41
42 `))
43
44 const out = "zcompare.go"
45
46 func main() {
47 flag.Parse()
48 specs, err := generate.StructTypeSpecs("rdata/rdata.go")
49 if err != nil {
50 log.Fatalf("Failed to generate %s: %v", out, err)
51 }
52 emptydata, err := generate.StructTypeSpecs("types.go")
53 if err != nil {
54 log.Fatalf("Failed to generate %s: %v", out, err)
55 }
56 specs = append(specs, generate.FilterTypeSpecs(emptydata, generate.EmptyData)...)
57 b := &bytes.Buffer{}
58 b.WriteString(hdr)
59
60 // main compare func
61 types, err := generate.Types("types.go")
62 if err != nil {
63 log.Fatalf("Failed to generate %s: %v", out, err)
64 }
65
66 if err := compareFunc.Execute(b, types); err != nil {
67 log.Fatalf("Failed to generate %s: %v", out, err)
68 }
69
70 for _, spec := range specs {
71 rrname := spec.Name.Name
72 //
73 // Compare functions, does compare(a, b RR) as a.compare(b)
74 //
75
76 fmt.Fprintf(b, "func (rr *%s) compare(b RR) (x int) {\n", rrname)
77 strct := spec.Type.(*ast.StructType)
78
79 if generate.IsEmbedded(strct) {
80 fmt.Fprintf(b, "return rr.%[1]s.compare(&b.(*%[2]s).%[1]s)\n", strct.Fields.List[0].Type, rrname)
81 fmt.Fprint(b, "}\n\n")
82 continue
83 }
84 for _, field := range strct.Fields.List {
85
86 if len(field.Names) == 0 {
87 continue
88 }
89 fieldname := field.Names[0].String()
90 if fieldname == "Hdr" {
91 continue
92 }
93
94 o := func(s string) {
95 fmt.Fprintf(b, s+`;if x != 0 {
96 if x < 0 {
97 return -1
98 }
99 return 1
100 }
101 `, fieldname, rrname, fieldname)
102 }
103 // fieldtype is either slice or the actual singular type name.
104 fieldtype := ""
105 if id, ok := field.Type.(*ast.Ident); ok {
106 fieldtype = id.Name
107 }
108 if _, ok := field.Type.(*ast.ArrayType); ok {
109 fieldtype = "slice"
110 }
111
112 tag := ""
113 if field.Tag != nil {
114 tag = strings.Trim(field.Tag.Value, "`")
115 }
116
117 if fieldtype == "slice" {
118 switch tag {
119 case `dns:"-"`: // ignored
120 case `dns:"txt"`:
121 o(`if len(rr.%[1]s) < len(b.(*%[2]s).%[3]s) {
122 return -1
123 }
124 if len(rr.%[1]s) > len(b.(*%[2]s).%[3]s) {
125 return 1
126 }
127 for i := range rr.%[1]s {
128 if x != 0 {
129 break
130 }
131 x = len(rr.%[1]s[i]) - len(b.(*%[2]s).%[3]s[i])
132 if x == 0 {
133 x = strings.Compare(rr.%[1]s[i], b.(*%[2]s).%[3]s[i])
134 }
135 }`)
136 case `dns:"opt"`:
137 // never signed, but compare would be nice...
138
139 case `dns:"nsec"`:
140 o(`x = slices.Compare(rr.%s, b.(*%s).%s)`)
141
142 case `dns:"pairs"`:
143 o(`x = comparepair(rr.%s, b.(*%s).%s)`)
144
145 case `dns:"infos"`:
146 o(`x = compareinfo(rr.%s, b.(*%s).%s)`)
147
148 case `dns:"name"`:
149 o(`if len(rr.%[1]s) < len(b.(*%[2]s).%[3]s) {
150 return -1
151 }
152 if len(rr.%[1]s) > len(b.(*%[2]s).%[3]s) {
153 return 1
154 }
155 for i := range rr.%[1]s {
156 if x != 0 {
157 break
158 }
159 x = comparename(rr.%[1]s[i], b.(*%[2]s).%[3]s[i])
160 }`)
161 }
162 continue
163 }
164
165 // non-slice fields
166
167 switch {
168 case tag == `dns:"-"`: // ignored
169 case tag == `dns:"cname"`:
170 fallthrough
171 case tag == `dns:"mname"`:
172 fallthrough
173 case tag == `dns:"name"`:
174 o("x = comparename(rr.%s, b.(*%s).%s)")
175 case tag == `dns:"a"`:
176 o("x = rr.%s.Compare(b.(*%s).%s)")
177 case tag == `dns:"aaaa"`:
178 o("x = rr.%s.Compare(b.(*%s).%s)")
179 case tag == `dns:"ttl"`:
180 o(`x = int(rr.%s) - int(b.(*%s).%s)`)
181 case tag == `dns:"uint48"`:
182 o(`x = cmp.Compare(rr.%s, b.(*%s).%s)`)
183 case strings.HasPrefix(tag, `dns:"size-base32`): // size-base32 can be packed just like base32
184 fallthrough
185 case tag == `dns:"base32"`:
186 o(`x = comparebase32(rr.%s, b.(*%s).%s)`)
187
188 case strings.HasPrefix(tag, `dns:"size-base64`): // size-base64 can be packed just like base64
189 fallthrough
190 case tag == `dns:"base64"`:
191 o(`x = comparebase64(rr.%s, b.(*%s).%s)`)
192
193 case strings.HasPrefix(tag, `dns:"size-hex:SaltLength`):
194 fallthrough
195 case strings.HasPrefix(tag, `dns:"size-hex`): // size-hex can be packed just like hex
196 fallthrough
197 case tag == `dns:"hex"`:
198 if fieldname == "Data" {
199 o(`x = comparehex(rr.RFC3597.%s, b.(*%s).RFC3597.%s)`)
200 } else {
201 o(`x = comparehex(rr.%s, b.(*%s).%s)`)
202 }
203
204 case tag == `dns:"any"`:
205 // can we do strings.Compare here? TODO(miek)
206 o("x = len(rr.%s) - len(b.(*%s).%s)")
207 o("x = strings.Compare(rr.%s, b.(*%s).%s)")
208
209 case tag == "":
210 switch fieldtype {
211 case "uint8":
212 fallthrough
213 case "uint16":
214 fallthrough
215 case "uint32":
216 fallthrough
217 case "uint64":
218 o(`x = cmp.Compare(rr.%s, b.(*%s).%s)`)
219 case "string":
220 o("x = len(rr.%s) - len(b.(*%s).%s)")
221 o("x = strings.Compare(rr.%s, b.(*%s).%s)")
222 }
223 }
224 }
225 fmt.Fprint(b, "return 0\n}\n\n")
226 }
227
228 generate.Write(b, out)
229 }
230
231 // structMember will take a tag like dns:"size-base32:SaltLength" and return the last part of this string.
232 func structMember(s string) string {
233 idx := strings.LastIndex(s, ":")
234 return strings.TrimSuffix(s[idx+1:], `"`)
235 }
236
237 // structTag will take a tag like dns:"size-base32:SaltLength" and return base32.
238 func structTag(s string) string {
239 s = strings.TrimPrefix(s, `dns:"size-`)
240 s, _, _ = strings.Cut(s, ":")
241 return s
242 }