formatters

package
v0.0.0-...-c864ae1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2023 License: BSD-2-Clause, BSD-3-Clause, MIT Imports: 2 Imported by: 0

Documentation

Overview

Package formatters defines observer formatters that take Attributes and convert them into observer-ready strings.

Index

Examples

Constants

View Source
const MessageErrorLabel = "Error"
View Source
const MessageNameLabel = "Message"

The default label for a Attributes entry that is used for storing free-form messages.

View Source
const MessageWarnLabel = "Warn"

Variables

This section is empty.

Functions

This section is empty.

Types

type JsonFormatter

type JsonFormatter struct{}

JsonFormatter formats a Attributes array into an equivalent JSON encoding.

func (*JsonFormatter) Format

func (formatter *JsonFormatter) Format(attributes attributes.Attributes) string
Example
package main

import (
	"fmt"

	"github.com/LindsayBradford/crem/pkg/attributes"
)

type jsonStringer struct{}

func (s *jsonStringer) String() string {
	return "stringerValue"
}

func main() {
	exampleAttributes := attributes.Attributes{
		{Name: "One", Value: "valueOne"},
		{Name: "Two", Value: 42},
		{Name: "Three", Value: uint64(0)},
		{Name: "Four", Value: 42.42},
		{Name: "Five", Value: new(jsonStringer)},
		{Name: "Six", Value: true},
		{Name: "Seven", Value: 7777.777777},
	}

	exampleFormatter := new(JsonFormatter)

	exampleJson := exampleFormatter.Format(exampleAttributes)
	fmt.Print(exampleJson)

}
Output:

{"One": "valueOne", "Two": 42, "Three": 0, "Four": 42.42, "Five": "stringerValue", "Six": true, "Seven": 7,777.777777}

type NameValuePairFormatter

type NameValuePairFormatter struct{}

NameValuePairFormatter formats a Attributes array into a string of comma-separated name-value pairs.

func (*NameValuePairFormatter) Format

func (formatter *NameValuePairFormatter) Format(attributes attributes.Attributes) string
Example
// Copyright (c) 2019 Australian Rivers Institute.

package main

import (
	"fmt"

	"github.com/LindsayBradford/crem/pkg/attributes"
)

type nvpStringer struct{}

func (s *nvpStringer) String() string {
	return "stringerValue"
}

func main() {
	exampleAttributes := attributes.Attributes{
		{Name: "One", Value: "valueOne"},
		{Name: "Two", Value: 42},
		{Name: "Three", Value: uint64(0)},
		{Name: "Four", Value: 42.42},
		{Name: "Five", Value: new(nvpStringer)},
		{Name: "Six", Value: true},
	}

	exampleFormatter := new(NameValuePairFormatter)

	exampleNvp := exampleFormatter.Format(exampleAttributes)
	fmt.Print(exampleNvp)

}
Output:

One="valueOne", Two=42, Three=0, Four=42.42, Five="stringerValue", Six=true

type NullFormatter

type NullFormatter struct{}

NullFormatter implements a 'null object' placeholder formatter that is supplied by default if one is not specified. It returns a static message as per nullFormatMessage as a reminder that a proper formatter must be supplied for the observer to do anything meaningful.

func (*NullFormatter) Format

func (nf *NullFormatter) Format(attributes attributes.Attributes) string
Example
exampleAttributes := attributes.Attributes{
	{Name: "NoMatter", Value: "Ignored anyway"},
}

exampleFormatter := new(NullFormatter)

exampleJson := exampleFormatter.Format(exampleAttributes)
fmt.Print(exampleJson)
Output:

No formatter specified. Using the NullFormatter.

type RawMessageFormatter

type RawMessageFormatter struct{}

RawMessageFormatter implements the Formatter interface by ignoring all logAttributes attributes supplied except the 'Message, 'Error' and 'Warn' attribute, returning a "formatted" string exactly as per the attribute's value.

func (*RawMessageFormatter) Format

func (formatter *RawMessageFormatter) Format(attributes attributes.Attributes) string
Example
expectedMessage := "here is an expected message"
exampleAttributes := attributes.Attributes{
	{Name: "Message", Value: expectedMessage},
	{Name: "NotAMessage", Value: "who cares?"},
}

exampleFormatter := new(RawMessageFormatter)

exampleMsg := exampleFormatter.Format(exampleAttributes)
fmt.Print(exampleMsg)
Output:

here is an expected message

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL