Generate ent schema in admin code. (#127)
This commit is contained in:
parent
67a97832a5
commit
9e6d9fd063
13 changed files with 303 additions and 142 deletions
|
|
@ -35,10 +35,10 @@
|
|||
}
|
||||
}
|
||||
|
||||
func (h *Handler) Create(ctx echo.Context, entityType string) error {
|
||||
switch entityType {
|
||||
func (h *Handler) Create(ctx echo.Context, entityType EntityType) error {
|
||||
switch entityType.(type) {
|
||||
{{- range $n := $.Nodes }}
|
||||
case "{{ $n.Name }}":
|
||||
case *{{ $n.Name }}:
|
||||
return h.{{ $n.Name }}Create(ctx)
|
||||
{{- end }}
|
||||
default:
|
||||
|
|
@ -46,10 +46,10 @@
|
|||
}
|
||||
}
|
||||
|
||||
func (h *Handler) Get(ctx echo.Context, entityType string, id int) (url.Values, error) {
|
||||
switch entityType {
|
||||
func (h *Handler) Get(ctx echo.Context, entityType EntityType, id int) (url.Values, error) {
|
||||
switch entityType.(type) {
|
||||
{{- range $n := $.Nodes }}
|
||||
case "{{ $n.Name }}":
|
||||
case *{{ $n.Name }}:
|
||||
return h.{{ $n.Name }}Get(ctx, id)
|
||||
{{- end }}
|
||||
default:
|
||||
|
|
@ -57,10 +57,10 @@
|
|||
}
|
||||
}
|
||||
|
||||
func (h *Handler) Delete(ctx echo.Context, entityType string, id int) error {
|
||||
switch entityType {
|
||||
func (h *Handler) Delete(ctx echo.Context, entityType EntityType, id int) error {
|
||||
switch entityType.(type) {
|
||||
{{- range $n := $.Nodes }}
|
||||
case "{{ $n.Name }}":
|
||||
case *{{ $n.Name }}:
|
||||
return h.{{ $n.Name }}Delete(ctx, id)
|
||||
{{- end }}
|
||||
default:
|
||||
|
|
@ -68,10 +68,10 @@
|
|||
}
|
||||
}
|
||||
|
||||
func (h *Handler) Update(ctx echo.Context, entityType string, id int) error {
|
||||
switch entityType {
|
||||
func (h *Handler) Update(ctx echo.Context, entityType EntityType, id int) error {
|
||||
switch entityType.(type) {
|
||||
{{- range $n := $.Nodes }}
|
||||
case "{{ $n.Name }}":
|
||||
case *{{ $n.Name }}:
|
||||
return h.{{ $n.Name }}Update(ctx, id)
|
||||
{{- end }}
|
||||
default:
|
||||
|
|
@ -79,10 +79,10 @@
|
|||
}
|
||||
}
|
||||
|
||||
func (h *Handler) List(ctx echo.Context, entityType string) (*EntityList, error) {
|
||||
switch entityType {
|
||||
func (h *Handler) List(ctx echo.Context, entityType EntityType) (*EntityList, error) {
|
||||
switch entityType.(type) {
|
||||
{{- range $n := $.Nodes }}
|
||||
case "{{ $n.Name }}":
|
||||
case *{{ $n.Name }}:
|
||||
return h.{{ $n.Name }}List(ctx)
|
||||
{{- end }}
|
||||
default:
|
||||
|
|
|
|||
54
ent/admin/templates/schema.tmpl
Normal file
54
ent/admin/templates/schema.tmpl
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
{{/* Tell Intellij/GoLand to enable the autocompletion based on the *gen.Graph type. */}}
|
||||
{{/* gotype: entgo.io/ent/entc/gen.Graph */}}
|
||||
|
||||
{{ define "admin/schema" }}
|
||||
// Code generated by ent, DO NOT EDIT.
|
||||
package admin
|
||||
|
||||
import (
|
||||
"entgo.io/ent/schema/field"
|
||||
)
|
||||
|
||||
type Enum struct {
|
||||
Label, Value string
|
||||
}
|
||||
|
||||
type FieldSchema struct {
|
||||
Name string
|
||||
Type field.Type
|
||||
Optional bool
|
||||
Immutable bool
|
||||
Sensitive bool
|
||||
Enums []Enum
|
||||
}
|
||||
|
||||
|
||||
{{- range $n := $.Nodes }}
|
||||
const Name{{ $n.Name }} = "{{ $n.Name }}"
|
||||
|
||||
var fields{{ $n.Name }} = []*FieldSchema{
|
||||
{{- range $f := $n.Fields }}
|
||||
{
|
||||
Name: "{{ $f.Name }}",
|
||||
Type: field.{{ $f.Type.Type.ConstName }},
|
||||
Optional: {{ $f.Optional }},
|
||||
Immutable: {{ $f.Immutable }},
|
||||
Sensitive: {{ $f.Sensitive }},
|
||||
{{- if len $f.Enums }}
|
||||
Enums: []Enum{
|
||||
{{- range $e := $f.Enums }}
|
||||
{
|
||||
Label: "{{ $e.Label }}",
|
||||
Value: "{{ $e.Value }}",
|
||||
},
|
||||
{{- end }}
|
||||
},
|
||||
{{- else }}
|
||||
Enums: nil,
|
||||
{{- end }}
|
||||
},
|
||||
{{- end }}
|
||||
}
|
||||
{{ end }}
|
||||
|
||||
{{ end }}
|
||||
|
|
@ -11,8 +11,27 @@
|
|||
{{ fieldName $f.Name }} {{ if (fieldIsPointer $f) }}*{{ end }}{{ $f.Type }} `form:"{{ $f.Name }}"`
|
||||
{{- end }}
|
||||
}
|
||||
|
||||
func (e *{{ $n.Name }}) GetName() string {
|
||||
return Name{{ $n.Name }}
|
||||
}
|
||||
|
||||
func (e *{{ $n.Name }}) GetSchema() []*FieldSchema {
|
||||
return fields{{ $n.Name }}
|
||||
}
|
||||
{{ end }}
|
||||
|
||||
type EntityType interface {
|
||||
GetName() string
|
||||
GetSchema() []*FieldSchema
|
||||
}
|
||||
|
||||
var entityTypes = []EntityType{
|
||||
{{- range $n := $.Nodes }}
|
||||
&{{ $n.Name }}{},
|
||||
{{- end }}
|
||||
}
|
||||
|
||||
type EntityList struct {
|
||||
Columns []string
|
||||
Entities []EntityValues
|
||||
|
|
@ -31,12 +50,7 @@
|
|||
TimeFormat string
|
||||
}
|
||||
|
||||
func GetEntityTypeNames() []string {
|
||||
return []string{
|
||||
{{- range $n := $.Nodes }}
|
||||
"{{ $n.Name }}",
|
||||
{{- end }}
|
||||
}
|
||||
func GetEntityTypes() []EntityType {
|
||||
return entityTypes
|
||||
}
|
||||
|
||||
{{ end }}
|
||||
Loading…
Add table
Add a link
Reference in a new issue