Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
github.com/go-ozzo/ozzo-di
ozzo-di is a dependency injection (DI) container in Go language. It has the following features:
Go 1.2 or above.
Run the following command to install the package:
go get github.com/go-ozzo/ozzo-di
The following code snippet shows how you can use the DI container.
package main
import (
"fmt"
"reflect"
"github.com/go-ozzo/ozzo-di"
)
type Bar interface {
String() string
}
func test(bar Bar) {
fmt.Println(bar.String())
}
type Foo struct {
s string
}
func (f *Foo) String() string {
return f.s
}
type MyBar struct {
Bar `inject`
}
func main() {
// creating a DI container
c := di.NewContainer()
// register a Foo instance as the Bar interface type
c.RegisterAs(&Foo{"hello"}, di.InterfaceOf((*Bar)(nil)))
// &Foo{"hello"} will be injected as the Bar parameter for test()
c.Call(test)
// Output:
// hello
// create a MyBar object and inject its Bar field
bar := c.Make(reflect.TypeOf(&MyBar{})).(Bar)
fmt.Println(bar.String())
// Output:
// hello
}
di.Container
is a DI container that relies on types to determine what values should be used for
injection. In order for this to happen, you usually should register the types that need DI support.
di.Container
supports three kinds of type registration, as shown in the following code snippet:
c := di.NewContainer()
// 1. registering a concrete type:
// &Foo{"hello"} is registered as the corresponding concrete type (*Foo)
c.Register(&Foo{"hello"})
// 2. registering an interface:
// &Foo{"hello"} is registered as the Bar interface
c.RegisterAs(&Foo{"hello"}, di.InterfaceOf((*Bar)(nil)))
// concrete type (*Foo) is registered as the Bar interface
c.RegisterAs(reflect.TypeOf(&Foo{}), di.InterfaceOf((*Bar)(nil)))
// 3. registering a provider:
// a provider function is registered as the Bar interface.
// The provider function will be called when injecting Bar.
c.RegisterProvider(func(di.Container) interface{} {
return &Foo{"hello"}
}, di.InterfaceOf((*Bar)(nil)), true)
Tip: To specify an interface type during registration, use the helper function
di.InterfaceOf((*InterfaceName)(nil))
. For concrete types, use go reflection functionreflect.TypeOf(TypeName{})
.
di.Container
supports three types of value injection, as shown in the following code snippet:
// ...following the previous registration example...
type Composite struct {
Bar `inject:"true"`
}
// 1. struct field injection:
// Exported struct field tagged with `inject` and anonymous fields will be injected with values.
// Here Composite.Bar will be injected with &Foo{"hello"}
composite := &Composite{}
c.Inject(composite)
// 2. function parameter injection:
// Function parameters will be injected with values according to their types.
// Here bar will be injected with &Foo{"hello"}
func test(bar Bar) {
fmt.Println(bar.String())
}
c.Call(test)
// 3. making new instances:
// New struct instances can be created with their fields injected.
// Or a singleton instance may be returned.
foo := c.Make(reflect.TypeOf(&Foo{})).(*Foo) // returns the singleton &Foo{"hello"}
bar := c.Make(di.InterfaceOf((*Bar)(nil))).(*Bar) // returns the singleton &Foo{"hello"}
// returns a new Composite instance with Bar injected with the singleton &Foo{"hello"}
composite := c.Make(reflect.TypeOf(&Composite{})).(*Composite)
When injecting a previously registered type, if a value is already registered as that type, the value itself will be used for injection.
If a provider is registered as a type, the provider will be called whose result will be used for injection.
While registering a provider, you may use the third parameter for Container.RegisterProvider()
to indicate
whether the provider should be called every time the injection is needed or only the first time. If the
latter, the provider will only be called once and the same return result will be used for injection of
the corresponding registered type.
When injecting a value for a type T
that has not been registered, the following strategy will be taken:
*T
has been registered, the corresponding value will be dereferenced and returned;T
is a pointer type of P
, the pointer to the value injected for P
will be returned;T
is a struct type, a new instance will be created and its fields will be injected;T
is Slice, Map, or Chan, a new instance will be created and initialized;ozzo-di has referenced the implementation of codegansta/inject.
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.