move gotd fork into repo. (#111)

- update to latest telegram layer
- remove some references to fields in tg.Entities that don't exist in
the schema
- originally added here:
https://github.com/beeper/td/commit/820929062a2ba0104397bc01235ab58a9cff780e
  - referenced here
-
https://github.com/mautrix/telegramgo/commit/124f0967ed195b5a380c9bd02e170ada9710dde3
-
https://github.com/mautrix/telegramgo/commit/4205047aab2e0639217148b5d125bfaab668bd8e
This commit is contained in:
Adam Van Ymeren
2025-06-27 20:03:37 -07:00
committed by GitHub
parent 0952df0244
commit 7a04f298d2
19264 changed files with 1539697 additions and 84 deletions
@@ -0,0 +1,2 @@
// Package genutil is a utility package for query helpers codegeneration.
package genutil
@@ -0,0 +1,55 @@
package genutil
import (
"go/types"
"golang.org/x/tools/go/packages"
)
// Func is a function representation.
type Func struct {
Sig *types.Signature
Decl *types.Func
}
// Results returns function results.
func (f Func) Results() *types.Tuple {
return f.Sig.Results()
}
// Args returns function arguments.
func (f Func) Args() *types.Tuple {
return f.Sig.Params()
}
// Funcs collects all function from package using given filter.
// Parameter keep may be nil.
func Funcs(pkg *packages.Package, keep func(f Func) bool) []Func {
var r []Func
for _, def := range pkg.TypesInfo.Defs {
if def == nil {
continue
}
f, ok := def.(*types.Func)
if !ok {
continue
}
sig, ok := f.Type().(*types.Signature)
if !ok {
continue
}
repr := Func{
Sig: sig,
Decl: f,
}
if keep(repr) {
r = append(r, repr)
}
}
return r
}
@@ -0,0 +1,46 @@
package genutil
import (
"context"
"go/ast"
"go/parser"
"go/token"
"os"
"github.com/go-faster/errors"
"golang.org/x/tools/go/packages"
)
func loadPackages(ctx context.Context, dir, pattern string, environ []string) ([]*packages.Package, error) {
return packages.Load(&packages.Config{
Context: ctx,
Dir: dir,
Mode: packages.NeedTypes |
packages.NeedTypesInfo |
packages.NeedTypesSizes |
packages.NeedSyntax |
packages.NeedDeps,
Env: environ,
Fset: token.NewFileSet(),
ParseFile: func(fset *token.FileSet, filename string, src []byte) (*ast.File, error) {
const mode = parser.AllErrors | parser.ParseComments
return parser.ParseFile(fset, filename, src, mode)
},
}, pattern)
}
// Load loads package using given pattern.
func Load(ctx context.Context, pattern string) (*packages.Package, error) {
pkgs, err := loadPackages(ctx, "", pattern, os.Environ())
if err != nil {
return nil, errors.Wrap(err, "load packages")
}
for _, pkg := range pkgs {
if pkg.ID == pattern {
return pkg, nil
}
}
return nil, errors.Errorf("package %s not found", pattern)
}
@@ -0,0 +1,10 @@
package genutil
import "go/types"
// PrintType prints typename into string without package name.
func PrintType(typ types.Type) string {
return types.TypeString(typ, func(i *types.Package) string {
return i.Name()
})
}
@@ -0,0 +1,75 @@
package genutil
import (
"go/types"
"github.com/go-faster/errors"
"golang.org/x/tools/go/packages"
)
// Implementations finds iface implementations.
func Implementations(pkg *packages.Package, iface *types.Interface) []*types.Named {
var r []*types.Named
for _, def := range pkg.TypesInfo.Defs {
if def == nil || !def.Exported() {
continue
}
named, ok := def.Type().(*types.Named)
if !ok {
continue
}
if !types.Implements(types.NewPointer(named), iface) {
continue
}
r = append(r, named)
}
return r
}
// Interfaces is a simple utility struct to find interfaces and implementations.
type Interfaces struct {
pkg *packages.Package
implsCache map[string][]*types.Named
}
// NewInterfaces creates new Interfaces structure.
func NewInterfaces(pkg *packages.Package) *Interfaces {
return &Interfaces{pkg: pkg, implsCache: map[string][]*types.Named{}}
}
// Interface finds interface by name.
func (c *Interfaces) Interface(name string) (*types.Interface, error) {
obj := c.pkg.Types.Scope().Lookup(name)
if obj == nil {
return nil, errors.Errorf("%q not found", name)
}
v, ok := obj.Type().Underlying().(*types.Interface)
if !ok {
return nil, errors.Errorf("%q has unexpected kind type %T", name, obj.Type().Underlying())
}
return v, nil
}
// Implementations finds interface implementations by interface name.
func (c *Interfaces) Implementations(name string) ([]*types.Named, error) {
impls, ok := c.implsCache[name]
if ok {
return impls, nil
}
iface, err := c.Interface(name)
if err != nil {
return nil, errors.Wrapf(err, "find %q", name)
}
impls = Implementations(c.pkg, iface)
c.implsCache[name] = impls
return impls, nil
}
@@ -0,0 +1,35 @@
package genutil
import (
"bytes"
"go/format"
"io"
"io/fs"
"os"
"text/template"
"github.com/go-faster/errors"
"go.mau.fi/mautrix-telegram/pkg/gotd/gen"
)
// WriteTemplate loads template from FS and executes it to given output writer.
func WriteTemplate(source fs.FS, out io.Writer, name string, data interface{}) error {
tmpl := template.New("templates").Funcs(gen.Funcs())
tmpl = template.Must(tmpl.ParseFS(source, "_template/*.tmpl"))
var buf bytes.Buffer
if err := tmpl.ExecuteTemplate(&buf, name, data); err != nil {
return errors.Wrap(err, "template")
}
formatted, err := format.Source(buf.Bytes())
if err != nil {
if _, cpyErr := io.Copy(os.Stdout, &buf); cpyErr != nil {
return errors.Wrapf(cpyErr, "dump generated (original error: %v)", err)
}
return errors.Wrap(err, "format")
}
_, err = out.Write(formatted)
return err
}