Files
mautrix-telegram/pkg/gotd/gen/_template/interface_slice.tmpl
T
2025-06-27 20:03:37 -07:00

95 lines
3.5 KiB
Cheetah

{{- /*gotype: go.mau.fi/mautrix-telegram/pkg/gotd/gen.interfaceDef*/ -}}
{{ define "interface_slice" }}{{ $f := . }}
{{ template "slice" $f }}
{{ range $field := sortableFields ($f.SharedFields.Common) }}
{{ template "slice_sort_by" concat ($f) ($field) }}
{{ end }}
{{ range $s := $f.Constructors }}{{ if $s.Fields }}
{{ range $field := mapCollectableFields ($f.SharedFields.Common) }}
{{ template "slice_collect_to_map" concat ($f) ($field) ($s.Name) }}
{{ end }}
// As{{ $s.Name }} returns copy with only {{ $s.Name }} constructors.
func (s {{ template "slice_name" $f }}) As{{ $s.Name }}() (to {{ template "slice_name" $s }}) {
for _, elem := range s {
value, ok := elem.(*{{ $s.Name }})
if !ok {
continue
}
to = append(to, *value)
}
return to
}
{{- end }}{{- end }}
{{ range $mapping := $f.Mappings }}{{- if (not $mapping.Constructor) }}
{{- range $field := mapCollectableFields (index ($f.SharedFields) ($mapping.MapperName)) }}
{{- /*gotype: go.mau.fi/mautrix-telegram/pkg/gotd/gen.fieldDef*/ -}}
// Fill{{ $mapping.MapperName }}Map fills only {{ $mapping.MapperName }} constructors to given map.
func (s {{ template "slice_name" $f }}) Fill{{ $mapping.MapperName }}{{ template "map_collector_name" $field }}Map(to map[{{ $field.Type }}]{{ template "print_mapper_type" $mapping }}) {
for _, elem := range s {
value, ok := elem.As{{ $mapping.MapperName }}()
if !ok {
continue
}
to[value.Get{{ $field.Name }}()] = value
}
}
// {{ $mapping.MapperName }}ToMap collects only {{ $mapping.MapperName }} constructors to map.
func (s {{ template "slice_name" $f }}) {{ $mapping.MapperName }}To{{ template "map_collector_name" $field }}Map() map[{{ $field.Type }}]{{ template "print_mapper_type" $mapping }} {
r := make(map[{{ $field.Type }}]{{ template "print_mapper_type" $mapping }}, len(s))
s.Fill{{ $mapping.MapperName }}{{ template "map_collector_name" $field }}Map(r)
return r
}
{{ end }}
// AppendOnly{{ $mapping.MapperName }} appends only {{ $mapping.MapperName }} constructors to
// given slice.
func (s {{ template "slice_name" $f }}) AppendOnly{{ $mapping.MapperName }}(to []{{ template "print_mapper_type" $mapping }}) []{{ template "print_mapper_type" $mapping }} {
for _, elem := range s {
value, ok := elem.As{{ $mapping.MapperName }}()
if !ok {
continue
}
to = append(to, value)
}
return to
}
// As{{ $mapping.MapperName }} returns copy with only {{ $mapping.MapperName }} constructors.
func (s {{ template "slice_name" $f }}) As{{ $mapping.MapperName }}() (to []{{ template "print_mapper_type" $mapping }}) {
return s.AppendOnly{{ $mapping.MapperName }}(to)
}
{{- range $method := concat ("First") ("Last") }}
// {{ $method }}As{{ $mapping.MapperName }} returns {{ lower $method }} element of slice (if exists).
func (s {{ template "slice_name" $f }}) {{ $method }}As{{ $mapping.MapperName }}() (v {{ template "print_mapper_type" $mapping }}, ok bool) {
value, ok := s.{{ $method }}()
if !ok {
return
}
return value.As{{ $mapping.MapperName }}()
}
{{- end }}
{{- range $method := concat ("PopFirst") ("Pop") }}
// {{ $method }}As{{ $mapping.MapperName }} returns element of slice (if exists).
func (s *{{ template "slice_name" $f }}) {{ $method }}As{{ $mapping.MapperName }}() (v {{ template "print_mapper_type" $mapping }}, ok bool) {
value, ok := s.{{ $method }}()
if !ok {
return
}
return value.As{{ $mapping.MapperName }}()
}
{{ end }}
{{- end }}{{- end }}
{{- end }}