jconfig

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2020 License: GPL-3.0 Imports: 7 Imported by: 5

README

jconfig

This is a Go package to parse a configuration file using JSON.

Build Status GoDoc

Installation

go get github.com/choueric/jconfig

import (
		"github.com/choueric/jconfig"
	   )

Usage


package main

import (
	"fmt"
	"github.com/choueric/jconfig"
)

const DefContent = `{
	"server": "127.0.0.1:8088"
}
`

type Config struct {
	Server string `json:"server"`
}

func getConfig() *Config {
	// NOTE: Config{} is the your own type to store configurations.
	jc := jconfig.New("config.json", Config{}) 

	if _, err := jc.Load(DefContent); err != nil {
		fmt.Println("load config error:", err)
		return nil
	}

	return jc.Data().(*Config) // convert to your own type and return.
}

Refer to jconfig_test.go for more details of how to use it.

Another usage is config.go of kbdashboard

Documentation

Overview

Simple Example

package main

import (
	"fmt"
	"github.com/choueric/jconfig"
)

const DefContent = `{
	"server": "127.0.0.1:8088"
}
`

type Config struct {
	Server string `json:"server"`
}

func getConfig() *Config {
	// NOTE: Config{} is the your own type to store configurations.
	jc := jconfig.New("config.json", Config{})

	if _, err := jc.Load(DefContent); err != nil {
		fmt.Println("load config error:", err)
		return nil
	}

	return jc.Data().(*Config) // convert to your own type and return.
}

Package jconfig provides struct JConfig to handle with configurations in JSON format.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JConfig

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

JConfig structure is an entity representing configurations.

func New

func New(filepath string, i interface{}) *JConfig

New returns a pointer of JConfig that contains information of configuration file path and variable i of user-defined configuration type.

func (*JConfig) Data

func (c *JConfig) Data() interface{}

Data retruns the user-defined configuration data.

func (*JConfig) Dir

func (c *JConfig) Dir() string

Dir returns the path of directory containing configuration file.

func (*JConfig) FilePath

func (c *JConfig) FilePath() string

FilePath returns the full path of configuration file.

func (*JConfig) Filename

func (c *JConfig) Filename() string

Filename returns just the file name of configuration file.

func (*JConfig) Load

func (c *JConfig) Load(defContent string) (interface{}, error)

Load loads and parses the configuration file, then allocats a user-defined configuration variable which is stored in JConfig and is returned. If directory or file does not exist, Load will create with defContent.

func (*JConfig) Save

func (c *JConfig) Save() error

Save writes the user-defined configruations into JSON file.

Jump to

Keyboard shortcuts

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