gophig

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2024 License: Unlicense Imports: 9 Imported by: 1

README

Getting Started

Gophig may be imported using go get:

go get git.restartfu.com/restart/gophig.git

Usage

You may create a new *Gophig:

type Foo struct{
foo string `toml:"foo"`
bar string `toml:"bar"`
}

g := gophig.NewGophig[Foo]("./config.toml", gophig.TOMLMarshaler, os.ModePerm)

Then you may use the method WriteConf(v any):

myFooStruct := Foo{foo: "foo", bar: "bar"}

if err := g.SaveConf(myFooStruct);err != nil{
   log.Fatalln(err)
}

// Output file content:
// ./config.toml
/* 
   foo = "foo"
   bar = "bar"
*/

Or the method ReadConf[T any]() T:

// If we assume that the output file content is the same as the example up there:
myFooStruct, err := g.LoadConf(&myFooStruct)
if err != nil {
log.Fatalln(err)
}

log.Println(foo)

// Output:
/*
   {foo: "foo", bar: "bar"}
*/

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsUnsupportedExtensionErr

func IsUnsupportedExtensionErr(err error) bool

IsUnsupportedExtensionErr returns true if the error is an UnsupportedExtensionError.

func LoadConf

func LoadConf[T any](path string, marshaler Marshaler) (T, error)

func LoadConfContext

func LoadConfContext[T any](ctx context.Context) (T, error)

LoadConfContext loads the configuration file into the given type.

func SaveConf

func SaveConf[T any](path string, marshaler Marshaler, v T) (T, error)

func SaveConfContext

func SaveConfContext(ctx context.Context) error

SaveConfContext saves the given type to the configuration file.

Types

type Gophig

type Gophig[T any] struct {
	// contains filtered or unexported fields
}

Gophig is a struct that contains the name, extension, and permission of a configuration file.

func NewGophig

func NewGophig[T any](name string, marshaler Marshaler, perm fs.FileMode) *Gophig[T]

NewGophig returns a new Gophig struct.

func (*Gophig[T]) LoadConf

func (g *Gophig[T]) LoadConf() (T, error)

LoadConf loads the configuration file into the given interface.

func (*Gophig[T]) SaveConf

func (g *Gophig[T]) SaveConf(v T) error

SaveConf saves the given interface to the configuration file.

type JSONMarshaler

type JSONMarshaler struct {
	Indent bool
}

JSONMarshaler is a Marshaler that uses the goccy/go-json package.

func (JSONMarshaler) Marshal

func (m JSONMarshaler) Marshal(v interface{}) ([]byte, error)

Marshal ...

func (JSONMarshaler) Unmarshal

func (JSONMarshaler) Unmarshal(data []byte, v interface{}) error

Unmarshal ...

type Marshaler

type Marshaler interface {
	Marshal(v interface{}) ([]byte, error)
	Unmarshal(data []byte, v interface{}) error
}

Marshaler is an interface that can marshal and unmarshal data.

func MarshalerFromExtension

func MarshalerFromExtension(ext string) (Marshaler, error)

MarshalerFromExtension is a Marshaler that uses a file extension to determine which Marshaler to use.

type RawContext

type RawContext struct {
	context.Context
	// contains filtered or unexported fields
}

func (*RawContext) Value

func (c *RawContext) Value(key any) any

type TOMLMarshaler

type TOMLMarshaler struct{}

TOMLMarshaler is a Marshaler that uses the pelletier/go-toml package.

func (TOMLMarshaler) Marshal

func (TOMLMarshaler) Marshal(v interface{}) ([]byte, error)

Marshal ...

func (TOMLMarshaler) Unmarshal

func (TOMLMarshaler) Unmarshal(data []byte, v interface{}) error

Unmarshal ...

type UnsupportedExtensionError

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

UnsupportedExtensionError is an error that is returned when a file extension is not supported.

func (UnsupportedExtensionError) Error

Error ...

type YAMLMarshaler

type YAMLMarshaler struct{}

YAMLMarshaler is a Marshaler that uses the go-yaml/yaml package.

func (YAMLMarshaler) Marshal

func (YAMLMarshaler) Marshal(v interface{}) ([]byte, error)

Marshal ...

func (YAMLMarshaler) Unmarshal

func (YAMLMarshaler) Unmarshal(data []byte, v interface{}) error

Unmarshal ...

Jump to

Keyboard shortcuts

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