Example #1
0
func main() {
	graph := inject.NewGraph()
	var (
		logger    log.Logger
		server    *app.Server
		a         *A
		b         *B
		mux       *http.ServeMux
		dconf     *distconf.Distconf
		authCheck *app.AuthorizationCheck
		tk        timekeeper.TimeKeeper
		auth      app.Authentication
	)

	graph.Define(&logger, inject.NewProvider(func() log.Logger {
		return log.DefaultLogger
	}))
	graph.Define(&tk, inject.NewProvider(func() timekeeper.RealTime {
		return timekeeper.RealTime{}
	}))
	graph.Define(&auth, inject.NewProvider(func() *app.StrictCheck {
		return &app.StrictCheck{
			Username: "******",
			Password: "******",
		}
	}))

	graph.Define(&dconf, inject.NewProvider(distconf.New, &[]distconf.Reader{distconf.Env()}))
	graph.Define(&mux, inject.NewProvider(http.NewServeMux))
	graph.Define(&authCheck, inject.NewAutoProvider(app.NewAuthorizationCheck))
	graph.Define(&a, inject.NewAutoProvider(NewA))
	graph.Define(&b, inject.NewAutoProvider(NewB))
	graph.Define(&server, inject.NewAutoProvider(app.NewServer))

	graph.Resolve(&logger)
	graph.Resolve(&server)
	graph.Resolve(&a)
	fmt.Println(a)
	logger.Log("Setting up")
	if err := server.Setup(); err != nil {
		logger.Log("Cannot setup!")
		panic(err)
	}
	err := server.Start(context.Background())
	fmt.Println(err)
}
Example #2
0
func TestGraphSupportsAutoProvider(t *testing.T) {
	RegisterTestingT(t)

	graph := inject.NewGraph()

	var (
		name = "FullName"
		a    InterfaceA
		b    InterfaceB
	)

	graph.Define(&a, inject.NewAutoProvider(NewA))
	graph.Define(&b, inject.NewProvider(NewB, &name))
	graph.ResolveAll()

	Expect(a).To(Equal(NewA(NewB(name))))
	Expect(a.String()).To(Equal("&implA{b: &implB{name: \"FullName\"}}"))

	Expect(b).To(Equal(NewB(name)))
	Expect(b.String()).To(Equal("&implB{name: \"FullName\"}"))

	expectedString := `&graph\{
  definitions: \[
    &definition\{
      ptr: \*test\.InterfaceA=0x.*,
      provider: &autoProvider\{
        constructor: func\(test\.InterfaceB\) test\.InterfaceA
      \},
      value: <test\.InterfaceA Value>
    },
    &definition\{
      ptr: \*test\.InterfaceB=0x.*,
      provider: &provider\{
        constructor: func\(string\) test\.InterfaceB,
        argPtrs: \[
          \*string=0x.*
        \]
      \},
      value: <test\.InterfaceB Value>
    \}
  \]
\}`
	Expect(graph.String()).To(MatchRegexp(expectedString))
}