config

package
v0.0.0-...-2834284 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2024 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Package config 类ini的配置文件库,支持注释信息

Index

Constants

This section is empty.

Variables

View Source
var EmptyValue = &Value{t: tstr}

EmptyValue an empty value

Functions

This section is empty.

Types

type File

type File struct {
	// contains filtered or unexported fields
}

File 配置文件

func NewConfig

func NewConfig(filepath string) *File

NewConfig 创建一个key:value格式的配置文件

依据文件的扩展名,支持yaml和json格式的文件

func (*File) Clean

func (f *File) Clean()

Clean 清空配置项

func (*File) DelItem

func (f *File) DelItem(key string)

DelItem 删除配置项

func (*File) ForEach

func (f *File) ForEach(do func(key string, value *Value) bool)

ForEach 遍历所有值

func (*File) FromFile

func (f *File) FromFile(configfile string) error

FromFile 从文件载入配置

func (*File) GetAll

func (f *File) GetAll() string

GetAll 返回所有配置项

func (*File) GetDefault

func (f *File) GetDefault(item *Item) *Value

GetDefault 读取一个配置,若不存在,则添加这个配置

func (*File) GetItem

func (f *File) GetItem(key string) *Value

GetItem 获取一个配置值

func (*File) Has

func (f *File) Has(key string) bool

Has 判断key是否存在

func (*File) Keys

func (f *File) Keys() []string

Keys 获取所有Key

func (*File) Len

func (f *File) Len() int

Len 获取配置数量

func (*File) Print

func (f *File) Print() string

Print 返回所有配置项

func (*File) PrintJSON

func (f *File) PrintJSON() string

func (*File) PutItem

func (f *File) PutItem(item *Item)

PutItem 添加配置项

func (*File) Save

func (f *File) Save() error

Save 将配置写入文件,依据文件扩展名判断写入格式

func (*File) SaveTo

func (f *File) SaveTo(filename string) error

SaveTo 将配置写入指定文件,依据文件扩展名判断写入格式

func (*File) ToFile

func (f *File) ToFile() error

ToFile 将配置写入文件,依据文件扩展名判断写入格式

func (*File) ToJSON

func (f *File) ToJSON() error

ToJSON 保存为json格式文件

func (*File) ToYAML

func (f *File) ToYAML() error

ToYAML 保存为yaml格式文件

type FormatType

type FormatType byte
const (
	KeyValue FormatType = iota
	JSON
	YAML
)

type Formatted

type Formatted[ITEM any] struct {
	// contains filtered or unexported fields
}

Formatted yaml/json 格式化配置文件

func NewFormatFile

func NewFormatFile[ITEM any](configfile string, ft FormatType) *Formatted[ITEM]

NewFormatFile 创建一个新的自定义结构的yaml/json配置文件

func (*Formatted[ITEM]) Clone

func (f *Formatted[ITEM]) Clone() map[string]*ITEM

Clone 获取所有配置

func (*Formatted[ITEM]) DelItem

func (f *Formatted[ITEM]) DelItem(key string)

DelItem 删除一个配置项

func (*Formatted[ITEM]) ForEach

func (f *Formatted[ITEM]) ForEach(do func(key string, value *ITEM) bool)

ForEach 遍历所有值

func (*Formatted[ITEM]) FromFile

func (f *Formatted[ITEM]) FromFile(configfile string) error

FromFile 从文件读取配置

func (*Formatted[ITEM]) GetItem

func (f *Formatted[ITEM]) GetItem(key string) (*ITEM, bool)

GetItem 读取一个配置项

func (*Formatted[ITEM]) Has

func (f *Formatted[ITEM]) Has(key string) bool

Has 判断key是否存在

func (*Formatted[ITEM]) Len

func (f *Formatted[ITEM]) Len() int

Len 获取配置数量

func (*Formatted[ITEM]) Print

func (f *Formatted[ITEM]) Print() string

Print 返回所有配置项

func (*Formatted[ITEM]) PrintFormat

func (f *Formatted[ITEM]) PrintFormat(fmt FormatType) string

PrintFormat 返回所有配置项

func (*Formatted[ITEM]) PutItem

func (f *Formatted[ITEM]) PutItem(key string, value *ITEM)

PutItem 添加一个配置项

func (*Formatted[ITEM]) ToFile

func (f *Formatted[ITEM]) ToFile() error

