本文整理匯總了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")
}
示例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())
}
}
示例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
}
示例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)
}
}
示例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)
}
示例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))
}
示例7: TestProvideNonPointer
func TestProvideNonPointer() {
var g inject.Graph
var i *int
err := g.Provide(&inject.Object{Value: i})
fmt.Println(err)
}
示例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")
}
示例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)
}
示例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)
}
示例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",
})
}
示例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")
}
}
示例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)
}
}
示例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)
}
示例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)
}