Skip to content

Instantly share code, notes, and snippets.

View liampulles's full-sized avatar

Liam Pulles liampulles

View GitHub Profile
@liampulles
liampulles / wire.go
Created July 9, 2022 18:02
You don't need a dependency injection library
func main() {
server := Wire()
server.Run()
}
func Wire() *http.Server {
cfgProvider := config.NewProvider()
svc := usecase.NewService(cfgProvider)
return http.NewServer(cfgProvider, svc)
}
@liampulles
liampulles / Makefile
Last active July 9, 2022 16:47
Makefile tool check and install
GOBIN := $(shell go env GOPATH)/bin
inspect: $(GOBIN)/golangci-lint
$(GOBIN)/golangci-lint run
$(GOBIN)/golangci-lint:
$(MAKE) install-golangci-lint
install-golangci-lint:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOBIN) v1.46.2
rm -rf ./v1.46.2
@liampulles
liampulles / golang-implementation-check-example.go
Last active July 9, 2022 16:28
Golang implementation check example
type Hashable interface {
Hash() []byte
}
type BasicThingy string
type StructThingy struct{}
// Implementation checks:
var _ Hashable = (BasicThingy)("")
var _ Hashable = &StructThingy{}
@liampulles
liampulles / design-by-contract-example.java
Created July 9, 2022 14:52
Design by contract example
public sendSms(String cell) {
assert PhoneValidationUtil.ValidNumber(cell);
// ...
}
// IsAvailable will return true if the inventory item may
// be checked out - false otherwise.
func (i *InventoryItemImpl) IsAvailable() bool {
return i.available
}
// Checkout will mark the inventory item as unavilable.
// If the inventory item is not available,
// then an error is returned.
func (i *InventoryItemImpl) Checkout() error {
// FromInventoryItemView converts a view to JSON
func (e *EncoderServiceImpl) FromInventoryItemView(view *inventory.ViewVO) ([]byte, error) {
intermediary := mapViewIntermediary(view)
bytes, err := json.Marshal(intermediary)
if err != nil {
return nil, fmt.Errorf("could not convert inventory item view to json - marshal error: %w", err)
}
return bytes, nil
}
@liampulles
liampulles / matchstick-video-wire.go
Created April 18, 2022 09:42
Example of wiring from Matchstick Video
// CreateServerFactory injects all the dependencies needed to create
// http.ServerFactory
func CreateServerFactory(source goConfig.Source) (http.ServerFactory, error) {
// Each "tap" below indicates a level of dependency
configStore, err := config.NewStoreImpl(
source,
)
if err != nil {
return nil, err
}
@liampulles
liampulles / pool-setup.sh
Last active October 11, 2021 13:47
pool-setup.sh
apt install make