ToFile 写入文件

type Item

type Item struct {
	Value   *Value `json:"value" yaml:"value"`
	Comment string `json:"comment" yaml:"comment"`
	Key     string `json:"-" yaml:"-"`
}

Item 配置内容,包含注释,key,value,是否加密value

func (*Item) String

func (i *Item) String() string

String 把配置项格式化成字符串

type PwdString

type PwdString string

PwdString 序列化反序列化时可自动加密解密字符串,用于敏感字段

func (*PwdString) MarshalJSON

func (p *PwdString) MarshalJSON() ([]byte, error)

func (PwdString) MarshalYAML

func (p PwdString) MarshalYAML() (interface{}, error)

func (*PwdString) UnmarshalJSON

func (p *PwdString) UnmarshalJSON(data []byte) error

func (*PwdString) UnmarshalYAML

func (p *PwdString) UnmarshalYAML(unmarshal func(interface{}) error) error

type VString

type VString string

VString value string, can parse to bool int64 float64

func (VString) Bytes

func (rs VString) Bytes() []byte

Bytes reutrn []byte

func (VString) String

func (rs VString) String() string

String reutrn string

func (VString) TryBool

func (rs VString) TryBool() bool

TryBool reutrn bool

func (VString) TryDecode

func (rs VString) TryDecode() string

TryDecode try decode the value, if failed, return the origin

func (VString) TryFloat32

func (rs VString) TryFloat32() float32

TryFloat32 reutrn float32

func (VString) TryFloat64

func (rs VString) TryFloat64() float64

TryFloat64 reutrn float64

func (VString) TryInt

func (rs VString) TryInt() int

TryInt reutrn int

func (VString) TryInt32

func (rs VString) TryInt32() int32

TryInt32 reutrn int32

func (VString) TryInt64

func (rs VString) TryInt64() int64

TryInt64 reutrn int64

func (VString) TryTimestamp

func (rs VString) TryTimestamp(f string) int64

TryTimestamp try turn time string to timestamp

f: datetime format string,default is 2006-01-02 15:04:05, use timezone +8.0

func (VString) TryUint64

func (rs VString) TryUint64() uint64

TryUint64 reutrn uint64

type Value

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

Value config value

func NewBoolValue

func NewBoolValue(n bool) *Value

NewBoolValue return a value

func NewCodeValue

func NewCodeValue(s string) *Value

NewCodeValue return a value after code the data

func NewFloat64Value

func NewFloat64Value(n float64) *Value

NewFloat64Value return a value

func NewInt64Value

func NewInt64Value(n int64) *Value

NewInt64Value return a value

func NewUint64Value

func NewUint64Value(n uint64) *Value

NewUint64Value return a value

func NewValue

func NewValue(s string) *Value

NewValue return a value

func (*Value) Bytes

func (v *Value) Bytes() []byte

Bytes reutrn []byte

func (*Value) MarshalJSON

func (v *Value) MarshalJSON() ([]byte, error)

func (*Value) MarshalYAML

func (v *Value) MarshalYAML() (any, error)

func (*Value) String

func (v *Value) String() string

String reutrn string

func (*Value) TryBool

func (v *Value) TryBool() bool

TryBool reutrn bool

func (*Value) TryDecode

func (v *Value) TryDecode() string

func (*Value) TryFloat32

func (v *Value) TryFloat32(dec ...int) float32

TryFloat32 reutrn float32 dec: 设置小数位数,仅第一个值有效

func (*Value) TryFloat64

func (v *Value) TryFloat64(dec ...int) float64

TryFloat64 reutrn float64 dec: 设置小数位数,仅第一个值有效

func (*Value) TryInt

func (v *Value) TryInt() int

TryInt reutrn int

func (*Value) TryInt32

func (v *Value) TryInt32() int32

TryInt32 reutrn int32

func (*Value) TryInt64

func (v *Value) TryInt64() int64

TryInt64 reutrn int64

func (*Value) TryTimestamp

func (v *Value) TryTimestamp(f string) int64

func (*Value) TryUint64

func (v *Value) TryUint64() uint64

TryUint64 reutrn uint64

func (*Value) UnmarshalJSON

func (v *Value) UnmarshalJSON(data []byte) error

func (*Value) UnmarshalYAML

func (v *Value) UnmarshalYAML(unmarshal func(interface{}) error) error

Jump to

Keyboard shortcuts

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