mirror of
https://github.com/c0re100/gotdlib.git
synced 2026-02-21 20:20:17 +01:00
init
This commit is contained in:
commit
3b23208ee0
23 changed files with 49288 additions and 0 deletions
102
codegen/unmarshaler.go
Normal file
102
codegen/unmarshaler.go
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
package codegen
|
||||
|
||||
import (
|
||||
"github.com/zelenin/go-tdlib/tlparser"
|
||||
"fmt"
|
||||
"bytes"
|
||||
)
|
||||
|
||||
func GenerateUnmarshalers(schema *tlparser.Schema, packageName string) []byte {
|
||||
buf := bytes.NewBufferString("")
|
||||
|
||||
buf.WriteString(fmt.Sprintf("%s\n\npackage %s\n\n", header, packageName))
|
||||
|
||||
buf.WriteString(`import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
`)
|
||||
|
||||
for _, class := range schema.Classes {
|
||||
tdlibClass := TdlibClass(class.Name, schema)
|
||||
|
||||
buf.WriteString(fmt.Sprintf(`func Unmarshal%s(data json.RawMessage) (%s, error) {
|
||||
var meta meta
|
||||
|
||||
err := json.Unmarshal(data, &meta)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch meta.Type {
|
||||
`, tdlibClass.ToGoType(), tdlibClass.ToGoType()))
|
||||
|
||||
for _, subType := range tdlibClass.GetSubTypes() {
|
||||
buf.WriteString(fmt.Sprintf(` case %s:
|
||||
return Unmarshal%s(data)
|
||||
|
||||
`, subType.ToTypeConst(), subType.ToGoType()))
|
||||
|
||||
}
|
||||
|
||||
buf.WriteString(` default:
|
||||
return nil, fmt.Errorf("Error unmarshaling. Unknown type: " + meta.Type)
|
||||
}
|
||||
}
|
||||
|
||||
`)
|
||||
}
|
||||
|
||||
for _, typ := range schema.Types {
|
||||
tdlibType := TdlibType(typ.Name, schema)
|
||||
|
||||
if tdlibType.IsList() || tdlibType.IsInternal() {
|
||||
continue
|
||||
}
|
||||
|
||||
buf.WriteString(fmt.Sprintf(`func Unmarshal%s(data json.RawMessage) (*%s, error) {
|
||||
var resp %s
|
||||
|
||||
err := json.Unmarshal(data, &resp)
|
||||
|
||||
return &resp, err
|
||||
}
|
||||
|
||||
`, tdlibType.ToGoType(), tdlibType.ToGoType(), tdlibType.ToGoType()))
|
||||
|
||||
}
|
||||
|
||||
buf.WriteString(`func UnmarshalType(data json.RawMessage) (Type, error) {
|
||||
var meta meta
|
||||
|
||||
err := json.Unmarshal(data, &meta)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch meta.Type {
|
||||
`)
|
||||
|
||||
for _, typ := range schema.Types {
|
||||
tdlibType := TdlibType(typ.Name, schema)
|
||||
|
||||
if tdlibType.IsList() || tdlibType.IsInternal() {
|
||||
continue
|
||||
}
|
||||
|
||||
buf.WriteString(fmt.Sprintf(` case %s:
|
||||
return Unmarshal%s(data)
|
||||
|
||||
`, tdlibType.ToTypeConst(), tdlibType.ToGoType()))
|
||||
|
||||
}
|
||||
|
||||
buf.WriteString(` default:
|
||||
return nil, fmt.Errorf("Error unmarshaling. Unknown type: " + meta.Type)
|
||||
}
|
||||
}
|
||||
`)
|
||||
|
||||
return buf.Bytes()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue