go-tdlib/codegen/type.go

186 lines
5 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 GenerateTypes(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"
)
`)
2018-10-23 12:49:10 +00:00
buf.WriteString("const (\n")
for _, entity := range schema.Classes {
tdlibClass := TdlibClass(entity.Name, schema)
buf.WriteString(fmt.Sprintf(" %s = %q\n", tdlibClass.ToClassConst(), entity.Name))
}
for _, entity := range schema.Types {
tdlibType := TdlibType(entity.Name, schema)
if tdlibType.IsInternal() || tdlibType.HasClass() {
continue
}
buf.WriteString(fmt.Sprintf(" %s = %q\n", tdlibType.ToClassConst(), entity.Class))
}
buf.WriteString(")")
buf.WriteString("\n\n")
buf.WriteString("const (\n")
for _, entity := range schema.Types {
tdlibType := TdlibType(entity.Name, schema)
if tdlibType.IsInternal() {
continue
}
buf.WriteString(fmt.Sprintf(" %s = %q\n", tdlibType.ToTypeConst(), entity.Name))
}
buf.WriteString(")")
buf.WriteString("\n\n")
for _, class := range schema.Classes {
tdlibClass := TdlibClass(class.Name, schema)
buf.WriteString(fmt.Sprintf(`// %s
2018-08-30 14:55:42 +00:00
type %s interface {
%sType() string
}
`, class.Description, 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)
if tdlibType.IsInternal() {
continue
}
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
buf.WriteString("// " + typ.Description + "\n")
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
if len(typ.Properties) > 0 {
buf.WriteString(`type ` + tdlibType.ToGoType() + ` struct {
2018-08-30 14:55:42 +00:00
meta
`)
2018-10-23 12:49:10 +00:00
for _, property := range typ.Properties {
tdlibTypeProperty := TdlibTypeProperty(property.Name, property.Type, schema)
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
buf.WriteString(fmt.Sprintf(" // %s\n", property.Description))
buf.WriteString(fmt.Sprintf(" %s %s `json:\"%s\"`\n", tdlibTypeProperty.ToGoName(), tdlibTypeProperty.ToGoType(), property.Name))
}
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
buf.WriteString("}\n\n")
} else {
buf.WriteString(`type ` + tdlibType.ToGoType() + ` struct{
2018-08-30 14:55:42 +00:00
meta
}
`)
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(fmt.Sprintf(`func (entity *%s) MarshalJSON() ([]byte, error) {
2018-08-30 14:55:42 +00:00
entity.meta.Type = entity.GetType()
type stub %s
return json.Marshal((*stub)(entity))
}
`, tdlibType.ToGoType(), tdlibType.ToGoType()))
2018-10-23 12:49:10 +00:00
buf.WriteString(fmt.Sprintf(`func (*%s) GetClass() string {
2018-08-30 14:55:42 +00:00
return %s
}
func (*%s) GetType() string {
return %s
}
`, tdlibType.ToGoType(), tdlibType.ToClassConst(), tdlibType.ToGoType(), tdlibType.ToTypeConst()))
2018-10-23 12:49:10 +00:00
if tdlibType.HasClass() {
tdlibClass := TdlibClass(tdlibType.GetClass().Name, schema)
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
buf.WriteString(fmt.Sprintf(`func (*%s) %sType() string {
2018-08-30 14:55:42 +00:00
return %s
}
`, tdlibType.ToGoType(), tdlibClass.ToGoType(), tdlibType.ToTypeConst()))
2018-10-23 12:49:10 +00:00
}
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
if tdlibType.HasClassProperties() {
buf.WriteString(fmt.Sprintf(`func (%s *%s) UnmarshalJSON(data []byte) error {
2018-08-30 14:55:42 +00:00
var tmp struct {
`, typ.Name, tdlibType.ToGoType()))
2018-10-23 12:49:10 +00:00
var countSimpleProperties int
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
for _, property := range typ.Properties {
tdlibTypeProperty := TdlibTypeProperty(property.Name, property.Type, schema)
2018-08-30 14:55:42 +00:00
2021-01-18 14:16:52 +00:00
if !tdlibTypeProperty.IsClass() {
2018-10-23 12:49:10 +00:00
buf.WriteString(fmt.Sprintf(" %s %s `json:\"%s\"`\n", tdlibTypeProperty.ToGoName(), tdlibTypeProperty.ToGoType(), property.Name))
countSimpleProperties++
} else {
2021-01-18 14:16:52 +00:00
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))
}
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(` }
2018-08-30 14:55:42 +00:00
err := json.Unmarshal(data, &tmp)
if err != nil {
return err
}
`)
2018-10-23 12:49:10 +00:00
for _, property := range typ.Properties {
tdlibTypeProperty := TdlibTypeProperty(property.Name, property.Type, schema)
2018-08-30 14:55:42 +00:00
2021-01-18 14:16:52 +00:00
if !tdlibTypeProperty.IsClass() {
2018-10-23 12:49:10 +00:00
buf.WriteString(fmt.Sprintf(" %s.%s = tmp.%s\n", typ.Name, tdlibTypeProperty.ToGoName(), tdlibTypeProperty.ToGoName()))
}
}
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
if countSimpleProperties > 0 {
buf.WriteString("\n")
}
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
for _, property := range typ.Properties {
tdlibTypeProperty := TdlibTypeProperty(property.Name, property.Type, schema)
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
if tdlibTypeProperty.IsClass() && !tdlibTypeProperty.IsList() {
buf.WriteString(fmt.Sprintf(` field%s, _ := Unmarshal%s(tmp.%s)
2018-08-30 14:55:42 +00:00
%s.%s = field%s
`, tdlibTypeProperty.ToGoName(), tdlibTypeProperty.ToGoType(), tdlibTypeProperty.ToGoName(), typ.Name, tdlibTypeProperty.ToGoName(), tdlibTypeProperty.ToGoName()))
2018-10-23 12:49:10 +00:00
}
2021-01-18 14:16:52 +00:00
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()))
}
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(` return nil
2018-08-30 14:55:42 +00:00
}
`)
2018-10-23 12:49:10 +00:00
}
}
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
return buf.Bytes()
2018-08-30 14:55:42 +00:00
}