{{- /*gotype: go.mau.fi/mautrix-telegram/pkg/gotd/gen.structConfig*/ -}}
{{ define "field_mapping" }}{{- $s := .Struct -}}

{{- range $f := $s.Fields }}{{ if and (not $f.DoubleSlice) (ne ($f.Type) ("bin.Fields")) }}
{{ if not $f.Slice }}

{{- $mappings := index ($.Config.Mappings) ($f.Type) -}}
{{- range $mapping := $mappings }}{{- if (not $mapping.Constructor) }}
{{- /*gotype: go.mau.fi/mautrix-telegram/pkg/gotd/gen.constructorMapping*/ -}}
// Get{{ $f.Name }}As{{ $mapping.MapperName }} returns mapped value of {{ $f.Name }} {{ if $f.Conditional }}
{{- if not ($f.ConditionalBool) }}conditional field and
// boolean which is true if field was set.
{{- else }}conditional field.
{{- end }}
{{- else }}field.
{{- end }}
func ({{ $s.Receiver }} *{{ $s.Name }}) Get{{ $f.Name }}As{{ $mapping.MapperName }}() ({{ template "print_mapper_type" $mapping }}, bool) {
    {{- if $f.Conditional }}
    if value, ok := {{ $s.Receiver }}.Get{{ $f.Name }}(); ok {
        return value.As{{ $mapping.MapperName }}()
    }
    return nil, false
    {{- else }}
    return {{ $s.Receiver }}.{{ $f.Name }}.As{{ $mapping.MapperName }}()
    {{- end }}
}
{{- end }}{{- end }}

{{- else }}

{{- if $f.Interface }}
// Map{{ $f.Name }} returns field {{ $f.Name }} wrapped in {{ template "slice_field_name" $f }} helper.
func ({{ $s.Receiver }} *{{ $s.Name }}) Map{{ $f.Name }}() (value {{ template "slice_field_name" $f }}{{ if $f.Conditional }}, ok bool{{ end }}) {
    {{- if $f.Conditional }}
    if !{{ $s.Receiver }}.{{ $f.ConditionalField }}.Has({{ $f.ConditionalIndex }}) {
        return value, false
    }
    return {{ template "slice_field_name" $f }}({{ $s.Receiver }}.{{ $f.Name }}), true
    {{- else }}
    return {{ template "slice_field_name" $f }}({{ $s.Receiver }}.{{ $f.Name }})
    {{- end }}
}
{{- end }}

{{- end }}
{{- end }}{{- end }}

{{ end }}
