Skip to content

Commit

Permalink
update test sources and golang bytes version factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Brat-vseznamus authored and hrissan committed Jul 11, 2024
1 parent 3f0a0be commit 9505f03
Show file tree
Hide file tree
Showing 14 changed files with 1,196 additions and 49 deletions.
62 changes: 62 additions & 0 deletions internal/tlcodegen/qt_factory_bytes.qtpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{%- import "fmt" -%}

{%- func (gen *Gen2) generateFactoryBytes(sortedImports []string, directImports *DirectImports) -%}
{%s= HeaderComment %}
{%- code typeWrappers := gen.generatedTypesList -%}
package {%s FactoryGoPackageNameBytes %}

import (
"{%s= gen.MetaPackageName %}"
{%- for _, wr := range sortedImports -%}
"{%s= gen.options.TLPackageNameFull %}/{%s= wr %}"
{%- endfor -%}
)

func CreateFunctionBytes(tag uint32) meta.Function {
return meta.CreateFunctionBytes(tag)
}

func CreateObjectBytes(tag uint32) meta.Object {
return meta.CreateObjectBytes(tag)
}

// name can be in any of 3 forms "ch_proxy.insert#7cf362ba", "ch_proxy.insert" or "#7cf362ba"
func CreateFunctionFromNameBytes(name string) meta.Function {
return meta.CreateFunctionFromNameBytes(name)
}

// name can be in any of 3 forms "ch_proxy.insert#7cf362ba", "ch_proxy.insert" or "#7cf362ba"
func CreateObjectFromNameBytes(name string) meta.Object {
return meta.CreateObjectFromNameBytes(name)
}

