예제 #1
0
func TestGraphSupportsPartialResolution(t *testing.T) {
	RegisterTestingT(t)

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

	graph := inject.NewGraph(
		inject.NewDefinition(&a, inject.NewProvider(NewA, &b)),
		inject.NewDefinition(&b, inject.NewProvider(NewB, &name)),
	)

	Expect(a).To(BeNil())
	Expect(b).To(BeNil())

	graph.Resolve(&b)

	Expect(a).To(BeNil())

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

	expectedString := `&graph\{
  definitions: \[
    &definition\{
      ptr: \*test\.InterfaceA=0x.*,
      provider: &provider\{
        constructor: func\(test\.InterfaceB\) test\.InterfaceA,
        argPtrs: \[
          \*test\.InterfaceB=0x.*
        \]
      \},
      value: <nil>
    \},
    &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))
}
예제 #2
0
파일: oinker.go 프로젝트: jdef/oinker-go
func (o *Oinker) NewGraph() inject.Graph {
	graph := inject.NewGraph()

	var server *http.ServeMux
	graph.Define(&server, inject.NewProvider(http.NewServeMux))

	var cqlCluster *gocql.ClusterConfig
	graph.Define(&cqlCluster, inject.NewProvider(func() *gocql.ClusterConfig {
		//TODO: use DiscoverHosts?
		return gocql.NewCluster(o.CQLHosts...)
	}))

	var oinkRepo model.OinkRepo
	if len(o.CQLHosts) > 0 {
		graph.Define(&oinkRepo, inject.NewProvider(model.NewCQLOinkRepo, &cqlCluster))
	} else {
		graph.Define(&oinkRepo, inject.NewProvider(model.NewMockOinkRepo))
	}

	var assetsController *controller.AssetsController
	graph.Define(&assetsController, inject.NewProvider(controller.NewAssetsController))

	var indexController *controller.IndexController
	graph.Define(&indexController, inject.NewProvider(controller.NewIndexController, &oinkRepo))

	var oinkController *controller.OinkController
	graph.Define(&oinkController, inject.NewProvider(controller.NewOinkController, &oinkRepo))

	var analyticsController *controller.AnalyticsController
	graph.Define(&analyticsController, inject.NewProvider(controller.NewAnalyticsController, &oinkRepo))

	return graph
}
예제 #3
0
func TestGraphSupportsStructPointers(t *testing.T) {
	RegisterTestingT(t)

	graph := inject.NewGraph()

	var (
		d *ImplD
	)

	graph.Define(&d, inject.NewProvider(NewD))
	graph.ResolveAll()

	Expect(d).To(Equal(NewD()))
	Expect(d.String()).To(Equal("&ImplD{}"))

	expectedString := `&graph\{
  definitions: \[
    &definition\{
      ptr: \*\*test\.ImplD=0x.*,
      provider: &provider\{
        constructor: func\(\) \*test\.ImplD,
        argPtrs: \[\]
      \},
      value: <\*test\.ImplD Value>
    \}
  \]
\}`
	Expect(graph.String()).To(MatchRegexp(expectedString))
}
예제 #4
0
func TestGraphSupportsInterfaces(t *testing.T) {
	RegisterTestingT(t)

	graph := inject.NewGraph()

	var (
		c InterfaceC
	)

	graph.Define(&c, inject.NewProvider(NewC))
	graph.ResolveAll()

	Expect(c).To(Equal(NewC()))
	Expect(c.String()).To(Equal("&implC{}"))

	expectedString := `&graph\{
  definitions: \[
    &definition\{
      ptr: \*test\.InterfaceC=0x.*,
      provider: &provider\{
        constructor: func\(\) test\.InterfaceC,
        argPtrs: \[\]
      \},
      value: <test\.InterfaceC Value>
    \}
  \]
\}`
	Expect(graph.String()).To(MatchRegexp(expectedString))
}
예제 #5
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)
}
예제 #6
0
func TestExtractAssignable(t *testing.T) {
	RegisterTestingT(t)

	graph := inject.NewGraph()

	var (
		a1 *alpha
		g1 *gamma
	)

	graph.Define(&a1, inject.NewProvider(func() *alpha { return &alpha{name: "a1"} }))
	graph.Define(&g1, inject.NewProvider(func() *gamma { return &gamma{name: "g1"} }))

	var o omega
	inject.ExtractAssignable(graph, &o)

	Expect(o).To(Equal(&alpha{name: "a1"}))
}
예제 #7
0
func TestExtractByTypeNoMatch(t *testing.T) {
	RegisterTestingT(t)

	graph := inject.NewGraph()

	var (
		b1 *beta
		g1 *gamma
	)

	graph.Define(&b1, inject.NewProvider(func() *beta { return &beta{name: "b1"} }))
	graph.Define(&g1, inject.NewProvider(func() *gamma { return &gamma{name: "g1"} }))

	var a *alpha

	defer ExpectPanic("no defined pointer matches the specified type")
	inject.ExtractByType(graph, &a)
}
예제 #8
0
func (o *Oinker) NewGraph() inject.Graph {
	graph := inject.NewGraph()

	var instanceName string
	graph.Define(&instanceName, inject.NewProvider(func() string {
		name := os.Getenv("OINKER_INSTANCE_NAME")
		if name == "" {
			return "instance-unknown"
		}
		return name
	}))

	var server *http.ServeMux
	graph.Define(&server, inject.NewProvider(http.NewServeMux))

	var cqlCluster *gocql.ClusterConfig
	graph.Define(&cqlCluster, inject.NewProvider(service.NewCQLCluster, &o.CQLHosts, &o.CQLReconnectInterval))

	var cqlSession *service.CQLSession
	graph.Define(&cqlSession, inject.NewProvider(service.NewCQLSession, &cqlCluster))

	var oinkRepo service.OinkRepo
	if len(o.CQLHosts) > 0 {
		graph.Define(&oinkRepo, inject.NewProvider(service.NewCQLOinkRepo, &cqlSession, &o.CQLReplicationFactor))
	} else {
		graph.Define(&oinkRepo, inject.NewProvider(service.NewMockOinkRepo))
	}

	var readyController *controller.ReadyController
	graph.Define(&readyController, inject.NewProvider(controller.NewReadyController, &oinkRepo, &instanceName))

	var assetsController *controller.AssetsController
	graph.Define(&assetsController, inject.NewProvider(controller.NewAssetsController))

	var indexController *controller.IndexController
	graph.Define(&indexController, inject.NewProvider(controller.NewIndexController, &oinkRepo, &instanceName))

	var oinkController *controller.OinkController
	graph.Define(&oinkController, inject.NewProvider(controller.NewOinkController, &oinkRepo))

	return graph
}
예제 #9
0
func TestGraphLifecycle(t *testing.T) {
	RegisterTestingT(t)

	var (
		i *initme
		f *finalme
		l *lifecycleme
	)

	graph := inject.NewGraph(
		inject.NewDefinition(&i, inject.NewProvider(func() *initme { return &initme{} })),
		inject.NewDefinition(&f, inject.NewProvider(func() *finalme { return &finalme{} })),
		inject.NewDefinition(&l, inject.NewProvider(func() *lifecycleme { return &lifecycleme{} })),
	)

	Expect(i).To(BeNil())
	Expect(f).To(BeNil())
	Expect(l).To(BeNil())

	graph.ResolveAll()

	// defined pointer values will be constructed and initialized
	Expect(i).To(Equal(&initme{initialized: true}))
	Expect(f).To(Equal(&finalme{finalized: false}))
	Expect(l).To(Equal(&lifecycleme{initialized: true, finalized: false}))

	ii := i
	ff := f
	ll := l

	graph.Finalize()

	// defined pointers will be zeroed
	Expect(i).To(BeNil())
	Expect(f).To(BeNil())
	Expect(l).To(BeNil())

	// values pointed at by defined pointers will be finalized
	Expect(ii).To(Equal(&initme{initialized: true}))
	Expect(ff).To(Equal(&finalme{finalized: true}))
	Expect(ll).To(Equal(&lifecycleme{initialized: true, finalized: true}))
}
예제 #10
0
func TestExtractAssignableMultiMatch(t *testing.T) {
	RegisterTestingT(t)

	graph := inject.NewGraph()

	var (
		a1 *alpha
		b1 *beta
		g1 *gamma
	)

	graph.Define(&a1, inject.NewProvider(func() *alpha { return &alpha{name: "a1"} }))
	graph.Define(&b1, inject.NewProvider(func() *beta { return &beta{name: "b1"} }))
	graph.Define(&g1, inject.NewProvider(func() *gamma { return &gamma{name: "g1"} }))

	var o omega

	defer ExpectPanic("more than one defined pointer is assignable to the specified type")
	inject.ExtractAssignable(graph, &o)
}
예제 #11
0
func TestExtractByType(t *testing.T) {
	RegisterTestingT(t)

	graph := inject.NewGraph()

	var (
		a1 *alpha
		b1 *beta
		g1 *gamma
	)

	graph.Define(&a1, inject.NewProvider(func() *alpha { return &alpha{name: "a1"} }))
	graph.Define(&b1, inject.NewProvider(func() *beta { return &beta{name: "b1"} }))
	graph.Define(&g1, inject.NewProvider(func() *gamma { return &gamma{name: "g1"} }))

	var a *alpha
	inject.ExtractByType(graph, &a)

	Expect(a).To(Equal(&alpha{name: "a1"}))
}
예제 #12
0
func TestExtractAssignableNoMatch(t *testing.T) {
	RegisterTestingT(t)

	graph := inject.NewGraph()

	var (
		g1 *gamma
	)

	graph.Define(&g1, inject.NewProvider(func() *gamma { return &gamma{name: "g1"} }))

	var o omega

	defer ExpectPanic("no defined pointer is assignable to the specified type")
	inject.ExtractAssignable(graph, &o)
}
예제 #13
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))
}