{{/* 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. */}} {{/* gotype: entgo.io/ent/entc/gen.Graph */}} {{ define "config" }} {{ $pkg := base $.Config.Package }} {{/* Additional dependencies. */}} {{ $deps := list }}{{ with $.Config.Annotations }}{{ $deps = $.Config.Annotations.Dependencies }}{{ end }} {{ template "header" $ }} {{ template "import" $ }} {{ with $deps }} import ( {{- range $dep := $deps }} {{ $dep.Type.PkgName }} "{{ $dep.Type.PkgPath }}" {{- end }} ) {{ end }} // Option function to configure the client. type Option func(*config) // Config is the configuration for the client and its builder. type config struct { // driver used for executing database requests. driver dialect.Driver // debug enable a debug logging. debug bool // log used for logging on debug mode. log func(...any) // hooks to execute on mutations. hooks *hooks {{- /* Additional dependency fields. */}} {{- range $dep := $deps }} {{ $dep.Field }} {{ $dep.Type }} {{- end }} {{- /* Support adding config fields from both global or dialect-specific templates. */}} {{- range $prefix := list "" (printf "dialect/%s/" $.Storage) }} {{- with $tmpls := matchTemplate (print $prefix "config/fields/*") }} {{- range $tmpl := $tmpls }} {{ xtemplate $tmpl $ }} {{- end }} {{- end }} {{- end }} } // hooks per client, for fast access. type hooks struct { {{- range $n := $.Nodes }} {{ $n.Name }} []ent.Hook {{- end }} } // Options applies the options on the config object. func (c *config) options(opts ...Option) { for _, opt := range opts { opt(c) } if c.debug { c.driver = dialect.Debug(c.driver, c.log) } } // Debug enables debug logging on the ent.Driver. func Debug() Option { return func(c *config) { c.debug = true } } // Log sets the logging function for debug mode. func Log(fn func(...any)) Option { return func(c *config) { c.log = fn } } // Driver configures the client driver. func Driver(driver dialect.Driver) Option { return func(c *config) { c.driver = driver } } {{- /* Additional dependency options. */}} {{- range $dep := $deps }} // {{ $dep.Option }} configures the {{ $dep.Field }}. func {{ $dep.Option }}(v {{ $dep.Type }}) Option { return func(c *config) { c.{{ $dep.Field }} = v } } {{- end }} {{- /* Support adding config options from both global or dialect-specific templates. */}} {{- range $prefix := list "" (printf "dialect/%s/" $.Storage) }} {{- with $tmpls := matchTemplate (print $prefix "config/options/*") }} {{- range $tmpl := $tmpls }} {{ xtemplate $tmpl $ }} {{- end }} {{- end }} {{- end }} {{- with $tmpls := matchTemplate "config/additional/*" "config/additional/*/*" }} {{- range $tmpl := $tmpls }} {{- xtemplate $tmpl $ }} {{- end }} {{- end }} {{ end }}