Initial commit
This commit is contained in:
125
structful.go
Normal file
125
structful.go
Normal file
@@ -0,0 +1,125 @@
|
||||
package structful
|
||||
|
||||
import (
|
||||
"log"
|
||||
)
|
||||
|
||||
var structful *Structful
|
||||
|
||||
type IStructful interface {
|
||||
Within(version string) *Structful
|
||||
|
||||
Save(data map[string]any) error
|
||||
|
||||
GetByGroup(group string) []map[string]any
|
||||
|
||||
FilterByGroup(group string, filter map[string]any) []map[string]any
|
||||
|
||||
CheckHash(string) bool
|
||||
|
||||
List() ([]map[string]any, error)
|
||||
}
|
||||
|
||||
type Structful struct {
|
||||
IStructful
|
||||
|
||||
SysDb string
|
||||
|
||||
Name string
|
||||
|
||||
Version string
|
||||
|
||||
Adaptor IAdaptor
|
||||
}
|
||||
|
||||
func CreateStructful(props *StructfulProps) *Structful {
|
||||
if props.Adaptor == AdaptorType_Mongo {
|
||||
s := &Structful{
|
||||
Adaptor: CreateMongoAdaptor(props.SysDb, props.Name),
|
||||
Version: props.Version,
|
||||
Name: props.Name,
|
||||
SysDb: props.SysDb,
|
||||
}
|
||||
|
||||
return s
|
||||
} else {
|
||||
log.Fatal("unsuported adaptor")
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
type AdaptorType int
|
||||
|
||||
const (
|
||||
AdaptorType_Mongo = iota
|
||||
)
|
||||
|
||||
type StructfulProps struct {
|
||||
Adaptor AdaptorType
|
||||
SysDb string
|
||||
Name string
|
||||
Version string
|
||||
}
|
||||
|
||||
func(s *Structful) Save(data map[string]any) error {
|
||||
if err := validator.Validate(data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return s.Adaptor.Save(data)
|
||||
}
|
||||
|
||||
func (s *Structful) FilterByGroup(group string, filter map[string]any) ([]map[string]any, error) {
|
||||
filter["_group"] = group
|
||||
|
||||
return s.Adaptor.GetWithFilter(filter)
|
||||
}
|
||||
|
||||
func (s *Structful) GetByGroup(group string) ([]map[string]any, error) {
|
||||
return s.FilterByGroup(group, map[string]any{})
|
||||
}
|
||||
|
||||
func (s *Structful) CheckHash(hash string) bool {
|
||||
return s.Adaptor.CheckHash(hash)
|
||||
}
|
||||
|
||||
func (s *Structful) List() ([]map[string]any, error) {
|
||||
return s.Adaptor.List()
|
||||
}
|
||||
|
||||
func Current() *Structful {
|
||||
return structful
|
||||
}
|
||||
|
||||
func GetStructful(props *StructfulProps) *Structful {
|
||||
if props.Adaptor == AdaptorType_Mongo {
|
||||
return &Structful{
|
||||
Adaptor: CreateMongoAdaptor(props.SysDb, props.Name),
|
||||
SysDb: props.SysDb,
|
||||
Version: props.Version,
|
||||
Name: props.Name,
|
||||
}
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
type InitProps struct {
|
||||
MongoSysDb string
|
||||
Version string
|
||||
MongoStructfulCollection string
|
||||
}
|
||||
|
||||
func Init(props *InitProps) {
|
||||
sysDb := props.MongoSysDb
|
||||
version := props.Version
|
||||
name := props.MongoStructfulCollection
|
||||
|
||||
structful = GetStructful(&StructfulProps{
|
||||
Adaptor: AdaptorType_Mongo,
|
||||
SysDb: sysDb,
|
||||
Version: version,
|
||||
Name: name,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user