Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fgm committed Sep 11, 2022
0 parents commit 22b73ab
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.idea
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2022 Frédéric G. MARAND <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module code.osinet.fr/fgm/izidic

go 1.19
94 changes: 94 additions & 0 deletions izidic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Package izidic defines a tiny dependency injection container,
// loosely inspired by a subset of silexphp/pimple.
//
// Like Pimple, the basic feature is that storing service definitions does not
// create instances, allowing users to store definitions of services requiring
// other services before those are actually defined.
package izidic

import (
"errors"
"fmt"
"sync"
)

// Service is the type used to define container serviceDefs accessors.
//
// It takes an instance of the container and returns an instance of the desired service,
// which should then be type-asserted before use.
//
// Any access to a service from the container returns the same instance.
type Service func(dic *Container) (any, error)

// Container is the container, holding both parameters and services
type Container struct {
sync.Mutex
parameters map[string]any
serviceDefs map[string]Service
services map[string]any
}

// Register registers a service with the container.
func (dic *Container) Register(name string, fn Service) {
dic.Lock()
defer dic.Unlock()
dic.serviceDefs[name] = fn
}

// Store stores a parameter in the container.
func (dic *Container) Store(name string, param any) {
dic.Lock()
defer dic.Unlock()
dic.parameters[name] = param
}

// Service returns the single instance of the requested service on success.
func (dic *Container) Service(name string) (any, error) {
dic.Lock()
defer dic.Unlock()

// Reuse existing instance if any.
instance, found := dic.services[name]
if found {
return instance, nil
}

// Otherwise instantiate.
service, found := dic.serviceDefs[name]
if !found {
return nil, fmt.Errorf("service %s not found", name)
}
instance, err := service(dic)
if err != nil {
return nil, fmt.Errorf("failed instantiating service %s: %w", name, err)
}
dic.services[name] = instance

return instance, nil
}

func (dic *Container) MustService(name string) any {
instance, err := dic.Service(name)
if err != nil {
panic(err)
}
return instance
}

func (dic *Container) Param(name string) (any, error) {
dic.Lock()
defer dic.Unlock()
p, found := dic.parameters[name]
if !found {
return nil, errors.New("parameter not found")
}
return p, nil
}

func (dic *Container) MustParam(name string) any {
p, err := dic.Param(name)
if err != nil {
panic(err)
}
return p
}

0 comments on commit 22b73ab

Please sign in to comment.