mirror of
https://github.com/c0re100/gotdlib.git
synced 2026-02-21 20:20:17 +01:00
go fmt formatted project
This commit is contained in:
parent
e292b9559e
commit
e253c67052
15 changed files with 884 additions and 882 deletions
|
|
@ -1,83 +1,83 @@
|
|||
package codegen
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"bytes"
|
||||
"fmt"
|
||||
|
||||
"github.com/zelenin/go-tdlib/tlparser"
|
||||
"github.com/zelenin/go-tdlib/tlparser"
|
||||
)
|
||||
|
||||
func GenerateFunctions(schema *tlparser.Schema, packageName string) []byte {
|
||||
buf := bytes.NewBufferString("")
|
||||
buf := bytes.NewBufferString("")
|
||||
|
||||
buf.WriteString(fmt.Sprintf("%s\n\npackage %s\n\n", header, packageName))
|
||||
buf.WriteString(fmt.Sprintf("%s\n\npackage %s\n\n", header, packageName))
|
||||
|
||||
buf.WriteString(`import (
|
||||
buf.WriteString(`import (
|
||||
"errors"
|
||||
)`)
|
||||
|
||||
buf.WriteString("\n")
|
||||
buf.WriteString("\n")
|
||||
|
||||
for _, function := range schema.Functions {
|
||||
tdlibFunction := TdlibFunction(function.Name, schema)
|
||||
tdlibFunctionReturn := TdlibFunctionReturn(function.Class, schema)
|
||||
for _, function := range schema.Functions {
|
||||
tdlibFunction := TdlibFunction(function.Name, schema)
|
||||
tdlibFunctionReturn := TdlibFunctionReturn(function.Class, schema)
|
||||
|
||||
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)
|
||||
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)
|
||||
|
||||
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")
|
||||
}
|
||||
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")
|
||||
}
|
||||
|
||||
buf.WriteString("\n")
|
||||
buf.WriteString("// " + function.Description)
|
||||
buf.WriteString("\n")
|
||||
buf.WriteString("\n")
|
||||
buf.WriteString("// " + function.Description)
|
||||
buf.WriteString("\n")
|
||||
|
||||
requestArgument := ""
|
||||
if len(function.Properties) > 0 {
|
||||
requestArgument = fmt.Sprintf("req *%sRequest", tdlibFunction.ToGoName())
|
||||
}
|
||||
requestArgument := ""
|
||||
if len(function.Properties) > 0 {
|
||||
requestArgument = fmt.Sprintf("req *%sRequest", tdlibFunction.ToGoName())
|
||||
}
|
||||
|
||||
buf.WriteString(fmt.Sprintf("func (client *Client) %s(%s) (%s, error) {\n", tdlibFunction.ToGoName(), requestArgument, tdlibFunctionReturn.ToGoReturn()))
|
||||
buf.WriteString(fmt.Sprintf("func (client *Client) %s(%s) (%s, error) {\n", tdlibFunction.ToGoName(), requestArgument, tdlibFunctionReturn.ToGoReturn()))
|
||||
|
||||
sendMethod := "Send"
|
||||
if function.IsSynchronous {
|
||||
sendMethod = "jsonClient.Execute"
|
||||
}
|
||||
sendMethod := "Send"
|
||||
if function.IsSynchronous {
|
||||
sendMethod = "jsonClient.Execute"
|
||||
}
|
||||
|
||||
if len(function.Properties) > 0 {
|
||||
buf.WriteString(fmt.Sprintf(` result, err := client.%s(Request{
|
||||
if len(function.Properties) > 0 {
|
||||
buf.WriteString(fmt.Sprintf(` result, err := client.%s(Request{
|
||||
meta: meta{
|
||||
Type: "%s",
|
||||
},
|
||||
Data: map[string]interface{}{
|
||||
`, sendMethod, function.Name))
|
||||
|
||||
for _, property := range function.Properties {
|
||||
tdlibTypeProperty := TdlibTypeProperty(property.Name, property.Type, schema)
|
||||
for _, property := range function.Properties {
|
||||
tdlibTypeProperty := TdlibTypeProperty(property.Name, property.Type, schema)
|
||||
|
||||
buf.WriteString(fmt.Sprintf(" \"%s\": req.%s,\n", property.Name, tdlibTypeProperty.ToGoName()))
|
||||
}
|
||||
buf.WriteString(fmt.Sprintf(" \"%s\": req.%s,\n", property.Name, tdlibTypeProperty.ToGoName()))
|
||||
}
|
||||
|
||||
buf.WriteString(` },
|
||||
buf.WriteString(` },
|
||||
})
|
||||
`)
|
||||
} else {
|
||||
buf.WriteString(fmt.Sprintf(` result, err := client.%s(Request{
|
||||
} else {
|
||||
buf.WriteString(fmt.Sprintf(` result, err := client.%s(Request{
|
||||
meta: meta{
|
||||
Type: "%s",
|
||||
},
|
||||
Data: map[string]interface{}{},
|
||||
})
|
||||
`, sendMethod, function.Name))
|
||||
}
|
||||
}
|
||||
|
||||
buf.WriteString(` if err != nil {
|
||||
buf.WriteString(` if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
@ -87,29 +87,29 @@ func GenerateFunctions(schema *tlparser.Schema, packageName string) []byte {
|
|||
|
||||
`)
|
||||
|
||||
if tdlibFunctionReturn.IsClass() {
|
||||
buf.WriteString(" switch result.Type {\n")
|
||||
if tdlibFunctionReturn.IsClass() {
|
||||
buf.WriteString(" switch result.Type {\n")
|
||||
|
||||
for _, subType := range tdlibFunctionReturn.GetClass().GetSubTypes() {
|
||||
buf.WriteString(fmt.Sprintf(` case %s:
|
||||
for _, subType := range tdlibFunctionReturn.GetClass().GetSubTypes() {
|
||||
buf.WriteString(fmt.Sprintf(` case %s:
|
||||
return Unmarshal%s(result.Data)
|
||||
|
||||
`, subType.ToTypeConst(), subType.ToGoType()))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
buf.WriteString(` default:
|
||||
buf.WriteString(` default:
|
||||
return nil, errors.New("invalid type")
|
||||
`)
|
||||
|
||||
buf.WriteString(" }\n")
|
||||
} else {
|
||||
buf.WriteString(fmt.Sprintf(` return Unmarshal%s(result.Data)
|
||||
buf.WriteString(" }\n")
|
||||
} else {
|
||||
buf.WriteString(fmt.Sprintf(` return Unmarshal%s(result.Data)
|
||||
`, tdlibFunctionReturn.ToGoType()))
|
||||
}
|
||||
}
|
||||
|
||||
buf.WriteString("}\n")
|
||||
}
|
||||
buf.WriteString("}\n")
|
||||
}
|
||||
|
||||
return buf.Bytes()
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue