本文整理匯總了Golang中github.com/facebookgo/inject.Graph.Populate方法的典型用法代碼示例。如果您正苦於以下問題:Golang Graph.Populate方法的具體用法?Golang Graph.Populate怎麽用?Golang Graph.Populate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/facebookgo/inject.Graph
的用法示例。
在下文中一共展示了Graph.Populate方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: 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")
}
}
示例2: 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
}
示例3: 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",
})
}
示例4: 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())
}
}
示例5: 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)
}
示例6: 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)
}
}
示例7: 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))
}
示例8: Initialize
func (p FBInjectInstanceInitializer) Initialize(instance interface{}, typeName string) (interface{}, error) {
var g inject.Graph
if err := g.Provide(&inject.Object{Value: instance}); err != nil {
return nil, err
}
if entry := p.initializers[typeName]; entry != nil {
for _, v := range entry.dependencies {
resolved, e1 := godi.Resolve(v)
if e1 != nil {
return nil, e1
}
obj := inject.Object{Value: resolved}
e2 := g.Provide(&obj)
if e2 != nil {
return nil, e2
}
}
}
// construct the instance.
//
if e3 := g.Populate(); e3 != nil {
return nil, e3
}
return instance, nil
}
示例9: newHarnessInternal
func newHarnessInternal(url string, s stopper, t testing.TB) *Harness {
replicaSet := ReplicaSet{
Addrs: url,
PortStart: 2000,
PortEnd: 3000,
MaxConnections: 5,
MinIdleConnections: 5,
ServerIdleTimeout: 5 * time.Minute,
ServerClosePoolSize: 5,
ClientIdleTimeout: 5 * time.Minute,
MaxPerClientConnections: 250,
GetLastErrorTimeout: 5 * time.Minute,
MessageTimeout: time.Minute,
}
log := tLogger{TB: t}
var graph inject.Graph
err := graph.Provide(
&inject.Object{Value: &log},
&inject.Object{Value: &replicaSet},
&inject.Object{Value: &stats.HookClient{}},
)
ensure.Nil(t, err)
ensure.Nil(t, graph.Populate())
objects := graph.Objects()
ensure.Nil(t, startstop.Start(objects, &log))
return &Harness{
T: t,
Stopper: s,
ReplicaSet: &replicaSet,
Graph: &graph,
Log: &log,
}
}
示例10: 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)
}
示例11: 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)
}
示例12: 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)
}
}
示例13: Main
func Main() error {
messageTimeout := flag.Duration("message_timeout", 2*time.Minute, "timeout for one message to be proxied")
clientIdleTimeout := flag.Duration("client_idle_timeout", 60*time.Minute, "idle timeout for client connections")
getLastErrorTimeout := flag.Duration("get_last_error_timeout", time.Minute, "timeout for getLastError pinning")
maxConnections := flag.Uint("max_connections", 100, "maximum number of connections per mongo")
portStart := flag.Int("port_start", 6000, "start of port range")
portEnd := flag.Int("port_end", 6010, "end of port range")
addrs := flag.String("addrs", "localhost:27017", "comma separated list of mongo addresses")
flag.Parse()
replicaSet := dvara.ReplicaSet{
Addrs: *addrs,
PortStart: *portStart,
PortEnd: *portEnd,
MessageTimeout: *messageTimeout,
ClientIdleTimeout: *clientIdleTimeout,
GetLastErrorTimeout: *getLastErrorTimeout,
MaxConnections: *maxConnections,
}
var statsClient stats.HookClient
var log stdLogger
var graph inject.Graph
err := graph.Provide(
&inject.Object{Value: &log},
&inject.Object{Value: &replicaSet},
&inject.Object{Value: &statsClient},
)
if err != nil {
return err
}
if err := graph.Populate(); err != nil {
return err
}
objects := graph.Objects()
// Temporarily setup the metrics against a test registry.
gregistry := gangliamr.NewTestRegistry()
for _, o := range objects {
if rmO, ok := o.Value.(registerMetrics); ok {
rmO.RegisterMetrics(gregistry)
}
}
if err := startstop.Start(objects, &log); err != nil {
return err
}
defer startstop.Stop(objects, &log)
ch := make(chan os.Signal, 2)
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT)
<-ch
signal.Stop(ch)
return nil
}
示例14: Main
func Main() error {
messageTimeout := flag.Duration("message_timeout", 2*time.Minute, "timeout for one message to be proxied")
clientIdleTimeout := flag.Duration("client_idle_timeout", 60*time.Minute, "idle timeout for client connections")
serverIdleTimeout := flag.Duration("server_idle_timeout", 1*time.Hour, "idle timeout for server connections")
serverClosePoolSize := flag.Uint("server_close_pool_size", 100, "number of goroutines that will handle closing server connections")
getLastErrorTimeout := flag.Duration("get_last_error_timeout", time.Minute, "timeout for getLastError pinning")
maxPerClientConnections := flag.Uint("max_per_client_connections", 100, "maximum number of connections per client")
maxConnections := flag.Uint("max_connections", 100, "maximum number of connections per mongo")
portStart := flag.Int("port_start", 6000, "start of port range")
portEnd := flag.Int("port_end", 6010, "end of port range")
addrs := flag.String("addrs", "localhost:27017", "comma separated list of mongo addresses")
flag.Parse()
replicaSet := dvara.ReplicaSet{
Addrs: *addrs,
PortStart: *portStart,
PortEnd: *portEnd,
MessageTimeout: *messageTimeout,
ClientIdleTimeout: *clientIdleTimeout,
ServerIdleTimeout: *serverIdleTimeout,
ServerClosePoolSize: *serverClosePoolSize,
GetLastErrorTimeout: *getLastErrorTimeout,
MaxConnections: *maxConnections,
MaxPerClientConnections: *maxPerClientConnections,
}
var statsClient stats.HookClient
var log stdLogger
var graph inject.Graph
err := graph.Provide(
&inject.Object{Value: &log},
&inject.Object{Value: &replicaSet},
&inject.Object{Value: &statsClient},
)
if err != nil {
return err
}
if err := graph.Populate(); err != nil {
return err
}
objects := graph.Objects()
if err := startstop.Start(objects, &log); err != nil {
return err
}
defer startstop.Stop(objects, &log)
ch := make(chan os.Signal, 2)
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT)
<-ch
signal.Stop(ch)
return nil
}
示例15: Injector
func Injector(params ...interface{}) (err error) {
var g inject.Graph
for _, v := range params {
if err = g.Provide(&inject.Object{Value: v}); err != nil {
return
}
}
if err = g.Populate(); err != nil {
return
}
return
}