go-tdlib/codegen/unmarshaler.go

120 lines
2.4 KiB
Go
Raw Permalink Normal View History

2018-08-30 14:55:42 +00:00
package codegen
import (
2018-10-23 12:49:10 +00:00
"bytes"
"fmt"
2022-01-26 20:48:37 +00:00
"github.com/c0re100/gotdlib/tlparser"
2018-08-30 14:55:42 +00:00
)
func GenerateUnmarshalers(schema *tlparser.Schema, packageName string) []byte {
2018-10-23 12:49:10 +00:00
buf := bytes.NewBufferString("")
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
buf.WriteString(fmt.Sprintf("%s\n\npackage %s\n\n", header, packageName))
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
buf.WriteString(`import (
2018-08-30 14:55:42 +00:00
"encoding/json"
"fmt"
)
`)
2018-10-23 12:49:10 +00:00
for _, class := range schema.Classes {
tdlibClass := TdlibClass(class.Name, schema)
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
buf.WriteString(fmt.Sprintf(`func Unmarshal%s(data json.RawMessage) (%s, error) {
2018-08-30 14:55:42 +00:00
var meta meta
err := json.Unmarshal(data, &meta)
if err != nil {
return nil, err
}
switch meta.Type {
`, tdlibClass.ToGoType(), tdlibClass.ToGoType()))
2018-10-23 12:49:10 +00:00
for _, subType := range tdlibClass.GetSubTypes() {
buf.WriteString(fmt.Sprintf(` case %s:
2018-08-30 14:55:42 +00:00
return Unmarshal%s(data)
`, subType.ToTypeConst(), subType.ToGoType()))
2018-10-23 12:49:10 +00:00
}
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
buf.WriteString(` default:
2018-08-30 14:55:42 +00:00
return nil, fmt.Errorf("Error unmarshaling. Unknown type: " + meta.Type)
}
}
`)
2021-01-18 14:16:52 +00:00
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()))
2018-10-23 12:49:10 +00:00
}
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
for _, typ := range schema.Types {
tdlibType := TdlibType(typ.Name, schema)
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
if tdlibType.IsList() || tdlibType.IsInternal() {
continue
}
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
buf.WriteString(fmt.Sprintf(`func Unmarshal%s(data json.RawMessage) (*%s, error) {
2018-08-30 14:55:42 +00:00
var resp %s
err := json.Unmarshal(data, &resp)
return &resp, err
}
`, tdlibType.ToGoType(), tdlibType.ToGoType(), tdlibType.ToGoType()))
2018-10-23 12:49:10 +00:00
}
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
buf.WriteString(`func UnmarshalType(data json.RawMessage) (Type, error) {
2018-08-30 14:55:42 +00:00
var meta meta
err := json.Unmarshal(data, &meta)
if err != nil {
return nil, err
}
switch meta.Type {
`)
2018-10-23 12:49:10 +00:00
for _, typ := range schema.Types {
tdlibType := TdlibType(typ.Name, schema)
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
if tdlibType.IsList() || tdlibType.IsInternal() {
continue
}
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
buf.WriteString(fmt.Sprintf(` case %s:
2018-08-30 14:55:42 +00:00
return Unmarshal%s(data)
`, tdlibType.ToTypeConst(), tdlibType.ToGoType()))
2018-10-23 12:49:10 +00:00
}
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
buf.WriteString(` default:
2018-08-30 14:55:42 +00:00
return nil, fmt.Errorf("Error unmarshaling. Unknown type: " + meta.Type)
}
}
`)
2018-10-23 12:49:10 +00:00
return buf.Bytes()
2018-08-30 14:55:42 +00:00
}