mconf

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2022 License: MIT Imports: 14 Imported by: 1

README

mconf

dasel基础上封装的yaml、json、toml文档进行增删改查库。


安装

go get -u github.com/zhufuyi/pkg/mconf


使用示例

Documentation

Index

Constants

View Source
const (
	// YamlFormat yaml格式
	YamlFormat = "yaml"
	// JSONFormat json格式
	JSONFormat = "json"
	// TomlFormat toml格式
	TomlFormat = "toml"

	// NotFound 记录yaml未找到的key的统一值
	NotFound = "__not_found__"
)
View Source
const ColouriseFormatter = "terminal"

ColouriseFormatter is the formatter used when colourising output.

View Source
const ColouriseStyle = "solarized-dark256"

ColouriseStyle is the style used when colourising output.

Variables

View Source
var ErrPlainParserNotImplemented = fmt.Errorf("PlainParser.FromBytes not implemented")

ErrPlainParserNotImplemented is returned when you try to use the PlainParser.FromBytes func.

Functions

func Bytes2Int

func Bytes2Int(val []byte) int

Bytes2Int bytes转int

func Bytes2Map

func Bytes2Map(val []byte) map[string]string

Bytes2Map bytes转map

func Bytes2Slice

func Bytes2Slice(val []byte) []string

Bytes2Slice bytes转slice

func Bytes2SliceForArgs

func Bytes2SliceForArgs(val []byte) []string

Bytes2SliceForArgs bytes转slice

func Bytes2Str

func Bytes2Str(val []byte) string

Bytes2Str bytes转string

func Colourise

func Colourise(content string, lexer string) (*bytes.Buffer, error)

Colourise colourises the given string.

func ColouriseBuffer

func ColouriseBuffer(content *bytes.Buffer, lexer string) error

ColouriseBuffer colourises the given buffer in-place.

func Count

func Count(in []byte, selector string, inFormat string) (int, error)

Count 计算查询字段值数量,inFormat输入数据解析格式yaml、json 、toml,outFormat输出数据解析格式yaml、json 、toml

func Delete

func Delete(in []byte, selector string, inFormat string, outFormat string) ([]byte, error)

Delete 删除字段,参数inFormat输是入数据解析格式yaml、json 、toml,outFormat是输出数据解析格式yaml、json 、toml,输出格式可以和输入格式不一致

func DeleteJSON added in v1.3.1

func DeleteJSON(in []byte, selector string) ([]byte, error)

DeleteJSON 删除字段,针对json文档

func DeleteYaml

func DeleteYaml(in []byte, selector string) ([]byte, error)

DeleteYaml 删除字段,针对yaml文档

func Find

func Find(in []byte, selector string, inFormat string, outFormat string) ([]byte, error)

Find 查询字段值,select表示选择字段,inFormat输入数据解析格式yaml、json 、toml,outFormat输出数据解析格式yaml、json 、toml,输出格式可以和输入格式不一致

func FindJSON added in v1.3.1

func FindJSON(in []byte, selector string) ([]byte, error)

FindJSON 查询json文档字段值,select表示选择字段,输入输出都是json格式

func FindYaml

func FindYaml(in []byte, selector string) ([]byte, error)

FindYaml 查询yaml文档字段值,select表示选择字段,输入输出都是yaml格式

func Load

func Load(p ReadParser, reader io.Reader) (interface{}, error)

Load loads data from the given io.Reader.

func LoadFromFile

func LoadFromFile(filename string, p ReadParser) (interface{}, error)

LoadFromFile loads data from the given file.

func Put

func Put(in []byte, selector string, valueType string, value string, inFormat string, outFormat string) ([]byte, error)

Put 修改或增加一个字段值,inFormat输入数据解析格式yaml、json 、toml,outFormat输出数据解析格式yaml、json 、toml,输出格式可以和输入格式不一致

func PutDocument

func PutDocument(in []byte, selector string, docs string, inFormat string, outFormat string) ([]byte, error)

