| 1 | package inject_test |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "os" |
| 7 | |
| 8 | "github.com/facebookgo/inject" |
| 9 | ) |
| 10 | |
| 11 | // Our Awesome Application renders a message using two APIs in our fake |
| 12 | // world. |
| 13 | type HomePlanetRenderApp struct { |
| 14 | // The tags below indicate to the inject library that these fields are |
| 15 | // eligible for injection. They do not specify any options, and will |
| 16 | // result in a singleton instance created for each of the APIs. |
| 17 | |
| 18 | NameAPI *NameAPI `inject:""` |
| 19 | PlanetAPI *PlanetAPI `inject:""` |
| 20 | } |
| 21 | |
| 22 | func (a *HomePlanetRenderApp) Render(id uint64) string { |
| 23 | return fmt.Sprintf( |
| 24 | "%s is from the planet %s.", |
| 25 | a.NameAPI.Name(id), |
| 26 | a.PlanetAPI.Planet(id), |
| 27 | ) |
| 28 | } |
| 29 | |
| 30 | // Our fake Name API. |
| 31 | type NameAPI struct { |
| 32 | // Here and below in PlanetAPI we add the tag to an interface value. |
| 33 | // This value cannot automatically be created (by definition) and |
| 34 | // hence must be explicitly provided to the graph. |
| 35 | |
| 36 | HTTPTransport http.RoundTripper `inject:""` |
| 37 | } |
| 38 | |
| 39 | func (n *NameAPI) Name(id uint64) string { |
| 40 | // in the real world we would use f.HTTPTransport and fetch the name |
| 41 | return "Spock" |
| 42 | } |
| 43 | |
| 44 | // Our fake Planet API. |
| 45 | type PlanetAPI struct { |
| 46 | HTTPTransport http.RoundTripper `inject:""` |
| 47 | } |
| 48 | |
| 49 | func (p *PlanetAPI) Planet(id uint64) string { |
| 50 | // in the real world we would use f.HTTPTransport and fetch the planet |
| 51 | return "Vulcan" |
| 52 | } |
| 53 | |
| 54 | func Example() { |
| 55 | // Typically an application will have exactly one object graph, and |
| 56 | // you will create it and use it within a main function: |
| 57 | var g inject.Graph |
| 58 | |
| 59 | // We provide our graph two "seed" objects, one our empty |
| 60 | // HomePlanetRenderApp instance which we're hoping to get filled out, |
| 61 | // and second our DefaultTransport to satisfy our HTTPTransport |
| 62 | // dependency. We have to provide the DefaultTransport because the |
| 63 | // dependency is defined in terms of the http.RoundTripper interface, |
| 64 | // and since it is an interface the library cannot create an instance |
| 65 | // for it. Instead it will use the given DefaultTransport to satisfy |
| 66 | // the dependency since it implements the interface: |
| 67 | var a HomePlanetRenderApp |
| 68 | err := g.Provide( |
| 69 | &inject.Object{Value: &a}, |
| 70 | &inject.Object{Value: http.DefaultTransport}, |
| 71 | ) |
| 72 | if err != nil { |
| 73 | fmt.Fprintln(os.Stderr, err) |
| 74 | os.Exit(1) |
| 75 | } |
| 76 | |
| 77 | // Here the Populate call is creating instances of NameAPI & |
| 78 | // PlanetAPI, and setting the HTTPTransport on both to the |
| 79 | // http.DefaultTransport provided above: |
| 80 | if err := g.Populate(); err != nil { |
| 81 | fmt.Fprintln(os.Stderr, err) |
| 82 | os.Exit(1) |
| 83 | } |
| 84 | |
| 85 | // There is a shorthand API for the simple case which combines the |
| 86 | // three calls above is available as inject.Populate: |
| 87 | // |
| 88 | // inject.Populate(&a, http.DefaultTransport) |
| 89 | // |
| 90 | // The above API shows the underlying API which also allows the use of |
| 91 | // named instances for more complex scenarios. |
| 92 | |
| 93 | fmt.Println(a.Render(42)) |
| 94 | |
| 95 | // Output: Spock is from the planet Vulcan. |
| 96 | } |