Add AddFormatter function to allow custom builds to register new formatters

This commit is contained in:
Dan Jones 2026-03-09 16:11:53 -05:00
commit 17a1cf1ade
5 changed files with 60 additions and 31 deletions

View file

@ -10,6 +10,8 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
const FormatJSON string = "json"
func newJson(ff *viper.Viper) (Formatter, error) { func newJson(ff *viper.Viper) (Formatter, error) {
js := new(Json) js := new(Json)
err := ff.Unmarshal(js) err := ff.Unmarshal(js)
@ -24,7 +26,7 @@ type Json struct {
} }
func (js *Json) Name() string { func (js *Json) Name() string {
return "json" return FormatJSON
} }
func (js *Json) marshal(v any) (o []byte, err error) { func (js *Json) marshal(v any) (o []byte, err error) {

View file

@ -2,18 +2,22 @@ package formatters
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"sync"
"codeberg.org/danjones000/my-log/config" "codeberg.org/danjones000/my-log/config"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
type formatMaker func(config *viper.Viper) (Formatter, error) var mut sync.RWMutex
var formatterMap = map[string]formatMaker{ type FormatInit func(config *viper.Viper) (Formatter, error)
"plain": newPlain,
"json": newJson, var formatterMap = map[string]FormatInit{
"zero": newNull, FormatPlain: newPlain,
FormatJSON: newJson,
FormatNull: newNull,
} }
func Preferred(ctx context.Context) (f Formatter, err error) { func Preferred(ctx context.Context) (f Formatter, err error) {
@ -25,6 +29,8 @@ func New(ctx context.Context, kind string) (f Formatter, err error) {
v, _ := config.RetrieveFromContext(ctx) v, _ := config.RetrieveFromContext(ctx)
formatterConf := v.Sub("formatters." + kind) formatterConf := v.Sub("formatters." + kind)
mut.RLock()
defer mut.RUnlock()
if maker, ok := formatterMap[kind]; ok { if maker, ok := formatterMap[kind]; ok {
return maker(formatterConf) return maker(formatterConf)
} }
@ -34,8 +40,24 @@ func New(ctx context.Context, kind string) (f Formatter, err error) {
func Kinds() []string { func Kinds() []string {
r := []string{} r := []string{}
mut.RLock()
defer mut.RUnlock()
for kind, _ := range formatterMap { for kind, _ := range formatterMap {
r = append(r, kind) r = append(r, kind)
} }
return r return r
} }
var ErrAlreadyAdded = errors.New("formatter already present")
func AddFormatter(key string, f FormatInit) error {
mut.Lock()
defer mut.Unlock()
if _, present := formatterMap[key]; present {
return fmt.Errorf("%w: %s", ErrAlreadyAdded, key)
}
formatterMap[key] = f
return nil
}

View file

@ -39,3 +39,17 @@ func TestPreferred(t *testing.T) {
be.Err(t, err, nil) be.Err(t, err, nil)
be.True(t, form != nil) be.True(t, form != nil)
} }
type dummyFormatter struct{ Null }
func (dummyFormatter) Name() string { return "dummy" }
func TestAddFormatter(t *testing.T) {
var df dummyFormatter
dummyInit := func(*viper.Viper) (Formatter, error) { return df, nil }
err := AddFormatter(df.Name(), dummyInit)
be.Err(t, err, nil)
err = AddFormatter(df.Name(), dummyInit)
be.Err(t, err, ErrAlreadyAdded)
}

View file

@ -5,28 +5,19 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
func newNull(ff *viper.Viper) (Formatter, error) { const FormatNull = "zero"
return &Null{}, nil
func newNull(*viper.Viper) (Formatter, error) {
return Null{}, nil
} }
type Null struct{} type Null struct{}
func (n *Null) Name() string { func (Null) Name() string {
return "zero" return FormatNull
} }
func (n *Null) Meta(m models.Meta) (o []byte, err error) { func (Null) Meta(m models.Meta) (o []byte, err error) { return }
return func (Null) Entry(e models.Entry) (o []byte, err error) { return }
} func (Null) Log(l models.Log) (o []byte, err error) { return }
func (Null) Logs(logs []models.Log) (out []byte, err error) { return }
func (n *Null) Entry(e models.Entry) (o []byte, err error) {
return
}
func (n *Null) Log(l models.Log) (o []byte, err error) {
return
}
func (n *Null) Logs(logs []models.Log) (out []byte, err error) {
return
}

View file

@ -8,16 +8,16 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
func newPlain(ff *viper.Viper) (Formatter, error) { const FormatPlain string = "plain"
func newPlain(*viper.Viper) (Formatter, error) {
return &PlainText{}, nil return &PlainText{}, nil
} }
type PlainText struct { type PlainText struct{}
// config might go here some day
}
func (pt *PlainText) Name() string { func (*PlainText) Name() string {
return "plain" return FormatPlain
} }
func (pt *PlainText) Logs(logs []models.Log) (out []byte, err error) { func (pt *PlainText) Logs(logs []models.Log) (out []byte, err error) {