mirror of
https://github.com/safing/portbase
synced 2025-04-22 02:09:09 +00:00
Fix linter errors
This commit is contained in:
parent
5accaad794
commit
3c697abd5b
9 changed files with 95 additions and 98 deletions
container
database
modules
notifications
runtime
utils
|
@ -5,23 +5,22 @@
|
||||||
// Byte slices added to the Container are not changed or appended, to not corrupt any other data that may be before and after the given slice.
|
// Byte slices added to the Container are not changed or appended, to not corrupt any other data that may be before and after the given slice.
|
||||||
// If interested, consider the following example to understand why this is important:
|
// If interested, consider the following example to understand why this is important:
|
||||||
//
|
//
|
||||||
// package main
|
// package main
|
||||||
//
|
//
|
||||||
// import (
|
// import (
|
||||||
// "fmt"
|
// "fmt"
|
||||||
// )
|
// )
|
||||||
//
|
//
|
||||||
// func main() {
|
// func main() {
|
||||||
// a := []byte{0, 1,2,3,4,5,6,7,8,9}
|
// a := []byte{0, 1,2,3,4,5,6,7,8,9}
|
||||||
// fmt.Printf("a: %+v\n", a)
|
// fmt.Printf("a: %+v\n", a)
|
||||||
// fmt.Printf("\nmaking changes...\n(we are not changing a directly)\n\n")
|
// fmt.Printf("\nmaking changes...\n(we are not changing a directly)\n\n")
|
||||||
// b := a[2:6]
|
// b := a[2:6]
|
||||||
// c := append(b, 10, 11)
|
// c := append(b, 10, 11)
|
||||||
// fmt.Printf("b: %+v\n", b)
|
// fmt.Printf("b: %+v\n", b)
|
||||||
// fmt.Printf("c: %+v\n", c)
|
// fmt.Printf("c: %+v\n", c)
|
||||||
// fmt.Printf("a: %+v\n", a)
|
// fmt.Printf("a: %+v\n", a)
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// run it here: https://play.golang.org/p/xu1BXT3QYeE
|
// run it here: https://play.golang.org/p/xu1BXT3QYeE
|
||||||
//
|
|
||||||
package container
|
package container
|
||||||
|
|
|
@ -1,63 +1,62 @@
|
||||||
/*
|
/*
|
||||||
Package database provides a universal interface for interacting with the database.
|
Package database provides a universal interface for interacting with the database.
|
||||||
|
|
||||||
A Lazy Database
|
# A Lazy Database
|
||||||
|
|
||||||
The database system can handle Go structs as well as serialized data by the dsd package.
|
The database system can handle Go structs as well as serialized data by the dsd package.
|
||||||
While data is in transit within the system, it does not know which form it currently has. Only when it reaches its destination, it must ensure that it is either of a certain type or dump it.
|
While data is in transit within the system, it does not know which form it currently has. Only when it reaches its destination, it must ensure that it is either of a certain type or dump it.
|
||||||
|
|
||||||
Record Interface
|
# Record Interface
|
||||||
|
|
||||||
The database system uses the Record interface to transparently handle all types of structs that get saved in the database. Structs include the Base struct to fulfill most parts of the Record interface.
|
The database system uses the Record interface to transparently handle all types of structs that get saved in the database. Structs include the Base struct to fulfill most parts of the Record interface.
|
||||||
|
|
||||||
Boilerplate Code:
|
Boilerplate Code:
|
||||||
|
|
||||||
type Example struct {
|
type Example struct {
|
||||||
record.Base
|
record.Base
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
|
|
||||||
Name string
|
Name string
|
||||||
Score int
|
Score int
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
db = database.NewInterface(nil)
|
db = database.NewInterface(nil)
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetExample gets an Example from the database.
|
// GetExample gets an Example from the database.
|
||||||
func GetExample(key string) (*Example, error) {
|
func GetExample(key string) (*Example, error) {
|
||||||
r, err := db.Get(key)
|
r, err := db.Get(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// unwrap
|
// unwrap
|
||||||
if r.IsWrapped() {
|
if r.IsWrapped() {
|
||||||
// only allocate a new struct, if we need it
|
// only allocate a new struct, if we need it
|
||||||
new := &Example{}
|
new := &Example{}
|
||||||
err = record.Unwrap(r, new)
|
err = record.Unwrap(r, new)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return new, nil
|
return new, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// or adjust type
|
// or adjust type
|
||||||
new, ok := r.(*Example)
|
new, ok := r.(*Example)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("record not of type *Example, but %T", r)
|
return nil, fmt.Errorf("record not of type *Example, but %T", r)
|
||||||
}
|
}
|
||||||
return new, nil
|
return new, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Example) Save() error {
|
func (e *Example) Save() error {
|
||||||
return db.Put(e)
|
return db.Put(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Example) SaveAs(key string) error {
|
|
||||||
e.SetKey(key)
|
|
||||||
return db.PutNew(e)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
func (e *Example) SaveAs(key string) error {
|
||||||
|
e.SetKey(key)
|
||||||
|
return db.PutNew(e)
|
||||||
|
}
|
||||||
*/
|
*/
|
||||||
package database
|
package database
|
||||||
|
|
|
@ -14,6 +14,7 @@ type snippet struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseQuery parses a plaintext query. Special characters (that must be escaped with a '\') are: `\()` and any whitespaces.
|
// ParseQuery parses a plaintext query. Special characters (that must be escaped with a '\') are: `\()` and any whitespaces.
|
||||||
|
//
|
||||||
//nolint:gocognit
|
//nolint:gocognit
|
||||||
func ParseQuery(query string) (*Query, error) {
|
func ParseQuery(query string) (*Query, error) {
|
||||||
snippets, err := extractSnippets(query)
|
snippets, err := extractSnippets(query)
|
||||||
|
|
|
@ -55,14 +55,13 @@ func (m *Module) EnabledAsDependency() bool {
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// EnableModuleManagement(func(m *modules.Module) {
|
// EnableModuleManagement(func(m *modules.Module) {
|
||||||
// // some module has changed ...
|
// // some module has changed ...
|
||||||
// // do what ever you like
|
// // do what ever you like
|
||||||
//
|
|
||||||
// // Run the built-in module management
|
|
||||||
// modules.ManageModules()
|
|
||||||
// })
|
|
||||||
//
|
//
|
||||||
|
// // Run the built-in module management
|
||||||
|
// modules.ManageModules()
|
||||||
|
// })
|
||||||
func EnableModuleManagement(changeNotifyFn func(*Module)) bool {
|
func EnableModuleManagement(changeNotifyFn func(*Module)) bool {
|
||||||
if moduleMgmtEnabled.SetToIf(false, true) {
|
if moduleMgmtEnabled.SetToIf(false, true) {
|
||||||
modulesChangeNotifyFn = changeNotifyFn
|
modulesChangeNotifyFn = changeNotifyFn
|
||||||
|
|
|
@ -125,11 +125,11 @@ func (mng *Manager) Get(keyOrPrefix string) ([]record.Record, error) {
|
||||||
// you. Pass a nil option to force enable.
|
// you. Pass a nil option to force enable.
|
||||||
//
|
//
|
||||||
// TODO(ppacher): IMHO the subsystem package is not responsible of registering
|
// TODO(ppacher): IMHO the subsystem package is not responsible of registering
|
||||||
// the "toggle option". This would also remove runtime
|
// the "toggle option". This would also remove runtime
|
||||||
// dependency to the config package. Users should either pass
|
// dependency to the config package. Users should either pass
|
||||||
// the BoolOptionFunc and the expertise/release level directly
|
// the BoolOptionFunc and the expertise/release level directly
|
||||||
// or just pass the configuration key so those information can
|
// or just pass the configuration key so those information can
|
||||||
// be looked up by the registry.
|
// be looked up by the registry.
|
||||||
func (mng *Manager) Register(id, name, description string, module *modules.Module, configKeySpace string, option *config.Option) error {
|
func (mng *Manager) Register(id, name, description string, module *modules.Module, configKeySpace string, option *config.Option) error {
|
||||||
mng.l.Lock()
|
mng.l.Lock()
|
||||||
defer mng.l.Unlock()
|
defer mng.l.Unlock()
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
Package notifications provides a notification system.
|
Package notifications provides a notification system.
|
||||||
|
|
||||||
Notification Lifecycle
|
# Notification Lifecycle
|
||||||
|
|
||||||
1. Create Notification with an ID and Message.
|
1. Create Notification with an ID and Message.
|
||||||
2. Set possible actions and save it.
|
2. Set possible actions and save it.
|
||||||
|
@ -9,19 +9,18 @@ Notification Lifecycle
|
||||||
|
|
||||||
Example
|
Example
|
||||||
|
|
||||||
// create notification
|
// create notification
|
||||||
n := notifications.New("update-available", "A new update is available. Restart to upgrade.")
|
n := notifications.New("update-available", "A new update is available. Restart to upgrade.")
|
||||||
// set actions and save
|
// set actions and save
|
||||||
n.AddAction("later", "Later").AddAction("restart", "Restart now!").Save()
|
n.AddAction("later", "Later").AddAction("restart", "Restart now!").Save()
|
||||||
|
|
||||||
// wait for user action
|
|
||||||
selectedAction := <-n.Response()
|
|
||||||
switch selectedAction {
|
|
||||||
case "later":
|
|
||||||
log.Infof("user wants to upgrade later.")
|
|
||||||
case "restart":
|
|
||||||
log.Infof("user wants to restart now.")
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// wait for user action
|
||||||
|
selectedAction := <-n.Response()
|
||||||
|
switch selectedAction {
|
||||||
|
case "later":
|
||||||
|
log.Infof("user wants to upgrade later.")
|
||||||
|
case "restart":
|
||||||
|
log.Infof("user wants to restart now.")
|
||||||
|
}
|
||||||
*/
|
*/
|
||||||
package notifications
|
package notifications
|
||||||
|
|
|
@ -15,17 +15,16 @@ type singleRecordReader struct {
|
||||||
//
|
//
|
||||||
// Example:
|
// Example:
|
||||||
//
|
//
|
||||||
// type MyValue struct {
|
// type MyValue struct {
|
||||||
// record.Base
|
// record.Base
|
||||||
// Value string
|
// Value string
|
||||||
// }
|
// }
|
||||||
// r := new(MyValue)
|
// r := new(MyValue)
|
||||||
// pushUpdate, _ := runtime.Register("my/key", ProvideRecord(r))
|
// pushUpdate, _ := runtime.Register("my/key", ProvideRecord(r))
|
||||||
// r.Lock()
|
// r.Lock()
|
||||||
// r.Value = "foobar"
|
// r.Value = "foobar"
|
||||||
// pushUpdate(r)
|
// pushUpdate(r)
|
||||||
// r.Unlock()
|
// r.Unlock()
|
||||||
//
|
|
||||||
func ProvideRecord(r record.Record) ValueProvider {
|
func ProvideRecord(r record.Record) ValueProvider {
|
||||||
return &singleRecordReader{r}
|
return &singleRecordReader{r}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,9 @@ type OnceAgain struct {
|
||||||
|
|
||||||
// Do calls the function f if and only if Do is being called for the
|
// Do calls the function f if and only if Do is being called for the
|
||||||
// first time for this instance of Once. In other words, given
|
// first time for this instance of Once. In other words, given
|
||||||
// var once Once
|
//
|
||||||
|
// var once Once
|
||||||
|
//
|
||||||
// if once.Do(f) is called multiple times, only the first call will invoke f,
|
// if once.Do(f) is called multiple times, only the first call will invoke f,
|
||||||
// even if f has a different value in each invocation. A new instance of
|
// even if f has a different value in each invocation. A new instance of
|
||||||
// Once is required for each function to execute.
|
// Once is required for each function to execute.
|
||||||
|
@ -32,14 +34,14 @@ type OnceAgain struct {
|
||||||
// Do is intended for initialization that must be run exactly once. Since f
|
// Do is intended for initialization that must be run exactly once. Since f
|
||||||
// is niladic, it may be necessary to use a function literal to capture the
|
// is niladic, it may be necessary to use a function literal to capture the
|
||||||
// arguments to a function to be invoked by Do:
|
// arguments to a function to be invoked by Do:
|
||||||
// config.once.Do(func() { config.init(filename) })
|
//
|
||||||
|
// config.once.Do(func() { config.init(filename) })
|
||||||
//
|
//
|
||||||
// Because no call to Do returns until the one call to f returns, if f causes
|
// Because no call to Do returns until the one call to f returns, if f causes
|
||||||
// Do to be called, it will deadlock.
|
// Do to be called, it will deadlock.
|
||||||
//
|
//
|
||||||
// If f panics, Do considers it to have returned; future calls of Do return
|
// If f panics, Do considers it to have returned; future calls of Do return
|
||||||
// without calling f.
|
// without calling f.
|
||||||
//
|
|
||||||
func (o *OnceAgain) Do(f func()) {
|
func (o *OnceAgain) Do(f func()) {
|
||||||
// Note: Here is an incorrect implementation of Do:
|
// Note: Here is an incorrect implementation of Do:
|
||||||
//
|
//
|
||||||
|
|
|
@ -9,7 +9,6 @@ import "sync"
|
||||||
// A StablePool is a set of temporary objects that may be individually saved and
|
// A StablePool is a set of temporary objects that may be individually saved and
|
||||||
// retrieved.
|
// retrieved.
|
||||||
//
|
//
|
||||||
//
|
|
||||||
// In contrast to sync.Pool, items are not removed automatically. Every item
|
// In contrast to sync.Pool, items are not removed automatically. Every item
|
||||||
// will be returned at some point. Items are returned in a FIFO manner in order
|
// will be returned at some point. Items are returned in a FIFO manner in order
|
||||||
// to evenly distribute usage of a set of items.
|
// to evenly distribute usage of a set of items.
|
||||||
|
|
Loading…
Add table
Reference in a new issue