Skip to content

Commit

Permalink
entc/gen: merge context.go contents to ent.go file (ent#3355)
Browse files Browse the repository at this point in the history
  • Loading branch information
a8m authored Feb 28, 2023
1 parent 5a93d89 commit 89e2d52
Show file tree
Hide file tree
Showing 73 changed files with 937 additions and 1,344 deletions.
6 changes: 1 addition & 5 deletions entc/gen/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ var (
"dialect/*/query/fields/init/*",
},
},
{
Name: "context",
Format: "context.go",
},
{
Name: "tx",
Format: "tx.go",
Expand Down Expand Up @@ -168,7 +164,7 @@ var (
},
}
// template files that were deleted and should be removed by the codegen.
deletedTemplates = []string{"config.go"}
deletedTemplates = []string{"config.go", "context.go"}
// patterns for extending partial-templates (included by other templates).
partialPatterns = [...]string{
"client/additional/*",
Expand Down
26 changes: 26 additions & 0 deletions entc/gen/template/base.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,32 @@ type (
MutateFunc = ent.MutateFunc
)

type clientCtxKey struct{}

// FromContext returns a Client stored inside a context, or nil if there isn't one.
func FromContext(ctx context.Context) *Client {
c, _ := ctx.Value(clientCtxKey{}).(*Client)
return c
}

// NewContext returns a new context with the given Client attached.
func NewContext(parent context.Context, c *Client) context.Context {
return context.WithValue(parent, clientCtxKey{}, c)
}

type txCtxKey struct {}

// TxFromContext returns a Tx stored inside a context, or nil if there isn't one.
func TxFromContext(ctx context.Context) *Tx {
tx, _ := ctx.Value(txCtxKey{}).(*Tx)
return tx
}

// NewTxContext returns a new context with the given Tx attached.
func NewTxContext(parent context.Context, tx *Tx) context.Context {
return context.WithValue(parent, txCtxKey{}, tx)
}

{{ $tmpl := printf "dialect/%s/order/signature" $.Storage }}
{{ xtemplate $tmpl . }}

Expand Down
44 changes: 0 additions & 44 deletions entc/gen/template/context.tmpl

This file was deleted.

37 changes: 0 additions & 37 deletions entc/integration/cascadelete/ent/context.go

This file was deleted.

26 changes: 26 additions & 0 deletions entc/integration/cascadelete/ent/ent.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 0 additions & 37 deletions entc/integration/config/ent/context.go

This file was deleted.

26 changes: 26 additions & 0 deletions entc/integration/config/ent/ent.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 0 additions & 37 deletions entc/integration/customid/ent/context.go

This file was deleted.

26 changes: 26 additions & 0 deletions entc/integration/customid/ent/ent.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 0 additions & 37 deletions entc/integration/edgefield/ent/context.go

This file was deleted.

26 changes: 26 additions & 0 deletions entc/integration/edgefield/ent/ent.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 89e2d52

Please sign in to comment.