
Security News
Opengrep Adds Apex Support and New Rule Controls in Latest Updates
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.
github.com/logrange/linker
Linker is Dependency Injection and Inversion of Control package. It supports the following features:
PostConstructor
, Initializer
and Shutdowner
interfaces implementationsPlease refer to this blogpost for some details.
Linker is used by Logrange, please take a look how it is used there.
import (
"github.com/logrange/linker"
)
type DatabaseAccessService interface {
RunQuery(query string) DbResult
}
// MySQLAccessService implements DatabaseAccessService
type MySQLAccessService struct {
// Conns uses field's tag to specify injection param name(mySqlConns)
// or sets-up the default value(32), if the param is not provided
Conns int `inject:"mySqlConns, optional:32"`
}
type BigDataService struct {
// DBa has DatabaseAccessService type which value will be injected by the injector
// in its Init() function, or it fails if there is no appropriate component with the name(dba)
// was registered...
DBa DatabaseAccessService `inject:"dba"`
}
...
func main() {
// 1st step is to create the injector
inj := linker.New()
// 2nd step is to register components
inj.Register(
linker.Component{Name: "dba", Value: &MySQLAccessService{}},
linker.Component{Name: "", Value: &BigDataService{}},
linker.Component{Name: "mySqlConns", Value: int(msconns)},
...
)
// 3rd step is to inject dependecies and initialize the registered components
inj.Init(ctx)
// the injector fails-fast, so if no panic everything is good so far.
...
// 4th de-initialize all compoments properly
inj.Shutdown()
}
The inject
tag field has the following format:
inject: "<name>[,optional[:<defaultValue]]"
So annotated fields can be assigned using different rules:
// Field will be assigned by component with registration name "compName",
// if there is no comonent with the name, or it could not be assigned to the type
// FieldType, panic will happen
Field FieldType `inject:"compName"`
// Field will be assigned by component with any name (indicated as ""), which could be
// assigned to the FieldType. If no such component or many matches to the type,
// panic will happen.
Field FieldType `inject:""`
// If no components match to either Field1 or Field2 they will be skipped with
// no panic. Ambigious situation still panics
Field1 FieldType `inject:"aaa, optional"`
Field2 FieldType `inject:", optional"`
// Default values could be provided for numeric and string types. The
// default values will be assigned if no components match to the rules
NumFld int `inject:"intFld, optional: 21"`
StrFld string `inject:"strFld,optional:abc"`
Injector is a main object, which controls the components: registers them, initializes, checks and provides initialization and shutdown calls.
inj := linker.New()
Component is an object that can be used for initialization of other components, or which requires an initialization. Components can have different types, but only fields of components, with 'pointer to struct' type, could be assigned by the Injector. Injector is responsible for the injection(initialization a component's fields) process. All components must be registered in injector via Register()
function before the initialization process will be run.
When all components are registered Init()
function of the Injector
allows to perform initialization. The Init()
function does the following actions:
Init()
can panic.PostConstruct()
function will be called.Init(ctx)
function will be called in a specific order. The initialization order is defined as following: less dependent components are initialized before the componentsProperly initialized components could be shut-down in back-initialization order by calling Shutdown()
function of the injector. Components, that implement linker.Shutdowner interface, will be called by the Shutdown()
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.
Security News
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.