PutDocument 修改或增加多个字段值,参数docs是yaml或json文档,例如`["foo","bar"] 参数inFormat输是入数据解析格式yaml、json 、toml,outFormat是输出数据解析格式yaml、json 、toml,输出格式可以和输入格式不一致

func PutDocumentJSON added in v1.3.1

func PutDocumentJSON(in []byte, selector string, docs string) ([]byte, error)

PutDocumentJSON 修改或增加文档,针对json文档

func PutDocumentYaml

func PutDocumentYaml(in []byte, selector string, docs string) ([]byte, error)

PutDocumentYaml 修改或增加文档,针对yaml文档

func PutJSON added in v1.3.1

func PutJSON(in []byte, selector string, valueType string, value string) ([]byte, error)

PutJSON 修改或增加字段,针对json文档

func PutObject

func PutObject(in []byte, selector string, valueTypes []string, kvs []string, inFormat string, outFormat string) ([]byte, error)

PutObject 修改或增加多个字段值,参数valueTypes是值的类型,kvs是值类型对应kv键值对,例如valueTypes=["int","string"],values=["a=1","b=hello"] 参数inFormat输是入数据解析格式yaml、json 、toml,outFormat是输出数据解析格式yaml、json 、toml,输出格式可以和输入格式不一致

func PutObjectJSON added in v1.3.1

func PutObjectJSON(in []byte, selector string, valueTypes []string, kvs []string) ([]byte, error)

PutObjectJSON 修改或增加多个字段值,针对json文档

func PutObjectYaml

func PutObjectYaml(in []byte, selector string, valueTypes []string, kvs []string) ([]byte, error)

PutObjectYaml 修改或增加多个字段值,针对yaml文档

func PutYaml

func PutYaml(in []byte, selector string, valueType string, value string) ([]byte, error)

PutYaml 修改或增加字段,针对yaml文档

func ResourcesMap2Str

func ResourcesMap2Str(m map[string]string) string

ResourcesMap2Str 资源字典转字符串

func Write

func Write(p WriteParser, value interface{}, originalValue interface{}, writer io.Writer, options ...ReadWriteOption) error

Write writes the value to the given io.Writer.

Types

type BasicMultiDocument

type BasicMultiDocument struct {
	Values []interface{}
	// contains filtered or unexported fields
}

BasicMultiDocument represents a multi-document file.

func (*BasicMultiDocument) Documents

func (d *BasicMultiDocument) Documents() []interface{}

Documents returns the documents that should be written to output.

func (BasicMultiDocument) OriginalRequired

func (d BasicMultiDocument) OriginalRequired() bool

OriginalRequired tells dasel if the parser requires the original value when converting to bytes.

func (*BasicMultiDocument) RealValue

func (d *BasicMultiDocument) RealValue() interface{}

RealValue returns the real value that dasel should use when processing data.

type BasicSingleDocument

type BasicSingleDocument struct {
	Value interface{}
	// contains filtered or unexported fields
}

BasicSingleDocument represents a single document file.

func (*BasicSingleDocument) Document

func (d *BasicSingleDocument) Document() interface{}

Document returns the document that should be written to output.

func (BasicSingleDocument) OriginalRequired

func (d BasicSingleDocument) OriginalRequired() bool

OriginalRequired tells dasel if the parser requires the original value when converting to bytes.

func (*BasicSingleDocument) RealValue

func (d *BasicSingleDocument) RealValue() interface{}

RealValue returns the real value that dasel should use when processing data.

type JSONParser

type JSONParser struct {
}

JSONParser is a Parser implementation to handle yaml files.

func (*JSONParser) FromBytes

func (p *JSONParser) FromBytes(byteData []byte) (interface{}, error)

FromBytes returns some data that is represented by the given bytes.

func (*JSONParser) ToBytes

func (p *JSONParser) ToBytes(value interface{}, options ...ReadWriteOption) ([]byte, error)

ToBytes returns a slice of bytes that represents the given value.

type MultiDocument

type MultiDocument interface {
	Documents() []interface{}
}

MultiDocument is a parser result that contains multiple documents.

type OptionKey

type OptionKey string

OptionKey is a defined type for keys within a ReadWriteOption.

const (
	// OptionIndent is the key used with IndentOption.
	OptionIndent OptionKey = "indent"
	// OptionPrettyPrint is the key used with PrettyPrintOption.
	OptionPrettyPrint OptionKey = "prettyPrint"
	// OptionColourise is the key used with ColouriseOption.
	OptionColourise OptionKey = "colourise"
	// OptionEscapeHTML is the key used with EscapeHTMLOption.
	OptionEscapeHTML OptionKey = "escapeHtml"
)

type OriginalRequired

type OriginalRequired interface {
	// OriginalRequired tells dasel if the parser requires the original value when converting to bytes.
	OriginalRequired() bool
}

OriginalRequired can be used in conjunction with RealValue to allow parsers to be more intelligent with the data they read/write.

type Parser

type Parser interface {
	ReadParser
	WriteParser
}

Parser can be used to load and save files from/to disk.

type PlainParser

type PlainParser struct {
}

PlainParser is a Parser implementation to handle yaml files.

func (*PlainParser) FromBytes

func (p *PlainParser) FromBytes(byteData []byte) (interface{}, error)

FromBytes returns some data that is represented by the given bytes.

func (*PlainParser) ToBytes

func (p *PlainParser) ToBytes(value interface{}, options ...ReadWriteOption) ([]byte, error)

ToBytes returns a slice of bytes that represents the given value.

type PutRecord

type PutRecord struct {
	Key    string `json:"key"`
	OldVal string `json:"oldVal"` // 如果存在则替换,如果查找的key不存在,则补充统一值__not_found__,表示是新添加
	NewVal string `json:"newVal"`
}

PutRecord 已成功替换的参数记录

type ReadParser

type ReadParser interface {
	// FromBytes returns some data that is represented by the given bytes.
	FromBytes(byteData []byte) (interface{}, error)
}

ReadParser can be used to convert bytes to data.

func NewReadParserFromFilename

func NewReadParserFromFilename(filename string) (ReadParser, error)

NewReadParserFromFilename returns a ReadParser from the given filename.

func NewReadParserFromString

func NewReadParserFromString(parser string) (ReadParser, error)

NewReadParserFromString returns a ReadParser from the given parser name.

type ReadWriteOption

type ReadWriteOption struct {
	Key   OptionKey
	Value interface{}
}

ReadWriteOption is an option to be used when writing.

func ColouriseOption

func ColouriseOption(enabled bool) ReadWriteOption

ColouriseOption returns an option that enables or disables colourised output.

func EscapeHTMLOption

func EscapeHTMLOption(enabled bool) ReadWriteOption

EscapeHTMLOption returns an option that enables or disables HTML escaping.

func IndentOption

func IndentOption(indent string) ReadWriteOption

IndentOption returns a write option that sets the given indent.

func PrettyPrintOption

func PrettyPrintOption(enabled bool) ReadWriteOption

PrettyPrintOption returns an option that enables or disables pretty printing.

type RealValue

type RealValue interface {
	// RealValue returns the real value that dasel should use when processing data.
	RealValue() interface{}
}

RealValue can be used in conjunction with OriginalRequired to allow parsers to be more intelligent with the data they read/write.

type SingleDocument

type SingleDocument interface {
	Document() interface{}
}

SingleDocument is a parser result that contains a single document.

type TOMLParser

type TOMLParser struct {
}

TOMLParser is a Parser implementation to handle toml files.

func (*TOMLParser) FromBytes

func (p *TOMLParser) FromBytes(byteData []byte) (interface{}, error)

FromBytes returns some data that is represented by the given bytes.

func (*TOMLParser) ToBytes

func (p *TOMLParser) ToBytes(value interface{}, options ...ReadWriteOption) ([]byte, error)

ToBytes returns a slice of bytes that represents the given value.

type UnknownParserErr

type UnknownParserErr struct {
	Parser string
}

UnknownParserErr is returned when an invalid parser name is given.

func (UnknownParserErr) Error

func (e UnknownParserErr) Error() string

Error returns the error message.

type WriteParser

type WriteParser interface {
	// ToBytes returns a slice of bytes that represents the given value.
	ToBytes(value interface{}, options ...ReadWriteOption) ([]byte, error)
}

WriteParser can be used to convert data to bytes.

func NewWriteParserFromFilename

func NewWriteParserFromFilename(filename string) (WriteParser, error)

NewWriteParserFromFilename returns a WriteParser from the given filename.

func NewWriteParserFromString

func NewWriteParserFromString(parser string) (WriteParser, error)

NewWriteParserFromString returns a WriteParser from the given parser name.

type YAMLParser

type YAMLParser struct {
}

YAMLParser is a Parser implementation to handle yaml files.

func (*YAMLParser) FromBytes

func (p *YAMLParser) FromBytes(byteData []byte) (interface{}, error)

FromBytes returns some data that is represented by the given bytes.

func (*YAMLParser) ToBytes

func (p *YAMLParser) ToBytes(value interface{}, options ...ReadWriteOption) ([]byte, error)

ToBytes returns a slice of bytes that represents the given value.

Jump to

Keyboard shortcuts

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