config

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2024 License: MIT Imports: 4 Imported by: 9

README

config Package

Overview

The config package provides a set of functions for working with JSON configuration files. It offers methods to retrieve different types of data (string, int, float, bool, etc.) from a JSON key. This is useful for applications that need to read and manage configurations stored in JSON format.

Installation

To install the config package, use the following command:

go get github.com/jgolang/config

Usage

Here’s a basic example of how to use the config package:

package main

import (
    "fmt"
    "log"
    "github.com/jgolang/config"
)

func main() {
    err := config.LoadConfigFile("config.json")
    if err != nil {
        log.Fatalf("Failed to open config file: %v", err)
    }

    appName := config.GetString("app.name")
    port := config.GetInt("server.port")
    debugMode := config.GetBool("debug")

    fmt.Printf("App Name: %s\n", appName)
    fmt.Printf("Port: %d\n", port)
    fmt.Printf("Debug Mode: %t\n", debugMode)
}

Functions

The config package provides several functions to retrieve data from a JSON configuration file.

GetString
func GetString(key string) string

Gets a string JSON node from a JSON key.

  • key: The JSON key (e.g., foo.bar).
  • Returns: The string value associated with the JSON key.
GetInt64
func GetInt64(key string) int64

Gets an integer 64 JSON node from a JSON key.

  • key: The JSON key (e.g., foo.bar).
  • Returns: The int64 value associated with the JSON key.
GetInt
func GetInt(key string) int

Gets an integer JSON node from a JSON key.

  • key: The JSON key (e.g., foo.bar).
  • Returns: The int value associated with the JSON key.
GetFloat64
func GetFloat64(key string) float64

Gets a float 64 JSON node from a JSON key.

  • key: The JSON key (e.g., foo.bar).
  • Returns: The float64 value associated with the JSON key.
GetBool
func GetBool(key string) bool

Gets a boolean JSON node from a JSON key.

  • key: The JSON key (e.g., foo.bar).
  • Returns: The boolean value associated with the JSON key.
Get
func Get(key string) interface{}

Gets a JSON node from a JSON key.

  • key: The JSON key (e.g., foo.bar).
  • Returns: The value associated with the JSON key as an interface{}.
GetArrayMaps
func GetArrayMaps(key string) []map[string]interface{}

Gets an array JSON node from a JSON key. Returns a JSON array as a map.

  • key: The JSON key (e.g., foo.bar).
  • Returns: The JSON array as a slice of maps.
LoadConfigFile
func LoadConfigFile(name string) error

Opens a file that contains a JSON object.

  • name: The name of the JSON file.
  • Returns: An error if the file cannot be opened.

Example

Here’s an example configuration file (config.json):

{
    "app": {
        "name": "MyApp"
    },
    "server": {
        "port": 8080
    },
    "debug": true
}

Using the configuration file:

package main

import (
    "fmt"
    "log"
    "github.com/jgolang/config"
)

func main() {
    err := config.LoadConfigFile("config.json")
    if err != nil {
        log.Fatalf("Failed to open config file: %v", err)
    }

    appName := config.GetString("app.name")
    port := config.GetInt("server.port")
    debugMode := config.GetBool("debug")

    fmt.Printf("App Name: %s\n", appName)
    fmt.Printf("Port: %d\n", port)
    fmt.Printf("Debug Mode: %t\n", debugMode)
}

This example demonstrates how to open a configuration file and retrieve different types of data from it using the config package.

Contributing

If you have suggestions for how We could be improved, or want to report a bug, open an issue! We'd love all and any contributions.

For more, check out the Contributing Guide.

License

This project is licensed under the MIT License.

Support

If you find this repository helpful and would like to support its development, consider making a donation. Your contributions will help ensure the continued improvement and maintenance of this repository.

Thank you for your support!

ko-fi

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FileName string = "config.json"

FileName file name that contain a json object. By default filename: "config.json"

View Source
var FilePath string = "./"

FilePath file config path. By default file config path: "./"

Functions

func Get

func Get(key string) interface{}

Get gets a json node from a json key. JSON key example: 'foo.bar'

func GetArrayMaps

