Skip to content

Commit

Permalink
add new flag + moved files to _php
Browse files Browse the repository at this point in the history
  • Loading branch information
Brat-vseznamus committed Dec 23, 2024
1 parent cf95826 commit 259cb2e
Show file tree
Hide file tree
Showing 14 changed files with 869 additions and 830 deletions.
4 changes: 4 additions & 0 deletions cmd/tlgen/main2.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ func parseFlags(opt *tlcodegen.Gen2Options) {
flag.StringVar(&opt.RootCPPNamespace, "cpp-namespace", "",
`c++ root namespace, separated by '::' if more than 1 element`)

// PHP
flag.BoolVar(&opt.AddFunctionBodies, "php-serialization-bodies", false,
`whether to generate body to write/read generated structs and functions`)

// .tlo
flag.StringVar(&opt.TLOPath, "tloPath", "",
"whether to serialize TL schema in binary form")
Expand Down
3 changes: 3 additions & 0 deletions internal/tlcodegen/tlgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ type Gen2Options struct {
RootCPPNamespace string
SeparateFiles bool

// PHP
AddFunctionBodies bool

// .tlo
TLOPath string
CanonicalFormPath string // combinators in canonical form, with comment of source schema file path
Expand Down
24 changes: 0 additions & 24 deletions internal/tlcodegen/type_rw_bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package tlcodegen

import (
"fmt"
"strings"
)

type TypeRWBool struct {
Expand Down Expand Up @@ -110,26 +109,3 @@ func (trw *TypeRWBool) typeJSONReadingCode(bytesVersion bool, directImports *Dir
func (trw *TypeRWBool) typeJSON2ReadingCode(bytesVersion bool, directImports *DirectImports, ins *InternalNamespace, jvalue string, val string, natArgs []string, ref bool) string {
return wrapLast(false, fmt.Sprintf("%sJson2ReadBool(%s, %s)", trw.wr.gen.InternalPrefix(), jvalue, addAmpersand(ref, val)))
}

func (trw *TypeRWBool) PhpClassName(withPath bool, bare bool) string {
return "boolean"
}

func (trw *TypeRWBool) PhpClassNameReplaced() bool {
return true
}

func (trw *TypeRWBool) PhpTypeName(withPath bool, bare bool) string {
return trw.PhpClassName(withPath, true)
}

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"
}

func (trw *TypeRWBool) PhpIterateReachableTypes(reachableTypes *map[*TypeRWWrapper]bool) {
}
29 changes: 29 additions & 0 deletions internal/tlcodegen/type_rw_bool_php.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tlcodegen

import (
"fmt"
"strings"
)

func (trw *TypeRWBool) PhpClassName(withPath bool, bare bool) string {
return "boolean"
}

func (trw *TypeRWBool) PhpClassNameReplaced() bool {
return true
}

func (trw *TypeRWBool) PhpTypeName(withPath bool, bare bool) string {
return trw.PhpClassName(withPath, true)
}

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"
}

func (trw *TypeRWBool) PhpIterateReachableTypes(reachableTypes *map[*TypeRWWrapper]bool) {
}
35 changes: 0 additions & 35 deletions internal/tlcodegen/type_rw_maybe.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package tlcodegen

import (
"fmt"
"strings"
)

type TypeRWMaybe struct {
Expand Down Expand Up @@ -119,37 +118,3 @@ func (trw *TypeRWMaybe) typeJSONReadingCode(bytesVersion bool, directImports *Di
func (trw *TypeRWMaybe) typeJSON2ReadingCode(bytesVersion bool, directImports *DirectImports, ins *InternalNamespace, jvalue string, val string, natArgs []string, ref bool) string {
return fmt.Sprintf("if err := %s.ReadJSON(legacyTypeNames, %s %s); err != nil { return err }", val, jvalue, joinWithCommas(natArgs))
}

func (trw *TypeRWMaybe) PhpClassName(withPath bool, bare bool) string {
target := trw.getInnerTarget()
return "maybe_" + target.t.trw.PhpClassName(withPath, target.bare)
}

func (trw *TypeRWMaybe) PhpClassNameReplaced() bool {
return true
}

func (trw *TypeRWMaybe) PhpTypeName(withPath bool, bare bool) string {
target := trw.getInnerTarget()
return target.t.trw.PhpTypeName(withPath, target.t.PHPIsBare()) + "|null"
}

func (trw *TypeRWMaybe) getInnerTarget() Field {
if inner, ok := trw.element.t.trw.(*TypeRWMaybe); ok {
return inner.getInnerTarget()
} else {
return trw.element
}
}

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"
}

func (trw *TypeRWMaybe) PhpIterateReachableTypes(reachableTypes *map[*TypeRWWrapper]bool) {
trw.element.t.PhpIterateReachableTypes(reachableTypes)
}
40 changes: 40 additions & 0 deletions internal/tlcodegen/type_rw_maybe_php.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package tlcodegen

import (
"fmt"
"strings"
)

func (trw *TypeRWMaybe) PhpClassName(withPath bool, bare bool) string {
target := trw.getInnerTarget()
return "maybe_" + target.t.trw.PhpClassName(withPath, target.bare)
}

func (trw *TypeRWMaybe) PhpClassNameReplaced() bool {
return true
}

func (trw *TypeRWMaybe) PhpTypeName(withPath bool, bare bool) string {
target := trw.getInnerTarget()
return target.t.trw.PhpTypeName(withPath, target.t.PHPIsBare()) + "|null"
}

func (trw *TypeRWMaybe) getInnerTarget() Field {
if inner, ok := trw.element.t.trw.(*TypeRWMaybe); ok {
return inner.getInnerTarget()
} else {
return trw.element
}
}

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"
}

func (trw *TypeRWMaybe) PhpIterateReachableTypes(reachableTypes *map[*TypeRWWrapper]bool) {
trw.element.t.PhpIterateReachableTypes(reachableTypes)
}
41 changes: 0 additions & 41 deletions internal/tlcodegen/type_rw_primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package tlcodegen
import (
"fmt"
"log"
"strings"
)

type TypeRWPrimitive struct {
Expand Down Expand Up @@ -171,43 +170,3 @@ func (trw *TypeRWPrimitive) typeJSON2ReadingCode(bytesVersion bool, directImport
func (trw *TypeRWPrimitive) GenerateCode(byteVersion bool, directImports *DirectImports) string {
return ""
}

func (trw *TypeRWPrimitive) PhpClassName(withPath bool, bare bool) string {
switch trw.goType {
case "int32", "int64", "uint32":
return "int"
case "string":
return "string"
case "float32", "float64":
return "float"
default:
return fmt.Sprintf("<? %s>", trw.tlType)
}
}

func (trw *TypeRWPrimitive) PhpClassNameReplaced() bool {
return true
}

func (trw *TypeRWPrimitive) PhpTypeName(withPath bool, bare bool) string {
return trw.PhpClassName(withPath, true)
}

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.0"
default:
return fmt.Sprintf("<? %s>", trw.tlType)
}
}

func (trw *TypeRWPrimitive) PhpIterateReachableTypes(reachableTypes *map[*TypeRWWrapper]bool) {}
46 changes: 46 additions & 0 deletions internal/tlcodegen/type_rw_primitive_php.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package tlcodegen

import (
"fmt"
"strings"
)

func (trw *TypeRWPrimitive) PhpClassName(withPath bool, bare bool) string {
switch trw.goType {
case "int32", "int64", "uint32":
return "int"
case "string":
return "string"
case "float32", "float64":
return "float"
default:
return fmt.Sprintf("<? %s>", trw.tlType)
}
}

func (trw *TypeRWPrimitive) PhpClassNameReplaced() bool {
return true
}

func (trw *TypeRWPrimitive) PhpTypeName(withPath bool, bare bool) string {
return trw.PhpClassName(withPath, true)
}

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.0"
default:
return fmt.Sprintf("<? %s>", trw.tlType)
}
}

func (trw *TypeRWPrimitive) PhpIterateReachableTypes(reachableTypes *map[*TypeRWWrapper]bool) {}
Loading

0 comments on commit 259cb2e

Please sign in to comment.