https://github.com/xchapter7x/ineed
easily give your structs and constructors what they need
https://github.com/xchapter7x/ineed
Last synced: 4 months ago
JSON representation
easily give your structs and constructors what they need
- Host: GitHub
- URL: https://github.com/xchapter7x/ineed
- Owner: xchapter7x
- License: apache-2.0
- Created: 2015-12-20T20:02:45.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-12-21T14:55:44.000Z (over 10 years ago)
- Last Synced: 2025-02-26T03:35:43.768Z (over 1 year ago)
- Language: Go
- Homepage:
- Size: 156 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
#I Need
## easily give your structs and constructors what they need
[](https://app.wercker.com/project/bykey/92296b904aa18774452c79ab3c3c295d)
[](https://coveralls.io/github/xchapter7x/ineed?branch=master)
### Written because:
* all i need is a clean way to invert control
* i want to keep as close to idiomatic go struct initialization as possible
* i dont want struct element tags (thats way too much like annotations for me)
* I dont want a over the top DI framework
* I dont want be required to have a 100 argument 'New' constructor functions in my packages
* I want to just be able to pass in fakes or real objects to initialize my structs with
### Examples:
```
package main
import (
"fmt"
i "github.com/xchapter7x/ineed"
)
func NewFromType(deps i.Need) *Something {
s := new(Something)
deps.CastInto(s)
return s
}
func NewWithUnexported(deps i.Need) *Something {
s := &Something{
randomPriv: deps.Get("randomPriv").(string),
}
deps.MapInto(s)
return s
}
func New(deps i.Need) *Something {
s := new(Something)
deps.MapInto(s)
return s
}
type Something struct {
randomPriv string
RandomPub string
Cool CoolObject
}
type CoolObject struct {
AField string
AnotherField string
}
func (s Something) PrintAll() {
fmt.Println(s.randomPriv)
fmt.Println(s.RandomPub)
fmt.Println(s.Cool)
}
func main() {
deps := i.Want().
ToMap("RandomPub", "i am public").
ToMap("randomPriv", "i am private")
coolDep := CoolObject{
AField: "inject me",
AnotherField: "Don't forget me too",
}
blindDeps := i.Want().
ToUse(coolDep)
something := New(deps)
something.PrintAll()
somethingPrivate := NewWithUnexported(deps)
somethingPrivate.PrintAll()
somethingWithRandomNamedFields := NewFromType(blindDeps)
somethingWithRandomNamedFields.PrintAll()
}
```