-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
main.go
115 lines (96 loc) · 2.36 KB
/
main.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
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
// Binary mktyping generates TypingActionBuilder.
package main
import (
"flag"
"reflect"
"github.com/go-faster/errors"
"github.com/gotd/td/tdp"
"github.com/gotd/td/telegram/message/internal/mkrun"
"github.com/gotd/td/tg"
)
// Field represents type field.
type Field struct {
// Name is Go name of field.
Name string
// Type is Go type of field.
Type string
}
// Type represents generated type.
type Type struct {
// Name is Go name of type.
Name string
// Fields is slice of type fields.
Fields []Field
// SchemaType is related schema type.
SchemaType tdp.Type
}
const rawTemplate = `// Code generated by mktyping, DO NOT EDIT.
package {{ $.PackageName }}
import (
"context"
"github.com/gotd/td/tg"
)
var (
_ = tg.Invoker(nil)
_ = context.Context(nil)
)
{{- /*gotype: github.com/gotd/td/telegram/message/internal/mkrun.Config*/ -}}
{{- range $typ := $.Data }}
{{ $helperName := trimSuffix (trimPrefix $typ.Name "SendMessage") "Action" -}}
// {{ $helperName }} sends {{ $typ.Name }}.
func (b *TypingActionBuilder) {{ $helperName }}(ctx context.Context,
{{- range $f := $typ.Fields }}{{ lowerFirst $f.Name }} {{ $f.Type }},{{ end }}) error {
return b.send(ctx, &tg.{{ $typ.Name }}{
{{- range $f := $typ.Fields }}
{{ $f.Name }}: {{ lowerFirst $f.Name }},
{{- end }}
})
}
{{- end }}
`
var (
constructors = tg.ClassConstructorsMap()
create = tg.TypesConstructorMap()
)
type generator struct{}
func (g generator) Name() string {
return "mktyping"
}
func (g generator) Flags(set *flag.FlagSet) {}
func (g generator) Template() string {
return rawTemplate
}
func (g generator) Data() (interface{}, error) {
var types []Type
for _, typeID := range constructors[tg.SendMessageActionClassName] {
v, ok := create[typeID]().(tdp.Object)
if !ok {
return nil, errors.Errorf("bad type %#x", typeID)
}
schemaType := v.TypeInfo()
tv := reflect.TypeOf(v).Elem()
var fields []Field
for _, field := range schemaType.Fields {
rf, ok := tv.FieldByName(field.Name)
if !ok {
return nil, errors.Errorf(
"field of %q type %q not found",
field.Name, schemaType.Name,
)
}
fields = append(fields, Field{
Name: field.Name,
Type: rf.Type.String(),
})
}
types = append(types, Type{
Name: tv.Name(),
Fields: fields,
SchemaType: v.TypeInfo(),
})
}
return types, nil
}
func main() {
mkrun.Main(generator{})
}