configparser

package module
v0.0.0-...-d588d89 Latest Latest
Warning

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

Go to latest
Published: May 2, 2023 License: MIT Imports: 9 Imported by: 0

README

ConfigParser

This project was created for filling a gap I found on the existing Golang ecosystem: There was no simple library that did config parsing, validation of required fields and allowed for default values at the same time.

The project is meant to be simple and currently supports only yaml config files.

There are 3 convenience functions that internally do the same thing:

  • ParseYaml
  • ParseYamlFile
  • ParseYamlReader

and for each there is a version that panics instead of returning an error:

  • MustParseYaml
  • MustParseYamlFile
  • MustParseYamlReader

Usage Example

package main

import (
	"fmt"

	"github.com/blackpointcyber/configparser"
)

func main() {
	var config struct {
		SecretKey int    `yaml:"secretKey" validate:"required"`
		BaseURL   string `yaml:"baseUrl" default:"https://example.com"`

		Address struct {
			Street  string `yaml:"street" default:"defaultStreet"`
			City    string `yaml:"city"`
			Country string `yaml:"country"`
		} `yaml:"address"`
	}

	configparser.MustParseYAMLFile("./examples/simple_usage/config.yaml", &config)

	fmt.Println(config)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustParseYAML

func MustParseYAML(file []byte, targetStruct any)

func MustParseYAMLFile

func MustParseYAMLFile(filepath string, targetStruct any)

func MustParseYAMLFromReader

func MustParseYAMLFromReader(file io.Reader, targetStruct any)

func ParseYAML

func ParseYAML(file []byte, targetStruct any) error

func ParseYAMLFile

func ParseYAMLFile(path string, targetStruct any) error

func ParseYAMLFromReader

func ParseYAMLFromReader(file io.Reader, targetStruct any) error

Types

This section is empty.

Directories

Path Synopsis
examples
internal

Jump to

Keyboard shortcuts

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