mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
schema/field: add annotation option to schema field (#622)
This commit is contained in:
13
entc/integration/ent/extension.go
Normal file
13
entc/integration/ent/extension.go
Normal file
@@ -0,0 +1,13 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
// Code generated by entc, DO NOT EDIT.
|
||||
|
||||
package ent
|
||||
|
||||
// CardExtension is a type for holding the extension information defined in the schema.
|
||||
type CardExtension struct {
|
||||
Number string
|
||||
Name string
|
||||
}
|
||||
@@ -6,6 +6,7 @@ package schema
|
||||
|
||||
import (
|
||||
"github.com/facebookincubator/ent"
|
||||
"github.com/facebookincubator/ent/entc/integration/ent/template"
|
||||
"github.com/facebookincubator/ent/schema/edge"
|
||||
"github.com/facebookincubator/ent/schema/field"
|
||||
"github.com/facebookincubator/ent/schema/mixin"
|
||||
@@ -27,11 +28,17 @@ func (Card) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.String("number").
|
||||
Immutable().
|
||||
NotEmpty(),
|
||||
NotEmpty().
|
||||
Annotations(&template.Extension{
|
||||
Type: "string",
|
||||
}),
|
||||
field.String("name").
|
||||
Optional().
|
||||
Comment("Exact name written on card").
|
||||
NotEmpty(),
|
||||
NotEmpty().
|
||||
Annotations(&template.Extension{
|
||||
Type: "string",
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
14
entc/integration/ent/template/ext.go
Normal file
14
entc/integration/ent/template/ext.go
Normal file
@@ -0,0 +1,14 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
package template
|
||||
|
||||
// Extension is a template extension.
|
||||
type Extension struct {
|
||||
Type string
|
||||
}
|
||||
|
||||
func (*Extension) Name() string {
|
||||
return "Extension"
|
||||
}
|
||||
27
entc/integration/ent/template/ext.tmpl
Normal file
27
entc/integration/ent/template/ext.tmpl
Normal file
@@ -0,0 +1,27 @@
|
||||
{{/*
|
||||
Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
This source code is licensed under the Apache 2.0 license found
|
||||
in the LICENSE file in the root directory of this source tree.
|
||||
*/}}
|
||||
|
||||
{{ define "extension" }}
|
||||
{{ $pkg := base $.Config.Package }}
|
||||
{{ template "header" $ }}
|
||||
|
||||
{{ range $n := $.Nodes }}
|
||||
{{ $hasExt := false }}
|
||||
{{ range $f := $n.Fields }}{{ if $f.Annotations.Extension }}{{ $hasExt = true }}{{ end }}{{ end }}
|
||||
{{/* If one or fields contain the "Extension" annotation */}}
|
||||
{{ if $hasExt }}
|
||||
// {{ $n.Name }}Extension is a type for holding the extension information defined in the schema.
|
||||
type {{ $n.Name }}Extension struct {
|
||||
{{- range $f := $n.Fields }}
|
||||
{{- with $ant := $f.Annotations.Extension }}
|
||||
{{ $f.StructField }} {{ $ant.Type }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
{{ end }}
|
||||
13
entc/integration/gremlin/ent/extension.go
Normal file
13
entc/integration/gremlin/ent/extension.go
Normal file
@@ -0,0 +1,13 @@
|
||||
// Copyright 2019-present Facebook Inc. All rights reserved.
|
||||
// This source code is licensed under the Apache 2.0 license found
|
||||
// in the LICENSE file in the root directory of this source tree.
|
||||
|
||||
// Code generated by entc, DO NOT EDIT.
|
||||
|
||||
package ent
|
||||
|
||||
// CardExtension is a type for holding the extension information defined in the schema.
|
||||
type CardExtension struct {
|
||||
Number string
|
||||
Name string
|
||||
}
|
||||
@@ -1115,6 +1115,12 @@ func Mutation(t *testing.T, client *ent.Client) {
|
||||
require.Equal(t, "boring", usr.Name)
|
||||
}
|
||||
|
||||
// Test templates codegen.
|
||||
var (
|
||||
_ = ent.CardExtension{}
|
||||
_ = ent.Card{}.StaticField
|
||||
)
|
||||
|
||||
func drop(t *testing.T, client *ent.Client) {
|
||||
t.Log("drop data from database")
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -43,7 +43,7 @@ type Edge struct {
|
||||
}
|
||||
|
||||
{{/* loop over all types and add implement the Node interface. */}}
|
||||
{{ range $_, $n := $.Nodes -}}
|
||||
{{ range $n := $.Nodes -}}
|
||||
{{ $receiver := $n.Receiver }}
|
||||
func ({{ $receiver }} *{{ $n.Name }}) Node(ctx context.Context) (node *Node, err error) {
|
||||
node = &Node{
|
||||
|
||||
Reference in New Issue
Block a user