diff --git a/internal/tlcodegen/type_rw.go b/internal/tlcodegen/type_rw.go index 9428806c..753c86cc 100644 --- a/internal/tlcodegen/type_rw.go +++ b/internal/tlcodegen/type_rw.go @@ -907,6 +907,10 @@ func addAsterisk(ref bool, val string) string { return ifString(ref, "*"+val, val) } +func addAsteriskAndBrackets(ref bool, val string) string { + return ifString(ref, "(*"+val+")", val) +} + func wrapLast(last bool, code string) string { return ifString(last, "return "+code+"", "if err := "+code+"; err != nil { return err }") } diff --git a/internal/tlcodegen/type_rw_maybe_cpp.go b/internal/tlcodegen/type_rw_maybe_cpp.go index 28cb3f37..bff8d2a2 100644 --- a/internal/tlcodegen/type_rw_maybe_cpp.go +++ b/internal/tlcodegen/type_rw_maybe_cpp.go @@ -142,7 +142,7 @@ bool %[6]s::%[1]sWriteBoxed(::basictl::tl_ostream & s, const %[2]s& item%[3]s) { func() string { indent := 1 s := "" - emptyCondition := trw.element.t.trw.CPPTypeJSONEmptyCondition(bytesVersion, "*item", false, nil) + emptyCondition := trw.element.t.trw.CPPTypeJSONEmptyCondition(bytesVersion, "(*item)", false, nil) if emptyCondition != "" { s += fmt.Sprintf("\tif(%s) {\n\t", emptyCondition) indent = 2 diff --git a/internal/tlcodegen/type_rw_primitive_cpp.go b/internal/tlcodegen/type_rw_primitive_cpp.go index 0c41ffc7..e13d1e3d 100644 --- a/internal/tlcodegen/type_rw_primitive_cpp.go +++ b/internal/tlcodegen/type_rw_primitive_cpp.go @@ -13,7 +13,7 @@ import ( func (trw *TypeRWPrimitive) CPPTypeJSONEmptyCondition(bytesVersion bool, val string, ref bool, deps []string) string { if trw.tlType == "string" { - return fmt.Sprintf("%s.size() != 0", addAsterisk(ref, val)) + return fmt.Sprintf("%s.size() != 0", addAsteriskAndBrackets(ref, val)) } return fmt.Sprintf("%s != 0", addAsterisk(ref, val)) } diff --git a/internal/tlcodegen/type_rw_struct_cpp.go b/internal/tlcodegen/type_rw_struct_cpp.go index 1ca973b6..b720b18e 100644 --- a/internal/tlcodegen/type_rw_struct_cpp.go +++ b/internal/tlcodegen/type_rw_struct_cpp.go @@ -504,6 +504,18 @@ func (trw *TypeRWStruct) CPPWriteFields(bytesVersion bool) string { return s.String() } +func countFields(fields []Field) (count int) { + for _, field := range fields { + if field.t.IsTrueType() { + if field.fieldMask == nil || !(field.fieldMask.isField || field.fieldMask.isArith) { + continue + } + } + count++ + } + return +} + func (trw *TypeRWStruct) CPPWriteJsonFields(bytesVersion bool) string { var s strings.Builder if trw.isTypeDef() { @@ -517,8 +529,12 @@ func (trw *TypeRWStruct) CPPWriteJsonFields(bytesVersion bool) string { return " s << \"true\";\n" } - s.WriteString(` auto add_comma = false; - s << "{"; + fieldsCount := countFields(trw.Fields) + if fieldsCount > 1 { + s.WriteString(` auto add_comma = false; +`) + } + s.WriteString(` s << "{"; `) for i, field := range trw.Fields { if field.t.IsTrueType() { @@ -545,8 +561,10 @@ func (trw *TypeRWStruct) CPPWriteJsonFields(bytesVersion bool) string { strings.Repeat("\t", indent+1), )) } - s.WriteString(strings.Repeat("\t", indent+1)) - s.WriteString("add_comma = true;\n") + if fieldsCount > 1 { + s.WriteString(strings.Repeat("\t", indent+1)) + s.WriteString("add_comma = true;\n") + } s.WriteString(fmt.Sprintf(`%ss << "\"%s\":"; `, strings.Repeat("\t", indent+1), diff --git a/internal/tlcodegen/type_rw_tuple_cpp.go b/internal/tlcodegen/type_rw_tuple_cpp.go index 2fe49595..2d5502dd 100644 --- a/internal/tlcodegen/type_rw_tuple_cpp.go +++ b/internal/tlcodegen/type_rw_tuple_cpp.go @@ -13,10 +13,10 @@ import ( func (trw *TypeRWBrackets) CPPTypeJSONEmptyCondition(bytesVersion bool, val string, ref bool, deps []string) string { if trw.dictLike || trw.vectorLike || trw.dynamicSize { - if trw.dynamicSize { - return fmt.Sprintf("(%s.size() != 0) || (%s != 0)", addAsterisk(ref, val), deps[0]) + if trw.dynamicSize && len(deps) != 0 { + return fmt.Sprintf("(%s.size() != 0) || (%s != 0)", addAsteriskAndBrackets(ref, val), deps[0]) } - return fmt.Sprintf("%s.size() != 0", addAsterisk(ref, val)) + return fmt.Sprintf("%s.size() != 0", addAsteriskAndBrackets(ref, val)) } return "" }