Skip to content

Commit

Permalink
Merge pull request #113 from tdakkota/feature/gen/encode-zero-value
Browse files Browse the repository at this point in the history
gen: automatic encoding of optional fields
  • Loading branch information
ernado authored Jan 8, 2021
2 parents 155719f + 629199c commit 19c0f17
Show file tree
Hide file tree
Showing 763 changed files with 24,706 additions and 18 deletions.
5 changes: 5 additions & 0 deletions bin/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import "strconv"
// that fields "1", "5" and "10" were set.
type Fields uint32

// Zero returns true, if all bits are equal to zero.
func (f Fields) Zero() bool {
return f == 0
}

// String implement fmt.Stringer
func (f Fields) String() string {
return strconv.FormatUint(uint64(f), 2)
Expand Down
13 changes: 8 additions & 5 deletions internal/gen/_template/handlers.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type UpdateContext struct {

Users map[int]*User
Chats map[int]*Chat
Channels map[int]*Channel
init bool
}

Expand All @@ -38,12 +39,14 @@ func (u *UpdateContext) lazyInitFromUpdates(updates *Updates) {
}

u.Chats = make(map[int]*Chat, len(updates.Chats))
u.Channels = make(map[int]*Channel, len(updates.Chats))
for _, class := range updates.Chats {
chat, ok := class.(*Chat)
if !ok {
continue
}
u.Chats[chat.ID] = chat
switch chat := class.(type) {
case *Chat:
u.Chats[chat.ID] = chat
case *Channel:
u.Channels[chat.ID] = chat
}
}
}

Expand Down
14 changes: 12 additions & 2 deletions internal/gen/_template/main.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type {{ $s.Name }} struct {
const {{ $s.Name }}TypeID = 0x{{ $s.HexID }};
{{- end }}

{{template "zero_derive" $s }}
{{template "string_derive" $s }}

// Encode implements bin.Encoder.
Expand All @@ -45,7 +46,14 @@ func ({{ $s.Receiver }} *{{ $s.Name }}) Encode({{ $s.BufArg }} *bin.Buffer) erro
return fmt.Errorf("can't encode {{ $s.RawType }} as nil")
}
{{- if not $s.Vector }}
{{ $s.BufArg }}.PutID({{ $s.Name }}TypeID)
{{ $s.BufArg }}.PutID({{ $s.Name }}TypeID)
{{- end }}
{{- range $f := $s.Fields }}
{{- if $f.Conditional }}
if !({{ $s.Receiver }}.{{template "compare_zero" $f}}) {
{{ $s.Receiver }}.{{ $f.ConditionalField }}.Set({{ $f.ConditionalIndex }})
}
{{- end }}
{{- end }}
{{- range $f := $s.Fields }}
{{- if not $f.ConditionalBool }}
Expand Down Expand Up @@ -100,7 +108,7 @@ func ({{ $s.Receiver }} *{{ $s.Name }}) Encode({{ $s.BufArg }} *bin.Buffer) erro
{{- range $f := $s.Fields }}
{{ if $f.Conditional }}
// Set{{ $f.Name }} sets value of {{ $f.Name }} conditional field.
func ({{ $s.Receiver }} *{{ $s.Name }}) Set{{ $f.Name }}(value {{ if $f.DoubleSlice }}[][]{{ else if $f.Slice }}[]{{ end }} {{ $f.Type }}) {
func ({{ $s.Receiver }} *{{ $s.Name }}) Set{{ $f.Name }}(value {{ template "print_type" $f }}) {
{{- if $f.ConditionalBool }}
if value {
{{ $s.Receiver }}.{{ $f.ConditionalField }}.Set({{ $f.ConditionalIndex }})
Expand Down Expand Up @@ -311,7 +319,9 @@ type {{ $f.Name }} interface {
bin.Encoder
bin.Decoder
construct() {{ $f.Name }}

fmt.Stringer
Zero() bool
}

// Decode{{ $f.Func }} implements binary de-serialization for {{ $f.Name }}.
Expand Down
30 changes: 30 additions & 0 deletions internal/gen/_template/zero.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{{ define "zero_derive" }}{{ $s := . }}
{{- /*gotype: github.com/gotd/td/internal/gen.structDef*/ -}}
func ({{ $s.Receiver }} *{{ $s.Name }}) Zero() bool {
if {{ $s.Receiver }} == nil {
return true
}
{{- /*gotype: github.com/gotd/td/internal/gen.fieldDef*/ -}}
{{- range $f := $s.Fields }}{{- if ne ($f.Name) ($f.ConditionalField) }}
if !({{ $s.Receiver }}.{{template "compare_zero" $f}}) {
return false
}
{{- end }}{{- end }}

return true
}
{{- end -}}

{{ define "compare_zero" }}
{{- /*gotype: github.com/gotd/td/internal/gen.fieldDef*/ -}}
{{- .Name -}}
{{- if or (.Slice) (.DoubleSlice) -}} == nil
{{- else if eq (.Type) ("bin.Int128") -}} == bin.Int128{}
{{- else if eq (.Type) ("bin.Int256") -}} == bin.Int256{}
{{- else if or (hasPrefix (.Type) ("int")) (hasPrefix (.Type) ("float")) }} == 0
{{- else if eq (.Type) ("string") -}} == ""
{{- else if eq (.Type) ("bool") -}} == false
{{- else if eq (.Type) ("bin.Object") }} == nil
{{- else if hasSuffix (.Type) ("Class") }} == nil
{{- else -}}.Zero()
{{- end -}}{{- end }}
78 changes: 78 additions & 0 deletions internal/gen/example/tl_abstract_message_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions internal/gen/example/tl_account_themes_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions internal/gen/example/tl_auth_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions internal/gen/example/tl_bool_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions internal/gen/example/tl_bytes_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 19c0f17

Please sign in to comment.