Skip to content

Commit

Permalink
add non function struct body
Browse files Browse the repository at this point in the history
  • Loading branch information
Brat-vseznamus committed Dec 10, 2024
1 parent ffe767e commit a77527d
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 11 deletions.
9 changes: 9 additions & 0 deletions internal/tlcodegen/tlgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,15 @@ func GenerateCode(tl tlast.TL, options Gen2Options) (*Gen2, error) {
return nil, err
}
case "php":
if gen.copyrightText == "" {
gen.copyrightText = `
/**
* AUTOGENERATED, DO NOT EDIT! If you want to modify it, check tl schema.
*
* This autogenerated code represents tl class for typed RPC API.
*/
`
}
if err := gen.generateCodePHP(generateByteVersions); err != nil {
return nil, err
}
Expand Down
34 changes: 28 additions & 6 deletions internal/tlcodegen/tlgen_php.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tlcodegen

import (
"fmt"
"path/filepath"
"reflect"
"strings"
)
Expand All @@ -26,14 +27,29 @@ func (gen *Gen2) generateCodePHP(generateByteVersions []string) error {
// select files where to write code
gen.PhpChoosePaths()

createdTypes := make(map[string]bool)

for _, wrapper := range gen.generatedTypesList {
if wrapper.PHPTypePath() == "" {
continue
}
if wrapper.PHPIsPrimitiveType() {
continue
}
if createdTypes[wrapper.trw.PhpClassName(true)] {
continue
}
var code strings.Builder
// add
// add start symbol
code.WriteString(PHPFileStart)
code.WriteString("\n")
// add copyright text
code.WriteString(gen.copyrightText)
code.WriteString(fmt.Sprintf("namespace VK\\%s;\n", strings.Join(wrapper.PHPTypePathElements(), "\\")))

wrapper.PHPGenerateCode(&code, true)
if err := wrapper.PHPGenerateCode(&code, true); err != nil {
return err
}

fmt.Printf("TL[%[1]s] = Go {%[2]s, %[4]s} -> PHP {%[3]s, %[5]s}\n",
wrapper.tlName.String(),
Expand All @@ -43,10 +59,16 @@ func (gen *Gen2) generateCodePHP(generateByteVersions []string) error {
wrapper.trw.PhpTypeName(true),
)

//filepathName := wrapper.phpInfo.FileName
//if err := gen.addCodeFile(filepathName, code.String()); err != nil {
// return err
//}
fmt.Printf("Core[%s] = %s, %s\n", wrapper.goGlobalName, wrapper.PHPGenCoreType().goGlobalName, reflect.TypeOf(wrapper.PHPGenCoreType().trw))

filepathParts := []string{"VK"}
filepathParts = append(filepathParts, wrapper.PHPTypePathElements()...)
filepathParts = append(filepathParts, fmt.Sprintf("%s.php", wrapper.trw.PhpClassName(false)))
filepathName := filepath.Join(filepathParts...)
if err := gen.addCodeFile(filepathName, code.String()); err != nil {
return err
}
createdTypes[wrapper.trw.PhpClassName(true)] = true
}
return nil
}
Expand Down
53 changes: 48 additions & 5 deletions internal/tlcodegen/type_rw.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,15 +490,15 @@ func (w *TypeRWWrapper) IsTrueType() bool {
return len(structElement.Fields) == 0
}

func (w *TypeRWWrapper) PHPGenerateCode(code *strings.Builder, bytes bool) {

func (w *TypeRWWrapper) PHPGenerateCode(code *strings.Builder, bytes bool) error {
return w.trw.PhpGenerateCode(code, bytes)
}

func (w *TypeRWWrapper) PHPTypePath() string {
func (w *TypeRWWrapper) PHPTypePathElements() []string {
_, isStruct := w.trw.(*TypeRWStruct)
_, isUnion := w.trw.(*TypeRWUnion)
if !(isStruct || isUnion) {
return ""
return nil
}

category := "Types"
Expand All @@ -509,7 +509,48 @@ func (w *TypeRWWrapper) PHPTypePath() string {
if w.tlName.Namespace != "" {
group = w.tlName.Namespace
}
return fmt.Sprintf("TL\\%[1]s\\%[2]s\\", group, category)
return []string{"TL", group, category}
}

func (w *TypeRWWrapper) PHPDefaultValue() string {
core := w.PHPGenCoreType()
return core.trw.PhpDefaultValue()
}

func (w *TypeRWWrapper) PHPTypePath() string {
path := w.PHPTypePathElements()
if path == nil {
return ""
} else {
return strings.Join(path, "\\") + "\\"
}
}

func (w *TypeRWWrapper) PHPGenCoreType() *TypeRWWrapper {
if w.unionParent == nil {
struct_, isStruct := w.trw.(*TypeRWStruct)
if isStruct && len(struct_.Fields) == 1 && struct_.ResultType == nil {
return struct_.Fields[0].t.PHPGenCoreType()
}
}
return w
}

func (w *TypeRWWrapper) PHPIsPrimitiveType() bool {
core := w.PHPGenCoreType()
if _, isPrimitive := core.trw.(*TypeRWPrimitive); isPrimitive {
return true
}
if struct_, isStruct := core.trw.(*TypeRWStruct); isStruct {
isDict, _, _, valueType := isDictionaryElement(struct_.wr)
if isDict {
return valueType.t.PHPIsPrimitiveType()
}
}
if _, isBrackets := core.trw.(*TypeRWBrackets); isBrackets {
return true
}
return false
}

func (w *TypeRWWrapper) CPPFillRecursiveChildren(visitedNodes map[*TypeRWWrapper]bool) {
Expand Down Expand Up @@ -751,6 +792,8 @@ outer:
type TypeRWPHPData interface {
PhpClassName(withPath bool) string
PhpTypeName(withPath bool) string
PhpGenerateCode(code *strings.Builder, bytes bool) error
PhpDefaultValue() string
}

// TODO remove skipAlias after we start generating go code like we do for C++
Expand Down
9 changes: 9 additions & 0 deletions internal/tlcodegen/type_rw_bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package tlcodegen

import (
"fmt"
"strings"
)

type TypeRWBool struct {
Expand Down Expand Up @@ -117,3 +118,11 @@ func (trw *TypeRWBool) PhpClassName(withPath bool) string {
func (trw *TypeRWBool) PhpTypeName(withPath bool) string {
return trw.PhpClassName(withPath)
}

func (trw *TypeRWBool) PhpGenerateCode(code *strings.Builder, bytes bool) error {
return fmt.Errorf("boolean doesn't have php code")
}

func (trw *TypeRWBool) PhpDefaultValue() string {
return "false"
}
9 changes: 9 additions & 0 deletions internal/tlcodegen/type_rw_maybe.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package tlcodegen

import (
"fmt"
"strings"
)

type TypeRWMaybe struct {
Expand Down Expand Up @@ -136,3 +137,11 @@ func (trw *TypeRWMaybe) getInnerTarget() *TypeRWWrapper {
return trw.element.t
}
}

func (trw *TypeRWMaybe) PhpGenerateCode(code *strings.Builder, bytes bool) error {
return fmt.Errorf("maybe doesn't have php code")
}

func (trw *TypeRWMaybe) PhpDefaultValue() string {
return "null"
}
18 changes: 18 additions & 0 deletions internal/tlcodegen/type_rw_primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package tlcodegen
import (
"fmt"
"log"
"strings"
)

type TypeRWPrimitive struct {
Expand Down Expand Up @@ -187,3 +188,20 @@ func (trw *TypeRWPrimitive) PhpClassName(withPath bool) string {
func (trw *TypeRWPrimitive) PhpTypeName(withPath bool) string {
return trw.PhpClassName(withPath)
}

func (trw *TypeRWPrimitive) PhpGenerateCode(code *strings.Builder, bytes bool) error {
return fmt.Errorf("primitives don't have php code")
}

func (trw *TypeRWPrimitive) PhpDefaultValue() string {
switch trw.goType {
case "int32", "int64", "uint32":
return "0"
case "string":
return "\"\""
case "float32", "float64":
return "0"
default:
return fmt.Sprintf("<? %s>", trw.tlType)
}
}
Loading

0 comments on commit a77527d

Please sign in to comment.