Skip to content

Commit

Permalink
add non function union body
Browse files Browse the repository at this point in the history
  • Loading branch information
Brat-vseznamus committed Dec 10, 2024
1 parent a77527d commit e71c8f6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
7 changes: 4 additions & 3 deletions internal/tlcodegen/tlgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -1217,13 +1217,14 @@ func GenerateCode(tl tlast.TL, options Gen2Options) (*Gen2, error) {
return nil, err
}
case "php":
if gen.copyrightText == "" {
gen.copyrightText = `
/**
{
// TODO ADD FEATURE TO CHANGE IT
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 {
Expand Down
18 changes: 15 additions & 3 deletions internal/tlcodegen/type_rw_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,8 +603,10 @@ func (trw *TypeRWStruct) PhpTypeName(withPath bool) string {
}

func (trw *TypeRWStruct) PhpGenerateCode(code *strings.Builder, bytes bool) error {
code.WriteString(`use VK\TL;
if isUsingTLImport(trw) {
code.WriteString("\nuse VK\\TL;\n")
}
code.WriteString(`
/**
* @kphp-tl-class
*/
Expand Down Expand Up @@ -692,7 +694,7 @@ func (trw *TypeRWStruct) PhpGenerateCode(code *strings.Builder, bytes bool) erro

code.WriteString(") {\n")
for _, f := range necessaryFieldsInConstructor {
code.WriteString(fmt.Sprintf(" $this->$%[1]s = $%[1]s;\n", f.originalName))
code.WriteString(fmt.Sprintf(" $this->%[1]s = $%[1]s;\n", f.originalName))
}
code.WriteString(" }\n")

Expand Down Expand Up @@ -764,6 +766,16 @@ func (trw *TypeRWStruct) PhpGenerateCode(code *strings.Builder, bytes bool) erro
return nil
}

func isUsingTLImport(trw *TypeRWStruct) bool {
for _, field := range trw.Fields {
fieldType, _ := fieldTypeAndDefaultValue(field)
if strings.Contains(fieldType, "TL\\") {
return true
}
}
return false
}

func fieldTypeAndDefaultValue(f Field) (string, string) {
fieldType := f.t.trw.PhpTypeName(true)
defaultValue := f.t.trw.PhpDefaultValue()
Expand Down
24 changes: 24 additions & 0 deletions internal/tlcodegen/type_rw_union.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,30 @@ func (trw *TypeRWUnion) PhpTypeName(withPath bool) string {
}

func (trw *TypeRWUnion) PhpGenerateCode(code *strings.Builder, bytes bool) error {
classes := make([]string, len(trw.Fields))
for i, field := range trw.Fields {
classes[i] = fmt.Sprintf("%s::class", field.t.trw.PhpClassName(true))
}

code.WriteString(`use VK\TL;
/**
* @kphp-tl-class
*/
`)
code.WriteString(fmt.Sprintf(
`interface %[1]s {
/** Allows kphp implicitly load all available constructors */
const CONSTRUCTORS = [
%[2]s
];
}
`,
trw.PhpClassName(false),
strings.Join(classes, ",\n "),
))
return nil
}

Expand Down

0 comments on commit e71c8f6

Please sign in to comment.