go-tdlib/codegen/function.go

116 lines
2.9 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"
2018-10-16 20:32:57 +00:00
2022-01-26 20:48:37 +00:00
"github.com/c0re100/gotdlib/tlparser"
2018-08-30 14:55:42 +00:00
)
func GenerateFunctions(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
"errors"
)`)
2018-10-23 12:49:10 +00:00
buf.WriteString("\n")
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
for _, function := range schema.Functions {
tdlibFunction := TdlibFunction(function.Name, schema)
tdlibFunctionReturn := TdlibFunctionReturn(function.Class, schema)
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
if len(function.Properties) > 0 {
buf.WriteString("\n")
buf.WriteString(fmt.Sprintf("type %sRequest struct { \n", tdlibFunction.ToGoName()))
for _, property := range function.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))
}
buf.WriteString("}\n")
}
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
buf.WriteString("\n")
buf.WriteString("// " + function.Description)
buf.WriteString("\n")
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
requestArgument := ""
if len(function.Properties) > 0 {
requestArgument = fmt.Sprintf("req *%sRequest", tdlibFunction.ToGoName())
}
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
buf.WriteString(fmt.Sprintf("func (client *Client) %s(%s) (%s, error) {\n", tdlibFunction.ToGoName(), requestArgument, tdlibFunctionReturn.ToGoReturn()))
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
sendMethod := "Send"
if function.IsSynchronous {
sendMethod = "jsonClient.Execute"
}
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
if len(function.Properties) > 0 {
buf.WriteString(fmt.Sprintf(` result, err := client.%s(Request{
2018-08-30 14:55:42 +00:00
meta: meta{
Type: "%s",
},
Data: map[string]interface{}{
`, sendMethod, function.Name))
2018-10-23 12:49:10 +00:00
for _, property := range function.Properties {
tdlibTypeProperty := TdlibTypeProperty(property.Name, property.Type, schema)
2018-10-16 20:32:57 +00:00
2018-10-23 12:49:10 +00:00
buf.WriteString(fmt.Sprintf(" \"%s\": req.%s,\n", property.Name, tdlibTypeProperty.ToGoName()))
}
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
})
`)
2018-10-23 12:49:10 +00:00
} else {
buf.WriteString(fmt.Sprintf(` result, err := client.%s(Request{
2018-08-30 14:55:42 +00:00
meta: meta{
Type: "%s",
},
Data: map[string]interface{}{},
})
`, sendMethod, function.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(` if err != nil {
2018-08-30 14:55:42 +00:00
return nil, err
}
if result.Type == "error" {
return nil, buildResponseError(result.Data)
}
`)
2018-10-23 12:49:10 +00:00
if tdlibFunctionReturn.IsClass() {
buf.WriteString(" switch result.Type {\n")
2018-08-30 14:55:42 +00:00
2018-10-23 12:49:10 +00:00
for _, subType := range tdlibFunctionReturn.GetClass().GetSubTypes() {
buf.WriteString(fmt.Sprintf(` case %s:
2018-08-30 14:55:42 +00:00
return Unmarshal%s(result.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, errors.New("invalid type")
`)
2018-10-23 12:49:10 +00:00
buf.WriteString(" }\n")
} else {
buf.WriteString(fmt.Sprintf(` return Unmarshal%s(result.Data)
2018-08-30 14:55:42 +00:00
`, tdlibFunctionReturn.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("}\n")
}
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
}