func init() {
{%- for _, wr := range typeWrappers -%}
{%- code hasBytes := wr.wantsBytesVersion && wr.hasBytesVersion -%}
{%- if wr.tlTag == 0 || !wr.IsTopLevel() -%} {%- continue -%} {%- endif -%}
{%- if fun, ok := wr.trw.(*TypeRWStruct); ok && len(wr.NatParams) == 0-%}
{%- code tlTag := fmt.Sprintf("0x%08x", wr.tlTag) -%}
{%- if wr.unionParent != nil && wr.unionParent.IsEnum -%}
meta.SetGlobalFactoryCreateForEnumElementBytes({%s tlTag %})
{%- continue -%}
{%- endif -%}
{% stripspace %}
{%- if fun.ResultType != nil -%}
meta.SetGlobalFactoryCreateForFunctionBytes({%s= tlTag %},
func() meta.Object { var ret{% space %}{%s= wr.TypeString2(hasBytes, directImports, nil, false, true) %}; return &ret },
func() meta.Function { var ret{% space %}{%s= wr.TypeString2(hasBytes, directImports, nil, false, true) %}; return &ret },
{%- if wr.WrLong != nil -%}
func() meta.Function { var ret{% space %}{%s= wr.WrLong.TypeString2(hasBytes, directImports, nil, false, true) %}; return &ret },
{%- else -%}
nil,
{%- endif -%}
{%- else -%}
meta.SetGlobalFactoryCreateForObjectBytes({%s= tlTag %},
func() meta.Object { var ret{% space %}{%s= wr.TypeString2(hasBytes, directImports, nil, false, true) %}; return &ret }
{%- endif -%}
){% endstripspace %}
{%- endif -%}
{%- endfor -%}
}

{%- endfunc -%}
129 changes: 129 additions & 0 deletions internal/tlcodegen/qt_factory_bytes.qtpl.go

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

87 changes: 87 additions & 0 deletions internal/tlcodegen/qt_meta.qtpl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,36 @@ func CreateObjectFromName(name string) Object {
return nil
}

func CreateFunctionBytes(tag uint32) Function {
if item := FactoryItemByTLTagBytes(tag); item != nil && item.createFunctionBytes != nil {
return item.createFunctionBytes()
}
return nil
}

func CreateObjectBytes(tag uint32) Object {
if item := FactoryItemByTLTagBytes(tag); item != nil && item.createObjectBytes != nil {
return item.createObjectBytes()
}
return nil
}

// name can be in any of 3 forms "ch_proxy.insert#7cf362ba", "ch_proxy.insert" or "#7cf362ba"
func CreateFunctionFromNameBytes(name string) Function {
if item := FactoryItemByTLNameBytes(name); item != nil && item.createFunctionBytes != nil {
return item.createFunctionBytes()
}
return nil
}

// name can be in any of 3 forms "ch_proxy.insert#7cf362ba", "ch_proxy.insert" or "#7cf362ba"
func CreateObjectFromNameBytes(name string) Object {
if item := FactoryItemByTLNameBytes(name); item != nil && item.createObjectBytes != nil {
return item.createObjectBytes()
}
return nil
}

{%- if gen.options.GenerateLegacyJsonRead -%}
// legacy wrapper, will be removed soon
func ReadJSONLegacy(item Object, legacyTypeNames bool, b []byte) error {
Expand All @@ -115,6 +145,10 @@ type TLItem struct {
createFunction func() Function
createFunctionLong func() Function
createObject func() Object
createFunctionBytes func() Function
createFunctionLongBytes func() Function
createObjectBytes func() Object

}

func (item TLItem) TLTag() uint32 { return item.tag }
Expand Down Expand Up @@ -201,10 +235,23 @@ func FactoryItemByTLName(name string) *TLItem {
return itemsByName[name]
}

func FactoryItemByTLTagBytes(tag uint32) *TLItem {
return itemsBytesByTag[tag]
}

func FactoryItemByTLNameBytes(name string) *TLItem {
return itemsBytesByName[name]
}


var itemsByTag = map[uint32]*TLItem {}

var itemsByName = map[string]*TLItem {}

var itemsBytesByTag = map[uint32]*TLItem {}

var itemsBytesByName = map[string]*TLItem {}

func SetGlobalFactoryCreateForFunction(itemTag uint32, createObject func() Object, createFunction func() Function, createFunctionLong func() Function) {
item := itemsByTag[itemTag]
if item == nil {
Expand All @@ -231,6 +278,40 @@ func SetGlobalFactoryCreateForEnumElement(itemTag uint32) {
item.createObject = func() Object { return item }
}

func SetGlobalFactoryCreateForFunctionBytes(itemTag uint32, createObject func() Object, createFunction func() Function, createFunctionLong func() Function) {
item := itemsBytesByTag[itemTag]
if item == nil {
panic(fmt.Sprintf("factory cannot find function tag #%08x to set", itemTag))
}
item.createObjectBytes = createObject
item.createFunctionBytes = createFunction
item.createFunctionLongBytes = createFunctionLong
}

func SetGlobalFactoryCreateForObjectBytes(itemTag uint32, createObject func() Object) {
item := itemsBytesByTag[itemTag]
if item == nil {
panic(fmt.Sprintf("factory cannot find item tag #%08x to set", itemTag))
}
item.createObjectBytes = createObject
}

func SetGlobalFactoryCreateForEnumElementBytes(itemTag uint32) {
item := itemsBytesByTag[itemTag]
if item == nil {
panic(fmt.Sprintf("factory cannot find enum tag #%08x to set", itemTag))
}
item.createObjectBytes = func() Object { return item }
}

func pleaseImportFactoryBytesObject() Object {
panic("factory functions are not linked to reduce code bloat, please import 'gen/factory_bytes' instead of 'gen/meta'.")
}

func pleaseImportFactoryBytesFunction() Function {
panic("factory functions are not linked to reduce code bloat, please import 'gen/factory_bytes' instead of 'gen/meta'.")
}

func pleaseImportFactoryObject() Object {
panic("factory functions are not linked to reduce code bloat, please import 'gen/factory' instead of 'gen/meta'.")
}
Expand All @@ -244,7 +325,12 @@ func fillObject(n1 string, n2 string, item *TLItem) {
itemsByName[item.tlName] = item
itemsByName[n1] = item
itemsByName[n2] = item
itemsBytesByTag[item.tag] = item
itemsBytesByName[item.tlName] = item
itemsBytesByName[n1] = item
itemsBytesByName[n2] = item
item.createObject = pleaseImportFactoryObject
item.createObjectBytes = pleaseImportFactoryBytesObject
// code below is as fast, but allocates some extra strings which are already in binary const segment due to JSON code
// itemsByName[fmt.Sprintf("%s#%08x", item.tlName, item.tag)] = item
// itemsByName[fmt.Sprintf("#%08x", item.tag)] = item
Expand All @@ -253,6 +339,7 @@ func fillObject(n1 string, n2 string, item *TLItem) {
func fillFunction(n1 string, n2 string, item *TLItem) {
fillObject(n1, n2, item)
item.createFunction = pleaseImportFactoryFunction
item.createFunctionBytes = pleaseImportFactoryBytesFunction
}

func init() {
Expand Down
Loading

0 comments on commit 9505f03

Please sign in to comment.