# README
Portable Abstractions and Patterns for Golang
This module is a part of the Pip.Services polyglot microservices toolkit. It provides a set of basic patterns used in microservices or backend services. Also the module implemenets a reasonably thin abstraction layer over most fundamental functions across all languages supported by the toolkit to facilitate symmetric implementation.
The module contains the following packages:
- Commands - commanding and eventing patterns
- Config - configuration framework
- Convert - soft value converters
- Data - data patterns
- Errors - application errors
- Random - random data generators
- Refer - locator (IoC) pattern
- Reflect - reflection framework
- Run - execution framework
- Validate - validation framework
- Configuration
- Locator Pattern
- Component Lifecycle
- Data Patterns
- API Reference
- Change Log
- Get Help
- Contribute
Use
Get the package from the Github repository:
go get -u github.com/pip-services3-gox/pip-services3-commons-gox@latest
Then you are ready to start using the Pip.Services patterns to augment your backend code.
For instance, here is how you can implement a component, that receives configuration, get assigned references, can be opened and closed using the patterns from this module.
import (
"context"
"fmt"
"github.com/pip-services3-gox/pip-services3-commons-gox/config"
"github.com/pip-services3-gox/pip-services3-commons-gox/refer"
)
type MyComponentA struct {
param1 string
param2 int
anotherComponent MyComponentB
opened bool
}
func NewMyComponentA() *MyComponentA {
return &MyComponentA{
param1: "ABC",
param2: 123,
opened: false,
}
}
type MyComponentB struct{
// ...
}
func (c *MyComponentA) Configure(ctx context.Context, config *config.ConfigParams) {
c.param1 = config.GetAsStringWithDefault("param1", c.param1)
c.param2 = config.GetAsIntegerWithDefault("param2", c.param2)
}
func (c *MyComponentA) SetReferences(ctx context.Context, references refer.IReferences) {
res, err := references.GetOneRequired(refer.NewDescriptor("myservice", "mycomponent-b", "*", "*", "1.0"))
if err != nil {
panic(err)
}
c.anotherComponent = res.(MyComponentB)
}
func (c *MyComponentA) IsOpen() bool {
return c.opened
}
func (c *MyComponentA) Open(ctx context.Context, correlationId string) error {
c.opened = true
fmt.Println("MyComponentA has been opened.")
return nil
}
func (c *MyComponentA) Close(ctx context.Context, correlationId string) error {
c.opened = false
fmt.Println("MyComponentA has been closed.")
return nil
}
Then here is how the component can be used in the code
package main
import (
"context"
"fmt"
"github.com/pip-services3-gox/pip-services3-commons-gox/config"
"github.com/pip-services3-gox/pip-services3-commons-gox/refer"
)
func main() {
myComponentA := NewMyComponentA()
// Configure the component
myComponentA.Configure(context.Background(), config.NewConfigParamsFromTuples(
"param1", "XYZ",
"param2", 987,
))
// Set references to the component
myComponentA.SetReferences(context.Background(),
refer.NewReferencesFromTuples(context.Background(),
refer.NewDescriptor("myservice", "mycomponent-b", "default", "default", "1.0"), &MyComponentB{},
),
)
// Open the component
err := myComponentA.Open(context.Background(), "123")
if err != nil {
panic(err)
} else {
fmt.Println("MyComponentA has been opened.")
}
}
Develop
For development you shall install the following prerequisites:
- Golang v1.18+
- Visual Studio Code or another IDE of your choice
- Docker
- Git
Run automated tests:
go test -v ./test/...
Generate API documentation:
./docgen.ps1
Before committing changes run dockerized test as:
./test.ps1
./clear.ps1
Contacts
The library is created and maintained by Sergey Seroukhov.
The documentation is written by Danyil Tretiakov and Levichev Dmitry.