entc/gen: add support for adding client fields using template

Summary: Pull Request resolved: https://github.com/facebookincubator/ent/pull/155

Reviewed By: alexsn

Differential Revision: D18421223

fbshipit-source-id: 112d4d26d53a64e2c78640c6d508ba2d1d3a7791
This commit is contained in:
Ariel Mashraki
2019-11-10 13:28:18 -08:00
committed by Facebook Github Bot
parent 6ed99b93e5
commit 514c0ff6d3
5 changed files with 25 additions and 2 deletions

File diff suppressed because one or more lines are too long

View File

@@ -34,6 +34,7 @@ type Client struct {
// {{ $n.Name }} is the client for interacting with the {{ $n.Name }} builders.
{{ $n.Name }} *{{ $n.Name }}Client
{{ end }}
{{ template "client/fields/additional" $ }}
}
// NewClient creates a new client configured with the given options.
@@ -204,3 +205,6 @@ func (c *{{ $client }}) Query{{ pascal $e.Name }}({{ $rec }} *{{ $n.Name }}) *{{
{{ end }}
{{ end }}
{{/* A template that can be overrided in order to add additional fields to the client.*/}}
{{ define "client/fields/additional" }}{{end}}

View File

@@ -10,6 +10,7 @@ import (
"context"
"fmt"
"log"
"sync"
"github.com/facebookincubator/ent/entc/integration/template/ent/migrate"
@@ -32,6 +33,10 @@ type Client struct {
Pet *PetClient
// User is the client for interacting with the User builders.
User *UserClient
// additional fields.
sync.Mutex
tables []string
}
// NewClient creates a new client configured with the given options.

View File

@@ -0,0 +1,11 @@
{{/*
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 "client/fields/additional" }}
// additional fields.
sync.Mutex
tables []string
{{ end }}

View File

@@ -42,4 +42,7 @@ func TestCustomTemplate(t *testing.T) {
require.NoError(t, err)
require.Equal(t, g.ID, node.ID)
require.Equal(t, &ent.Field{Type: "int", Name: "MaxUsers", Value: "10"}, node.Fields[0])
// compile time check for client fields.
_ = &client.Mutex
}