-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The `scope` defines when a service should be created, or when it can be reused. It must be one of the following values: - `prototype`: A new instance will be created whenever the service is requested or injected into another service as a dependency. - `container` (default): The instance will created once for this container, and then it will be returned in future requests. This is sometimes called a singleton, however the service will not be shared outside of the container. Fixes #2
- Loading branch information
1 parent
2586155
commit 4fdd3d9
Showing
7 changed files
with
248 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,13 @@ Easy, fast and type-safe dependency injection for Go. | |
* [Installation](#installation) | ||
* [Building the Container](#building-the-container) | ||
* [Configuring Services](#configuring-services) | ||
+ [error](#error) | ||
+ [import](#import) | ||
+ [interface](#interface) | ||
+ [properties](#properties) | ||
+ [returns](#returns) | ||
+ [scope](#scope) | ||
+ [type](#type) | ||
* [Using Services](#using-services) | ||
* [Unit Testing](#unit-testing) | ||
|
||
|
@@ -31,75 +38,114 @@ Here is an example of a `dingo.yml`: | |
```yml | ||
services: | ||
SendEmail: | ||
type: *SendEmail | ||
type: '*SendEmail' | ||
interface: EmailSender | ||
properties: | ||
From: '"[email protected]"' | ||
|
||
CustomerWelcome: | ||
type: *CustomerWelcome | ||
returns: NewCustomerWelcome(@SendEmail) | ||
type: '*CustomerWelcome' | ||
returns: NewCustomerWelcome(@{SendEmail}) | ||
``` | ||
It will generate a file called `dingo.go`. This must be committed with your | ||
code. | ||
|
||
## Configuring Services | ||
|
||
The `dingo.yml` is described below: | ||
The root level `services` key describes each of the services. | ||
|
||
```yml | ||
services: | ||
# Describes each of the services. The name of the service follow the same | ||
# naming conventions as Go, so service names that start with a capital letter | ||
# will be exported (available outside this package). | ||
SendEmail: | ||
# Required: You must provide either 'type' or 'interface'. | ||
# Optional: The type returned by the `return` expression. You must provide a | ||
# fully qualified name that includes the package name if the type does not | ||
# belong to this package. For example: | ||
# | ||
# type: '*github.com/go-redis/redis.Options' | ||
# | ||
type: *SendEmail | ||
|
||
# Optional: If you need to replace this service with another struct type in | ||
# unit tests you will need to provide an interface. This will override | ||
# `type` and must be compatible with returned type of `return`. | ||
interface: EmailSender | ||
|
||
# Optional: The expression used to instantiate the service. You can provide | ||
# any Go code here, including referencing other services and environment | ||
# variables. Described in more detail below. | ||
returns: NewSendEmail() | ||
|
||
# Optional: If 'returns' provides two arguments (where the second one is the | ||
# error) you must include an 'error'. This is the expression when | ||
# "err != nil". | ||
error: panic(err) | ||
|
||
# Optional: If provided, a map of case-sensitive properties to be set on the | ||
# instance. Each of the properties is Go code and can have the same | ||
# substitutions described below. | ||
properties: | ||
From: "[email protected]" | ||
maxRetries: 10 | ||
|
||
# Optional: You can provide explicit imports if you need to reference | ||
# packages in expressions (such as 'returns') that do not exist 'type' or | ||
# 'interface'. | ||
import: | ||
- 'github.com/aws/aws-sdk-go/aws/session' | ||
``` | ||
The name of the service follows the same naming conventions as Go, so service | ||
names that start with a capital letter will be exported (available outside this | ||
package). | ||
|
||
All options described below are optional. However, you must provide either | ||
`type` or `interface`. | ||
|
||
The `returns` and properties can contain any Go code, and allows the following | ||
substitutions: | ||
Any option below that expects an expression can contain any valid Go code. | ||
References to other services and variables will be substituted automatically: | ||
|
||
- `@{SendEmail}` will inject the service named `SendEmail`. | ||
- `${DB_PASS}` will inject the environment variable `DB_PASS`. | ||
|
||
### error | ||
|
||
If `returns` provides two arguments (where the second one is the error) you must | ||
include an `error`. This is the expression when `err != nil`. | ||
|
||
Examples: | ||
|
||
- `error: panic(err)` - panic if an error occurs. | ||
- `error: return nil` - return a nil service if an error occurs. | ||
|
||
### import | ||
|
||
You can provide explicit imports if you need to reference packages in | ||
expressions (such as `returns`) that do not exist in `type` or `interface`. | ||
|
||
If a package listed in `import` is already imported, either directly or | ||
indirectly, it value will be ignored. | ||
|
||
Example: | ||
|
||
```yml | ||
import: | ||
- 'github.com/aws/aws-sdk-go/aws/session' | ||
``` | ||
|
||
### interface | ||
|
||
If you need to replace this service with another `struct` type in unit tests you | ||
will need to provide an `interface`. This will override `type` and must be | ||
compatible with returned type of `returns`. | ||
|
||
Examples: | ||
|
||
- `interface: EmailSender` - `EmailSender` in this package. | ||
- `interface: io.Writer` - `Writer` in the `io` package. | ||
|
||
### properties | ||
|
||
If provided, a map of case-sensitive properties to be set on the instance. Each | ||
of the properties is a Go expression. | ||
|
||
Example: | ||
|
||
```yml | ||
properties: | ||
From: "[email protected]" | ||
maxRetries: 10 | ||
emailer: '@{Emailer}' | ||
``` | ||
|
||
### returns | ||
|
||
The expression used to instantiate the service. You can provide any Go code | ||
here, including referencing other services and environment variables. | ||
|
||
### scope | ||
|
||
The `scope` defines when a service should be created, or when it can be reused. | ||
It must be one of the following values: | ||
|
||
- `prototype`: A new instance will be created whenever the service is requested | ||
or injected into another service as a dependency. | ||
|
||
- `container` (default): The instance will created once for this container, and | ||
then it will be returned in future requests. This is sometimes called a | ||
singleton, however the service will not be shared outside of the container. | ||
|
||
### type | ||
|
||
The type returned by the `return` expression. You must provide a fully qualified | ||
name that includes the package name if the type does not belong to this package. | ||
|
||
Example | ||
|
||
```: | ||
type: '*github.com/go-redis/redis.Options' | ||
``` | ||
## Using Services | ||
As part of the generated file, `dingo.go`. There will be a module-level variable | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.