You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.0 KiB
Go
57 lines
1.0 KiB
Go
package ndef
|
|
|
|
import (
|
|
"bytes"
|
|
nio "git.noahlan.cn/noahlan/ntool/nstd/io"
|
|
"io"
|
|
)
|
|
|
|
// DataFormatter interface
|
|
type DataFormatter interface {
|
|
Format() string
|
|
FormatTo(w io.Writer)
|
|
}
|
|
|
|
// BaseFormatter struct
|
|
type BaseFormatter struct {
|
|
ow nio.ByteStringWriter
|
|
// Out formatted to the writer
|
|
Out io.Writer
|
|
// Src data(array, map, struct) for format
|
|
Src any
|
|
// MaxDepth limit depth for array, map data TODO
|
|
MaxDepth int
|
|
// Prefix string for each element
|
|
Prefix string
|
|
// Indent string for format each element
|
|
Indent string
|
|
// ClosePrefix string for last "]", "}"
|
|
ClosePrefix string
|
|
}
|
|
|
|
// Reset after format
|
|
func (f *BaseFormatter) Reset() {
|
|
f.Out = nil
|
|
f.Src = nil
|
|
}
|
|
|
|
// SetOutput writer
|
|
func (f *BaseFormatter) SetOutput(out io.Writer) {
|
|
f.Out = out
|
|
}
|
|
|
|
// BsWriter build and get
|
|
func (f *BaseFormatter) BsWriter() nio.ByteStringWriter {
|
|
if f.ow == nil {
|
|
if f.Out == nil {
|
|
f.ow = new(bytes.Buffer)
|
|
} else if ow, ok := f.Out.(nio.ByteStringWriter); ok {
|
|
f.ow = ow
|
|
} else {
|
|
f.ow = nio.NewWriteWrapper(f.Out)
|
|
}
|
|
}
|
|
|
|
return f.ow
|
|
}
|