go-tdlib/codegen/unmarshaler.go

103 lines
2 KiB
Go
Raw Normal View History

2018-08-30 14:55:42 +00:00
package codegen
import (
2018-10-23 12:49:10 +00:00
"bytes"
"fmt"
"github.com/zelenin/go-tdlib/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)
}
}
`)
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
}