structviewer

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2024 License: MPL-2.0 Imports: 10 Imported by: 0

README

struct_viewer

The struct_viewer package is a Go library that provides functionality for managing application configurations using Go struct types and environment variables. It allows you to define your configuration as a struct and easily populate it with values from environment variables

Installation

To install the struct_viewer package, use the go get command:

go get -u github.com/TykTechnologies/struct-viewer

Usage

package main

import (
	"log"
	"net/http"

	"github.com/TykTechnologies/structviewer"
)

type Config struct {
	ListenPort int    `json:"listen_port"`
	Debug      bool   `json:"debug"`
	LogFile    string `json:"log_file"`
}

func main() {
	config := &struct_viewer.Config{
		Object: &Config{
			ListenPort: 8080,
			Debug:      true,
			LogFile:    "/var/log/app.log",
		},
		Path:          "./main.go",
		ParseComments: true,
	}

	// prefix is added to each env var
	v, err := structviewer.New(config, "APP_")
	if err != nil {
		panic(err)
	}

	http.HandleFunc("/config", v.JSONHandler)
	http.HandleFunc("/envs", v.EnvsHandler)
	log.Println("Running server on port :8080")
	err = http.ListenAndServe(":8080", nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}

This will expose two endpoints:

  • /config: returns a json representation of the config struct
  • /envs: returns a json representation of the environment variables of the config struct

You can pass query parameters field or env to the endpoints to retrieve specific config fields or specific environment variables.

Limitations

  • Only exported fields in struct are parsed
  • Only struct fields can have comments in them
  • Single file parsing
  • No obfuscation

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvement, please open an issue or submit a pull request on GitHub.

Documentation

Index

Constants

View Source
const (
	// JSONQueryKey is the query key for JSONHandler
	JSONQueryKey = "field"
	// EnvQueryKey is the query key for EnvsHandler
	EnvQueryKey = "env"
)
View Source
const StructViewerTag = "structviewer"

Variables

View Source
var (
	// ErrNilConfig is returned when the Config struct is nil.
	ErrNilConfig = errors.New("invalid Config structure provided")
	// ErrEmptyStruct is returned when config.Object is nil
	ErrEmptyStruct = errors.New("empty Struct in configuration")
	// ErrInvalidObjectType is returned when config.Object is not a struct.
	ErrInvalidObjectType = errors.New("invalid object type")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	// Object represents the config struct that is going to be parsed.
	Object interface{}

	// ParseComments decides parsing comments of given Object or not. If it is set to false,
	// the comment parser skips parsing comments of given Object.
	ParseComments bool

	// Path is the file path of the Object. Needed for comment parser.
	// Default value is "./config.go".
	Path string
}

Config represents configuration structure.

type EnvVar

type EnvVar struct {

	// ConfigField represents a JSON notation of the given struct fields.
	ConfigField string `json:"config_field,omitempty"`
	// Env represents an environment variable notation of the given struct fields.
	Env string `json:"env,omitempty"`
	// Description represents the comment of the given struct fields.
	Description string `json:"description,omitempty"`
	// Value represents the value of the given struct fields.
	Value interface{} `json:"value"`
	// Obfuscated represents whether the given struct field is obfuscated or not.
	// This is a pointer to a boolean value to distinguish between the zero value
	// and the actual value (because of the 'omitempty' tag).
	Obfuscated *bool `json:"obfuscated,omitempty"`
	// contains filtered or unexported fields
}

EnvVar is a key:value string struct for environment variables representation

func (EnvVar) String

func (ev EnvVar) String() string

String returns a key:value string from EnvVar

type Viewer

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

Viewer is the pkg control structure where the prefix and env vars are stored.

func New

func New(config *Config, prefix string) (*Viewer, error)

New receives a configuration structure and a prefix and returns a Viewer struct to manipulate this library.

func (*Viewer) ConfigHandler

func (v *Viewer) ConfigHandler(rw http.ResponseWriter, r *http.Request)

ConfigHandler exposes the configuration struct as JSON fields

func (*Viewer) DetailedConfigHandler

func (v *Viewer) DetailedConfigHandler(rw http.ResponseWriter, r *http.Request)

DetailedConfigHandler exposes the detailed configuration struct as JSON fields

func (*Viewer) EnvNotation

func (v *Viewer) EnvNotation(jsonField string) EnvVar

EnvNotation takes JSON notation of a configuration field (e.g, 'listen_port') and returns EnvVar object of the given notation.

func (*Viewer) Envs

func (v *Viewer) Envs() []*EnvVar

Envs returns environment variables parsed by struct-viewer.

func (*Viewer) EnvsHandler

func (v *Viewer) EnvsHandler(rw http.ResponseWriter, r *http.Request)

EnvsHandler expose the environment variables of the configuration struct

func (*Viewer) JSONNotation

func (v *Viewer) JSONNotation(envVarNotation string) EnvVar

JSONNotation takes environment variable and returns EnvVars object of the given environment variable.

func (*Viewer) ParseEnvs

func (v *Viewer) ParseEnvs() []string

ParseEnvs parse Viewer config field, generating a string slice of prefix+key:value of each config field

Directories

Path Synopsis
internal
test command

Jump to

Keyboard shortcuts

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