func GetArrayMaps(key string) []map[string]interface{}

GetArrayMaps gets a array json node from a json key. Returns a json array as map. JSON key example: 'foo.bar'

func GetBool

func GetBool(key string) bool

GetBool gets a bool json node from a json key. JSON key example: 'foo.bar'

func GetFloat64

func GetFloat64(key string) float64

GetFloat64 gets a float 64 json node from a json key. JSON key example: 'foo.bar'

func GetInt

func GetInt(key string) int

GetInt gets an integer json node from a json key. JSON key example: 'foo.bar'

func GetInt64

func GetInt64(key string) int64

GetInt64 gets an integer 64 json node from a json key. JSON key example: 'foo.bar'

func GetString

func GetString(key string) string

GetString gets a string json node from a json key. JSON key example: 'foo.bar'

func LoadConfigFile added in v1.1.0

func LoadConfigFile(name string) error

LoadConfigFile Open file that contins a json object.

func RegisterConfigurator added in v1.1.0

func RegisterConfigurator(conf Configurator)

RegisterConfigurator a new implement of Configurator interface

func ValidateConfiguratorRegister added in v1.1.0

func ValidateConfiguratorRegister() error

ValidateConfiguratorRegister validate if the Configurator interfaces has been implement

Types

type Configurator added in v1.1.0

type Configurator interface {
	// GetString gets a string json node from a json key.
	// JSON key example: 'foo.bar'
	GetString(key string) string

	// GetInt64 gets an integer 64 json node from a json key.
	// JSON key example: 'foo.bar'
	GetInt64(key string) int64

	// GetInt gets an integer json node from a json key.
	// JSON key example: 'foo.bar'
	GetInt(key string) int

	// GetFloat64 gets a float 64 json node from a json key.
	// JSON key example: 'foo.bar'
	GetFloat64(key string) float64

	// GetBool gets a bool json node from a json key.
	// JSON key example: 'foo.bar'
	GetBool(key string) bool

	// Get gets a json node from a json key.
	// JSON key example: 'foo.bar'
	Get(key string) interface{}

	// GetArrayMaps gets a array json node from a json key. Returns
	// a json array as map.
	// JSON key example: 'foo.bar'
	GetArrayMaps(key string) []map[string]interface{}

	// Open file that contins a json object.
	Open(name string) error
}

Configurator interface provider

type JSONConfigurator added in v1.1.0

type JSONConfigurator struct{}

JSONConfigurator implement interface provider.

func (JSONConfigurator) Get added in v1.1.0

func (conf JSONConfigurator) Get(key string) interface{}

Get gets a json node from a json key. JSON key example: 'foo.bar'

func (JSONConfigurator) GetArrayMaps added in v1.1.0

func (conf JSONConfigurator) GetArrayMaps(key string) []map[string]interface{}

GetArrayMaps gets a array json node from a json key. Returns a json array as map. JSON key example: 'foo.bar'

func (JSONConfigurator) GetBool added in v1.1.0

func (conf JSONConfigurator) GetBool(key string) bool

GetBool gets a bool json node from a json key. JSON key example: 'foo.bar'

func (JSONConfigurator) GetFloat64 added in v1.1.0

func (conf JSONConfigurator) GetFloat64(key string) float64

GetFloat64 gets a float 64 json node from a json key. JSON key example: 'foo.bar'

func (JSONConfigurator) GetInt added in v1.1.0

func (conf JSONConfigurator) GetInt(key string) int

GetInt gets an integer json node from a json key. JSON key example: 'foo.bar'

func (JSONConfigurator) GetInt64 added in v1.1.0

func (conf JSONConfigurator) GetInt64(key string) int64

GetInt64 gets an integer 64 json node from a json key. JSON key example: 'foo.bar'

func (JSONConfigurator) GetString added in v1.1.0

func (conf JSONConfigurator) GetString(key string) string

GetString gets a string json node from a json key. JSON key example: 'foo.bar'

func (JSONConfigurator) Open added in v1.1.0

func (conf JSONConfigurator) Open(name string) error

Open file that contins a json object.

Jump to

Keyboard shortcuts

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