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"
)
const FormatJSON string = "json"
func newJson(ff *viper.Viper) (Formatter, error) {
js := new(Json)
err := ff.Unmarshal(js)
@ -24,7 +26,7 @@ type Json struct {
}
func (js *Json) Name() string {
return "json"
return FormatJSON
}
func (js *Json) marshal(v any) (o []byte, err error) {

View file

@ -2,18 +2,22 @@ package formatters
import (
"context"
"errors"
"fmt"
"sync"
"codeberg.org/danjones000/my-log/config"
"github.com/spf13/viper"
)
type formatMaker func(config *viper.Viper) (Formatter, error)
var mut sync.RWMutex
var formatterMap = map[string]formatMaker{
"plain": newPlain,
"json": newJson,
"zero": newNull,
type FormatInit func(config *viper.Viper) (Formatter, error)
var formatterMap = map[string]FormatInit{
FormatPlain: newPlain,
FormatJSON: newJson,
FormatNull: newNull,
}
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)
formatterConf := v.Sub("formatters." + kind)
mut.RLock()
defer mut.RUnlock()
if maker, ok := formatterMap[kind]; ok {
return maker(formatterConf)
}
@ -34,8 +40,24 @@ func New(ctx context.Context, kind string) (f Formatter, err error) {
func Kinds() []string {
r := []string{}
mut.RLock()
defer mut.RUnlock()
for kind, _ := range formatterMap {
r = append(r, kind)
}
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.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"
)
func newNull(ff *viper.Viper) (Formatter, error) {
return &Null{}, nil
const FormatNull = "zero"
func newNull(*viper.Viper) (Formatter, error) {
return Null{}, nil
}
type Null struct{}
func (n *Null) Name() string {
return "zero"
func (Null) Name() string {
return FormatNull
}
func (n *Null) Meta(m models.Meta) (o []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
}
func (Null) Meta(m models.Meta) (o []byte, err error) { 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 }

View file

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