當前位置: 首頁>>代碼示例>>Golang>>正文


Golang inject.Graph類代碼示例

本文整理匯總了Golang中github.com/facebookgo/inject.Graph的典型用法代碼示例。如果您正苦於以下問題:Golang Graph類的具體用法?Golang Graph怎麽用?Golang Graph使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Graph類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestProvideWithFields

func TestProvideWithFields(t *testing.T) {
	var g inject.Graph
	a := &TypeAnswerStruct{}
	err := g.Provide(&inject.Object{Value: a, Fields: map[string]*inject.Object{}})
	ensure.NotNil(t, err)
	ensure.DeepEqual(t, err.Error(), "fields were specified on object *inject_test.TypeAnswerStruct when it was provided")
}
開發者ID:stellar,項目名稱:bridge-server,代碼行數:7,代碼來源:inject_test.go

示例2: Start

func Start(port int, fileLocation string) {

	var router routers.Router
	var graph inject.Graph

	// Create database
	db := data.NewJsonDB(fileLocation)

	// Setup DI
	if err := graph.Provide(
		&inject.Object{Value: db},
		&inject.Object{Value: repositories.NewCharacterRepository()},
		&inject.Object{Value: &router}); err != nil {
		log.Fatalf("Error providing dependencies: ", err.Error())
	}

	if err := graph.Populate(); err != nil {
		log.Fatalf("Error populating dependencies: ", err.Error())
	}

	n := negroni.Classic()
	n.UseHandler(router.NewRouter())

	err := http.ListenAndServe(fmt.Sprintf(":%v", port), n)

	if err != nil {
		panic("Error: " + err.Error())
	}
}
開發者ID:mattdotmatt,項目名稱:bigstar,代碼行數:29,代碼來源:server.go

示例3: NewApp

// NewApp constructs an new App instance from the provided config.
func NewApp(config config.Config, migrateFlag bool) (app *App, err error) {
	var g inject.Graph

	var driver db.Driver
	switch config.Database.Type {
	case "mysql":
		driver = &mysql.Driver{}
	case "postgres":
		driver = &postgres.Driver{}
	default:
		return nil, fmt.Errorf("%s database has no driver", config.Database.Type)
	}

	err = driver.Init(config.Database.URL)
	if err != nil {
		return
	}

	entityManager := db.NewEntityManager(driver)
	repository := db.NewRepository(driver)

	if migrateFlag {
		var migrationsApplied int
		migrationsApplied, err = driver.MigrateUp("compliance")
		if err != nil {
			return
		}

		log.Info("Applied migrations: ", migrationsApplied)
		os.Exit(0)
		return
	}

	requestHandler := handlers.RequestHandler{}

	err = g.Provide(
		&inject.Object{Value: &requestHandler},
		&inject.Object{Value: &config},
		&inject.Object{Value: &entityManager},
		&inject.Object{Value: &repository},
		&inject.Object{Value: &crypto.SignerVerifier{}},
		&inject.Object{Value: &stellartoml.Resolver{}},
		&inject.Object{Value: &federation.Resolver{}},
		&inject.Object{Value: &http.Client{}},
	)

	if err != nil {
		log.Fatal("Injector: ", err)
	}

	if err := g.Populate(); err != nil {
		log.Fatal("Injector: ", err)
	}

	app = &App{
		config:         config,
		requestHandler: requestHandler,
	}
	return
}
開發者ID:stellar,項目名稱:bridge-server,代碼行數:61,代碼來源:app.go

示例4: TestInjectLogging

func TestInjectLogging() {
	g := inject.Graph{
		Logger: &logger{
			Expected: []string{
				"provided *main.TypeForLoggingCreated named name_for_logging",
				"provided *main.TypeForLogging",
				"provided embedded *main.TypeForLoggingEmbedded",
				"created *main.TypeForLoggingCreated",
				"assigned newly created *main.TypeForLoggingCreated to field TypeForLoggingCreated in *main.TypeForLogging",
				"assigned existing *main.TypeForLoggingCreated to field TypeForLoggingCreated in *main.TypeForLoggingEmbedded",
				"assigned *main.TypeForLoggingCreated named name_for_logging to field TypeForLoggingCreatedNamed in *main.TypeForLoggingEmbedded",
				"made map for field Map in *main.TypeForLoggingEmbedded",
				"assigned existing *main.TypeForLoggingCreated to interface field TypeForLoggingInterface in *main.TypeForLoggingEmbedded",
			},
		},
	}
	var v TypeForLogging

	err := g.Provide(
		&inject.Object{Value: &TypeForLoggingCreated{}, Name: "name_for_logging"},
		&inject.Object{Value: &v},
	)
	if err != nil {
		fmt.Println(err)
	}
	if err := g.Populate(); err != nil {
		fmt.Println(err)
	}
}
開發者ID:uileyar,項目名稱:helloworld,代碼行數:29,代碼來源:injecttest.go

