mirror of
https://github.com/c0re100/gotdlib.git
synced 2026-02-22 04:30:17 +01:00
add list unmarshallers
This commit is contained in:
parent
cc7d37acc2
commit
ec36320d03
5 changed files with 1534 additions and 17 deletions
|
|
@ -285,10 +285,9 @@ func (entity *tdlibType) GetClass() *tlparser.Class {
|
|||
func (entity *tdlibType) HasClassProperties() bool {
|
||||
for _, prop := range entity.GetType().Properties {
|
||||
tdlibTypeProperty := TdlibTypeProperty(prop.Name, prop.Type, entity.schema)
|
||||
if tdlibTypeProperty.IsClass() && !tdlibTypeProperty.IsList() {
|
||||
if tdlibTypeProperty.IsClass() {
|
||||
return true
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return false
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package codegen
|
|||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"github.com/zelenin/go-tdlib/tlparser"
|
||||
)
|
||||
|
||||
|
|
@ -125,11 +124,15 @@ func (*%s) GetType() string {
|
|||
for _, property := range typ.Properties {
|
||||
tdlibTypeProperty := TdlibTypeProperty(property.Name, property.Type, schema)
|
||||
|
||||
if !tdlibTypeProperty.IsClass() || tdlibTypeProperty.IsList() {
|
||||
if !tdlibTypeProperty.IsClass() {
|
||||
buf.WriteString(fmt.Sprintf(" %s %s `json:\"%s\"`\n", tdlibTypeProperty.ToGoName(), tdlibTypeProperty.ToGoType(), property.Name))
|
||||
countSimpleProperties++
|
||||
} else {
|
||||
buf.WriteString(fmt.Sprintf(" %s %s `json:\"%s\"`\n", tdlibTypeProperty.ToGoName(), "json.RawMessage", property.Name))
|
||||
if tdlibTypeProperty.IsList() {
|
||||
buf.WriteString(fmt.Sprintf(" %s %s `json:\"%s\"`\n", tdlibTypeProperty.ToGoName(), "[]json.RawMessage", property.Name))
|
||||
} else {
|
||||
buf.WriteString(fmt.Sprintf(" %s %s `json:\"%s\"`\n", tdlibTypeProperty.ToGoName(), "json.RawMessage", property.Name))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -145,7 +148,7 @@ func (*%s) GetType() string {
|
|||
for _, property := range typ.Properties {
|
||||
tdlibTypeProperty := TdlibTypeProperty(property.Name, property.Type, schema)
|
||||
|
||||
if !tdlibTypeProperty.IsClass() || tdlibTypeProperty.IsList() {
|
||||
if !tdlibTypeProperty.IsClass() {
|
||||
buf.WriteString(fmt.Sprintf(" %s.%s = tmp.%s\n", typ.Name, tdlibTypeProperty.ToGoName(), tdlibTypeProperty.ToGoName()))
|
||||
}
|
||||
}
|
||||
|
|
@ -163,6 +166,12 @@ func (*%s) GetType() string {
|
|||
|
||||
`, tdlibTypeProperty.ToGoName(), tdlibTypeProperty.ToGoType(), tdlibTypeProperty.ToGoName(), typ.Name, tdlibTypeProperty.ToGoName(), tdlibTypeProperty.ToGoName()))
|
||||
}
|
||||
if tdlibTypeProperty.IsClass() && tdlibTypeProperty.IsList() {
|
||||
buf.WriteString(fmt.Sprintf(` field%s, _ := UnmarshalListOf%s(tmp.%s)
|
||||
%s.%s = field%s
|
||||
|
||||
`, tdlibTypeProperty.ToGoName(), tdlibTypeProperty.GetClass().ToGoType(), tdlibTypeProperty.ToGoName(), typ.Name, tdlibTypeProperty.ToGoName(), tdlibTypeProperty.ToGoName()))
|
||||
}
|
||||
}
|
||||
|
||||
buf.WriteString(` return nil
|
||||
|
|
|
|||
|
|
@ -46,6 +46,23 @@ func GenerateUnmarshalers(schema *tlparser.Schema, packageName string) []byte {
|
|||
}
|
||||
|
||||
`)
|
||||
|
||||
buf.WriteString(fmt.Sprintf(`func UnmarshalListOf%s(dataList []json.RawMessage) ([]%s, error) {
|
||||
list := []%s{}
|
||||
|
||||
for _, data := range dataList {
|
||||
entity, err := Unmarshal%s(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
list = append(list, entity)
|
||||
}
|
||||
|
||||
return list, nil
|
||||
}
|
||||
|
||||
`, tdlibClass.ToGoType(), tdlibClass.ToGoType(), tdlibClass.ToGoType(), tdlibClass.ToGoType()))
|
||||
|
||||
}
|
||||
|
||||
for _, typ := range schema.Types {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue