schema/field: add annotation option to schema field (#622)

This commit is contained in:
Ariel Mashraki
2020-07-19 18:01:04 +03:00
committed by GitHub
parent bdd80ebb3f
commit 54f0a6769b
16 changed files with 418 additions and 30 deletions

View 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
}

View File

@@ -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",
}),
}
}

View 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"
}

View 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 }}

View 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
}

View File

@@ -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()

View File

@@ -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{