示例5: initiateTests

func initiateTests() {
	var router routers.Router
	var graph inject.Graph

	setupTestData()

	db := data.NewJsonDB("../data/testing/testingDB.json")

	if err := graph.Provide(
		&inject.Object{Value: db},
		&inject.Object{Value: repositories.NewCharacterRepository()},
		&inject.Object{Value: &router}); err != nil {
		log.Fatalf("Error providing dependencies: ", err.Error())
	}

	if err := graph.Populate(); err != nil {
		log.Fatalf("Error populating dependencies: ", err.Error())
	}

	n := negroni.Classic()

	n.UseHandler(router.NewRouter())

	ts = httptest.NewServer(n)
}
開發者ID:mattdotmatt,項目名稱:bigstar,代碼行數:25,代碼來源:server_test.go

示例6: StartMyApp

func StartMyApp(port int, dbName string) {

	var env Env.Env
	var g inject.Graph

	db, e := Data.NewDB(dbName)

	log.Println(e)

	err := g.Provide(
		&inject.Object{Value: &env},
		&inject.Object{Value: db},
	)

	if err != nil {
		log.Fatalf("Error providing dependencies: ", err.Error())
	}

	if err := g.Populate(); err != nil {
		log.Fatalf("Error providing dependencies: ", err.Error())
	}

	routes := Api.Routes(env)

	routes.Run(fmt.Sprintf(":%v", port))
}
開發者ID:FanszHub,項目名稱:test-site,代碼行數:26,代碼來源:Server.go

示例7: TestProvideNonPointer

func TestProvideNonPointer() {
	var g inject.Graph
	var i *int
	err := g.Provide(&inject.Object{Value: i})

	fmt.Println(err)

}
開發者ID:uileyar,項目名稱:helloworld,代碼行數:8,代碼來源:injecttest.go

示例8: ProvideWithFields

func ProvideWithFields() {
	var g inject.Graph
	a := &TypeAnswerStruct{}
	err := g.Provide(&inject.Object{Value: a, Fields: map[string]*inject.Object{}})

	fmt.Println(err)
	//ensure.NotNil(glog.Fatal, err)
	//ensure.DeepEqual(fmt, err.Error(), "fields were specified on object *inject_test.TypeAnswerStruct when it was provided")
}
開發者ID:uileyar,項目名稱:helloworld,代碼行數:9,代碼來源:injecttest.go

示例9: TestProvideTwoOfTheSame

func TestProvideTwoOfTheSame() {
	var g inject.Graph
	a := TypeAnswerStruct{}
	err := g.Provide(&inject.Object{Value: &a})
	fmt.Println(err)

	err = g.Provide(&inject.Object{Value: &a})
	fmt.Println(err)

}
開發者ID:uileyar,項目名稱:helloworld,代碼行數:10,代碼來源:injecttest.go

示例10: TestInjectNamedTwoSatisfyInterface

func TestInjectNamedTwoSatisfyInterface() {
	var g inject.Graph
	var v TypeInjectNamedTwoSatisfyInterface
	if err := g.Provide(&inject.Object{Name: "foo", Value: &v}); err != nil {
		fmt.Println(err)
	}

	err := g.Populate()
	fmt.Println(err)
}
開發者ID:uileyar,項目名稱:helloworld,代碼行數:10,代碼來源:injecttest.go

示例11: TestObjectString

