(ic *Inception, si *StructInfo)
| 481 | } |
| 482 | |
| 483 | func CreateMarshalJSON(ic *Inception, si *StructInfo) error { |
| 484 | conditionalWrites := lastConditional(si.Fields) |
| 485 | out := "" |
| 486 | |
| 487 | out += "// MarshalJSON marshal bytes to json - template\n" |
| 488 | out += `func (j *` + si.Name + `) MarshalJSON() ([]byte, error) {` + "\n" |
| 489 | out += `var buf fflib.Buffer` + "\n" |
| 490 | |
| 491 | out += `if j == nil {` + "\n" |
| 492 | out += ` buf.WriteString("null")` + "\n" |
| 493 | out += " return buf.Bytes(), nil" + "\n" |
| 494 | out += `}` + "\n" |
| 495 | |
| 496 | out += `err := j.MarshalJSONBuf(&buf)` + "\n" |
| 497 | out += `if err != nil {` + "\n" |
| 498 | out += " return nil, err" + "\n" |
| 499 | out += `}` + "\n" |
| 500 | out += `return buf.Bytes(), nil` + "\n" |
| 501 | out += `}` + "\n" |
| 502 | |
| 503 | out += "// MarshalJSONBuf marshal buff to json - template\n" |
| 504 | out += `func (j *` + si.Name + `) MarshalJSONBuf(buf fflib.EncodingBuffer) (error) {` + "\n" |
| 505 | out += ` if j == nil {` + "\n" |
| 506 | out += ` buf.WriteString("null")` + "\n" |
| 507 | out += " return nil" + "\n" |
| 508 | out += ` }` + "\n" |
| 509 | |
| 510 | out += `var err error` + "\n" |
| 511 | out += `var obj []byte` + "\n" |
| 512 | out += `_ = obj` + "\n" |
| 513 | out += `_ = err` + "\n" |
| 514 | |
| 515 | ic.q.Write("{") |
| 516 | |
| 517 | // The extra space is inserted here. |
| 518 | // If nothing is written to the field this will be deleted |
| 519 | // instead of the last comma. |
| 520 | if conditionalWrites || len(si.Fields) == 0 { |
| 521 | ic.q.Write(" ") |
| 522 | } |
| 523 | |
| 524 | for _, f := range si.Fields { |
| 525 | out += getField(ic, f, "j.") |
| 526 | } |
| 527 | |
| 528 | // Handling the last comma is tricky. |
| 529 | // If the last field has omitempty, conditionalWrites is set. |
| 530 | // If something has been written, we delete the last comma, |
| 531 | // by backing up the buffer, otherwise it will delete a space. |
| 532 | if conditionalWrites { |
| 533 | out += ic.q.Flush() |
| 534 | out += `buf.Rewind(1)` + "\n" |
| 535 | } else { |
| 536 | ic.q.DeleteLast() |
| 537 | } |
| 538 | |
| 539 | out += ic.q.WriteFlush("}") |
| 540 | out += `return nil` + "\n" |
no test coverage detected
searching dependent graphs…