Skip to content

Commit

Permalink
feat: add Print method to entities
Browse files Browse the repository at this point in the history
Method takes an io.Writer and writes the entity's unformatted code to
it. These methods are also used when generating the package source code
to reduce the amount of string builders.
  • Loading branch information
michenriksen committed Oct 18, 2023
1 parent a8faa63 commit 6bc317b
Showing 1 changed file with 67 additions and 40 deletions.
107 changes: 67 additions & 40 deletions entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,32 @@ func (p *Package) Source() (string, error) {
return string(formatted), nil
}

// String returns the unformatted package signature source.
func (p *Package) String() string {
var b strings.Builder

// Print writes unformatted package code to writer.
func (p *Package) Print(w io.Writer) {
if p.Doc != "" {
b.WriteString(mkComment(p.Doc))
fmt.Fprint(w, mkComment(p.Doc))
}

fmt.Fprintf(&b, "package %s", p.Name)
fmt.Fprintf(w, "package %s", p.Name)

for _, t := range p.Types {
fmt.Fprintf(&b, "\n\n%s", t)
fmt.Fprint(w, "\n\n")
t.Print(w)
}

for _, f := range p.Funcs {
fmt.Fprintf(&b, "\n\n%s", f)
fmt.Fprint(w, "\n\n")
f.Print(w)
}

b.WriteString("\n")
fmt.Fprint(w, "\n")
}

// String returns the unformatted package signature source.
func (p *Package) String() string {
var b strings.Builder

p.Print(&b)

return b.String()
}
Expand All @@ -71,27 +78,34 @@ func (f Func) IsExported() bool {
return isExportedIdent(f.Name)
}

// String returns the function signature code.
func (f Func) String() string {
var b strings.Builder

// Print writes unformatted function signature code to writer.
func (f Func) Print(w io.Writer) {
if f.Doc != "" {
b.WriteString(mkComment(f.Doc))
fmt.Fprint(w, mkComment(f.Doc))
}

if f.funcKw {
b.WriteString("func ")
fmt.Fprint(w, "func ")
}

if f.Receiver != nil {
fmt.Fprintf(&b, "(%s) ", f.Receiver)
fmt.Fprint(w, "(")
f.Receiver.Print(w)
fmt.Fprint(w, ") ")
}

fmt.Fprintf(&b, "%s(%s) %s", f.Name, fieldsList(f.Params), resultsList(f.Results))
fmt.Fprintf(w, "%s(%s) %s", f.Name, fieldsList(f.Params), resultsList(f.Results))

if f.Comment != "" {
fmt.Fprintf(&b, " // %s", f.Comment)
fmt.Fprintf(w, " // %s", f.Comment)
}
}

// String returns the function signature code.
func (f Func) String() string {
var b strings.Builder

f.Print(&b)

return b.String()
}
Expand Down Expand Up @@ -122,34 +136,40 @@ func (td TypeDef) IsExported() bool {
return isExportedIdent(td.Name)
}

// String returns the type definition code.
func (td TypeDef) String() string {
var b strings.Builder

// Print writes unformatted type definition code to writer.
func (td TypeDef) Print(w io.Writer) {
switch td.Type {
case "struct":
printStructType(&b, td)
printStructType(w, td)
case "interface":
printInterfaceType(&b, td)
printInterfaceType(w, td)
case "func":
printFuncType(&b, td)
printFuncType(w, td)
case "map":
printMapType(&b, td)
printMapType(w, td)
case "chan":
printChanType(&b, td)
printChanType(w, td)
case "array":
printArrayType(&b, td)
printArrayType(w, td)
default:
if td.Doc != "" {
b.WriteString(mkComment(td.Doc))
fmt.Fprint(w, mkComment(td.Doc))
}

fmt.Fprintf(&b, "type %s %s", td.Name, td.Type)
fmt.Fprintf(w, "type %s %s", td.Name, td.Type)

for _, m := range td.Methods {
fmt.Fprintf(&b, "\n\n%s", m)
fmt.Fprint(w, "\n\n")
m.Print(w)
}
}
}

// String returns the type definition code.
func (td TypeDef) String() string {
var b strings.Builder

td.Print(&b)

return b.String()
}
Expand All @@ -172,19 +192,24 @@ func (sf Field) IsExported() bool {
return isExportedIdent(sf.Names[0])
}

// String returns the unformatted struct field code fragment.
func (sf Field) String() string {
var b strings.Builder

// Print writes unformatted field code fragment to writer.
func (sf Field) Print(w io.Writer) {
if sf.Doc != "" {
b.WriteString(mkComment(sf.Doc))
fmt.Fprint(w, mkComment(sf.Doc))
}

fmt.Fprintf(&b, "%s %s", strings.Join(sf.Names, ", "), sf.Type)
fmt.Fprintf(w, "%s %s", strings.Join(sf.Names, ", "), sf.Type)

if sf.Comment != "" {
fmt.Fprintf(&b, " // %s", sf.Comment)
fmt.Fprintf(w, " // %s", sf.Comment)
}
}

// String returns the unformatted field code fragment.
func (sf Field) String() string {
var b strings.Builder

sf.Print(&b)

return b.String()
}
Expand All @@ -200,7 +225,8 @@ func printStructType(w io.Writer, s TypeDef) {
fmt.Fprint(w, "\n")

for _, f := range s.Fields {
fmt.Fprintf(w, " %s\n", f)
f.Print(w)
fmt.Fprint(w, "\n")
}
}

Expand All @@ -211,7 +237,8 @@ func printStructType(w io.Writer, s TypeDef) {
}

for _, fn := range s.Methods {
fmt.Fprintf(w, "\n\n%s", fn)
fmt.Fprint(w, "\n\n")
fn.Print(w)
}
}

Expand Down

0 comments on commit 6bc317b

Please sign in to comment.