func TestObjectString(t *testing.T) {
	var g inject.Graph
	a := &TypeNestedStruct{}
	if err := g.Provide(&inject.Object{Value: a, Name: "foo"}); err != nil {
		t.Fatal(err)
	}

	var c TypeForObjectString
	if err := g.Provide(&inject.Object{Value: &c}); err != nil {
		t.Fatal(err)
	}

	if err := g.Populate(); err != nil {
		t.Fatal(err)
	}

	var actual []string
	for _, o := range g.Objects() {
		actual = append(actual, fmt.Sprint(o))
	}

	ensure.SameElements(t, actual, []string{
		"*inject_test.TypeForObjectString",
		"*inject_test.TypeNestedStruct",
		"*inject_test.TypeNestedStruct named foo",
		"*inject_test.TypeAnswerStruct",
	})
}
開發者ID:stellar,項目名稱:bridge-server,代碼行數:28,代碼來源:inject_test.go

示例12: TestTwoNamedInstances

func TestTwoNamedInstances(t *testing.T) {
	var g inject.Graph
	a := &TypeAnswerStruct{}
	b := &TypeAnswerStruct{}
	if err := g.Provide(&inject.Object{Value: a, Name: "foo"}); err != nil {
		t.Fatal(err)
	}

	if err := g.Provide(&inject.Object{Value: b, Name: "bar"}); err != nil {
		t.Fatal(err)
	}

	var c struct {
		A *TypeAnswerStruct `inject:"foo"`
		B *TypeAnswerStruct `inject:"bar"`
	}
	if err := g.Provide(&inject.Object{Value: &c}); err != nil {
		t.Fatal(err)
	}

	if err := g.Populate(); err != nil {
		t.Fatal(err)
	}

	if c.A != a {
		t.Fatal("did not find expected c.A")
	}
	if c.B != b {
		t.Fatal("did not find expected c.B")
	}
}
開發者ID:stellar,項目名稱:bridge-server,代碼行數:31,代碼來源:inject_test.go

示例13: main

func main() {
	// command line flags
	confFile := flag.String("conf", "emerald.conf", "location of config-file")
	flag.Parse()
	conf := core.LoadConfiguration(*confFile)

	var app core.EmeraldApp
	mongoService := mongo.NewService(&conf)
	ircClient := irc.NewClient()

	var g inject.Graph
	err := g.Provide(
		&inject.Object{Value: &conf},
		&inject.Object{Value: &app},
		&inject.Object{Value: mongoService},
		&inject.Object{Value: ircClient},
	)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	if err := g.Populate(); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	//setup gin
	gin.SetMode(gin.ReleaseMode)
	router := gin.New()

	//Middlewares
	// router.Use(gin.Logger())
	router.Use(gin.Recovery())

	//static files
	router.Static("/assets", "./assets")
	router.StaticFile("/", "./assets/index.html")

	//register all controllers
	app.AddControllers(router)

	//Start all jobs
	app.StartJobs()

	// Listen and server on 0.0.0.0:8080
	addr := fmt.Sprintf(":%d", conf.Port)
	log.Printf("Emerald started port %v\n", addr)
	fmt.Printf("Emerald started port %v\n", addr)
	err = router.Run(addr)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}
開發者ID:kahoona77,項目名稱:emerald,代碼行數:55,代碼來源:main.go

示例14: TestObjectString

func TestObjectString() {
	var g inject.Graph

	a := &TypeNestedStruct{}
	if err := g.Provide(&inject.Object{Value: a, Name: "foo"}); err != nil {
		fmt.Println(err)
	}

	var c TypeForObjectString
	if err := g.Provide(&inject.Object{Value: &c}); err != nil {
		fmt.Println(err)
	}

	if err := g.Populate(); err != nil {
		fmt.Println(err)
	}

	a.A.answer = 100
	fmt.Printf("c.A.A = %#v, c.B.A = %#v\n", c.A.A, c.B.A)

	var actual []string
	for _, o := range g.Objects() {

		fmt.Printf("%#v\n", o)
		actual = append(actual, fmt.Sprint(o))
	}

	//fmt.Printf("%#v \n", actual)

}
開發者ID:uileyar,項目名稱:helloworld,代碼行數:30,代碼來源:injecttest.go

示例15: TestProvideTwoWithTheSameName

func TestProvideTwoWithTheSameName() {
	var g inject.Graph
	const name = "foo"
	a := TypeAnswerStruct{}
	err := g.Provide(&inject.Object{Value: &a, Name: name})
	fmt.Println(err)

	err = g.Provide(&inject.Object{Value: &a, Name: name})
	fmt.Println(err)

}
開發者ID:uileyar,項目名稱:helloworld,代碼行數:11,代碼來源:injecttest.go


注:本文中的github.com/facebookgo/inject.Graph類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。