Skip to content

Commit

Permalink
add write json and working test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
Brat-vseznamus committed Aug 1, 2024
1 parent da82541 commit b366b2d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
4 changes: 4 additions & 0 deletions internal/tlcodegen/type_rw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/tlcodegen/type_rw_maybe_cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/tlcodegen/type_rw_primitive_cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
26 changes: 22 additions & 4 deletions internal/tlcodegen/type_rw_struct_cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions internal/tlcodegen/type_rw_tuple_cpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}
Expand Down

0 comments on commit b366b2d

Please sign in to comment.