entc/gen: allow users to override the stringer implementation using templates

* Update ent.tmpl

Fixed a small typo

* allow customization of stringer implementation

* remove "model/stringer" from ExtendPatterns
This commit is contained in:
Jannik C
2020-11-05 17:29:56 +01:00
committed by GitHub
parent 3abb2726d0
commit d6702a49fa
4 changed files with 89 additions and 53 deletions

File diff suppressed because one or more lines are too long

View File

@@ -103,42 +103,7 @@ func ({{ $receiver }} *{{ $.Name }}) Unwrap() *{{ $.Name }} {
return {{ $receiver }}
}
// String implements the fmt.Stringer.
func ({{ $receiver }} *{{ $.Name }}) String() string {
var builder strings.Builder
builder.WriteString("{{ $.Name }}(")
builder.WriteString(fmt.Sprintf("id=%v", {{ $receiver }}.ID))
{{- range $i, $f := $.Fields }}
{{- if $f.Sensitive }}
builder.WriteString(", {{ $f.Name }}=<sensitive>")
{{- else }}
{{- $sf := printf "%s.%s" $receiver $f.StructField }}
{{- if $f.Nillable }}
if v := {{ $sf }}; v != nil {
builder.WriteString(", {{ $f.Name }}=")
{{- if and $f.IsTime (not $f.HasGoType) }}
builder.WriteString(v.Format(time.ANSIC))
{{- else if and $f.IsString (not $f.HasGoType) }}
builder.WriteString(*v)
{{- else }}
builder.WriteString(fmt.Sprintf("%v", *v))
{{- end }}
}
{{- else }}
builder.WriteString(", {{ $f.Name }}=")
{{- if and $f.IsTime (not $f.HasGoType) }}
builder.WriteString({{ $sf }}.Format(time.ANSIC))
{{- else if and $f.IsString (not $f.HasGoType) }}
builder.WriteString({{ $sf }})
{{- else }}
builder.WriteString(fmt.Sprintf("%v", {{ $sf }}))
{{- end }}
{{- end }}
{{- end }}
{{- end }}
builder.WriteByte(')')
return builder.String()
}
{{ template "model/stringer" $ }}
{{ $slice := plural $.Name }}
// {{ $slice }} is a parsable slice of {{ $.Name }}.
@@ -156,6 +121,48 @@ func ({{ $receiver }} {{ $slice }}) config(cfg config) {
}
{{ end }}
{{/* A template to generate a fmt.Stringer implementation. */}}
{{ define "model/stringer" }}
{{ $receiver := $.Receiver }}
// String implements the fmt.Stringer.
func ({{ $receiver }} *{{ $.Name }}) String() string {
var builder strings.Builder
builder.WriteString("{{ $.Name }}(")
builder.WriteString(fmt.Sprintf("id=%v", {{ $receiver }}.ID))
{{- range $i, $f := $.Fields }}
{{- if $f.Sensitive }}
builder.WriteString(", {{ $f.Name }}=<sensitive>")
{{- else }}
{{- $sf := printf "%s.%s" $receiver $f.StructField }}
{{- if $f.Nillable }}
if v := {{ $sf }}; v != nil {
builder.WriteString(", {{ $f.Name }}=")
{{- if and $f.IsTime (not $f.HasGoType) }}
builder.WriteString(v.Format(time.ANSIC))
{{- else if and $f.IsString (not $f.HasGoType) }}
builder.WriteString(*v)
{{- else }}
builder.WriteString(fmt.Sprintf("%v", *v))
{{- end }}
}
{{- else }}
builder.WriteString(", {{ $f.Name }}=")
{{- if and $f.IsTime (not $f.HasGoType) }}
builder.WriteString({{ $sf }}.Format(time.ANSIC))
{{- else if and $f.IsString (not $f.HasGoType) }}
builder.WriteString({{ $sf }})
{{- else }}
builder.WriteString(fmt.Sprintf("%v", {{ $sf }}))
{{- end }}
{{- end }}
{{- end }}
{{- end }}
builder.WriteByte(')')
return builder.String()
}
{{ end }}
{{/* A template for generating the tag of the Edges struct-field. */}}
{{ define "model/edgetags" }}{{ with $.Annotations.Edges.StructTag }}`{{ . }}`{{ else }}`json:"edges"`{{ end }}{{ end }}

View File

@@ -8,7 +8,6 @@ package ent
import (
"fmt"
"strings"
"time"
"github.com/facebook/ent/dialect/sql"
@@ -128,20 +127,7 @@ func (pe *Pet) Unwrap() *Pet {
return pe
}
// String implements the fmt.Stringer.
func (pe *Pet) String() string {
var builder strings.Builder
builder.WriteString("Pet(")
builder.WriteString(fmt.Sprintf("id=%v", pe.ID))
builder.WriteString(", age=")
builder.WriteString(fmt.Sprintf("%v", pe.Age))
if v := pe.LicensedAt; v != nil {
builder.WriteString(", licensed_at=")
builder.WriteString(v.Format(time.ANSIC))
}
builder.WriteByte(')')
return builder.String()
}
// custom stringer implementation (in this case none)
// Pets is a parsable slice of Pet.
type Pets []*Pet

View File

@@ -0,0 +1,43 @@
{{ define "model/stringer" }}
{{- if eq $.Name "Pet" }}
// custom stringer implementation (in this case none)
{{ else }}
{{ $receiver := $.Receiver }}
// String implements the fmt.Stringer.
func ({{ $receiver }} *{{ $.Name }}) String() string {
var builder strings.Builder
builder.WriteString("{{ $.Name }}(")
builder.WriteString(fmt.Sprintf("id=%v", {{ $receiver }}.ID))
{{- range $i, $f := $.Fields }}
{{- if $f.Sensitive }}
builder.WriteString(", {{ $f.Name }}=<sensitive>")
{{- else }}
{{- $sf := printf "%s.%s" $receiver $f.StructField }}
{{- if $f.Nillable }}
if v := {{ $sf }}; v != nil {
builder.WriteString(", {{ $f.Name }}=")
{{- if and $f.IsTime (not $f.HasGoType) }}
builder.WriteString(v.Format(time.ANSIC))
{{- else if and $f.IsString (not $f.HasGoType) }}
builder.WriteString(*v)
{{- else }}
builder.WriteString(fmt.Sprintf("%v", *v))
{{- end }}
}
{{- else }}
builder.WriteString(", {{ $f.Name }}=")
{{- if and $f.IsTime (not $f.HasGoType) }}
builder.WriteString({{ $sf }}.Format(time.ANSIC))
{{- else if and $f.IsString (not $f.HasGoType) }}
builder.WriteString({{ $sf }})
{{- else }}
builder.WriteString(fmt.Sprintf("%v", {{ $sf }}))
{{- end }}
{{- end }}
{{- end }}
{{- end }}
builder.WriteByte(')')
return builder.String()
}
{{- end}}
{{ end }}