file

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2025 License: MIT Imports: 11 Imported by: 0

README

File-based Feature Flag Module

This module provides a file-based implementation for the feature flag system. It allows you to load feature flags from a JSON file.

JSON Structure

The feature flags are defined in a JSON file. The structure supports nested objects, which are treated as folders.

Example config.json:

{
    "key_bool": true,
    "key_string": "some string value",
    "key_int": 42,
    "key_float": 3.14,
    "feature_group": {
        "sub_feature_enabled": true,
        "config_value": "nested value"
    }
}

Usage

Initialization

To use the file module, you need to initialize it with the path to your JSON configuration file. You can optionally provide a prefix to scope the feature flags to a specific section of the JSON.

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/aidapedia/gdk/featureflag/module/file"
)

func main() {
	// Initialize with the path to the JSON file and an empty prefix
	ffModule := file.New("/path/to/config.json", "")

	// Or initialize with a prefix to scope to "feature_group"
	// ffModule := file.New("/path/to/config.json", "feature_group")
}
Retrieving Values

You can retrieve values using the standard methods provided by the module interface. Note that for nested keys, you should use the / separator.

ctx := context.Background()

// Get a boolean value
enabled, err := ffModule.GetBool(ctx, "key_bool")
if err != nil {
    log.Printf("Error getting bool: %v", err)
}

// Get a string value
strVal, err := ffModule.GetString(ctx, "key_string")
if err != nil {
    log.Printf("Error getting string: %v", err)
}

// Get an integer value
intVal, err := ffModule.GetInt(ctx, "key_int")
if err != nil {
    log.Printf("Error getting int: %v", err)
}

// Get a value from a nested structure (if initialized without prefix)
// Use slash separator for nested keys
nestedVal, err := ffModule.GetBool(ctx, "feature_group/sub_feature_enabled") 

// Get a struct value
// The value in JSON must be a string containing the JSON representation of the struct
// Example JSON: "key_struct": "{\"field\": \"value\"}"
type MyStruct struct {
    Field string `json:"field"`
}
var myStruct MyStruct
err = ffModule.GetStruct(ctx, "key_json_string", &myStruct)
if err != nil {
    log.Printf("Error getting struct: %v", err)
}
Watching for Changes

The Watch method allows you to monitor the configuration file for changes. It polls the file every 5 seconds. If a change is detected, it updates the internal state and sends a signal on the returned channel.

// Start watching for changes
changeChan, err := ffModule.Watch(ctx)
if err != nil {
    log.Fatalf("Failed to watch for changes: %v", err)
}

// Listen for changes in a goroutine
go func() {
    for range changeChan {
        log.Println("Configuration file changed! Reloading values...")
        // You can now retrieve the updated values
        newVal, _ := ffModule.GetBool(ctx, "key_bool")
        log.Printf("New value for key_bool: %v", newVal)
    }
}()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(address, prefix string) module.Interface

New creates a new file module.

Types

type FeatureFlag

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

File is the file module.

func (*FeatureFlag) GetBool

func (i *FeatureFlag) GetBool(ctx context.Context, key string) (bool, error)

func (*FeatureFlag) GetInt

func (i *FeatureFlag) GetInt(ctx context.Context, key string) (int, error)

func (*FeatureFlag) GetString

func (i *FeatureFlag) GetString(ctx context.Context, key string) (string, error)

func (*FeatureFlag) GetStruct

func (i *FeatureFlag) GetStruct(ctx context.Context, key string, v interface{}) error

func (*FeatureFlag) GetValue

func (i *FeatureFlag) GetValue(ctx context.Context, key string) (interface{}, error)

func (*FeatureFlag) Watch

func (i *FeatureFlag) Watch(ctx context.Context) (chan bool, error)

type Folder

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

func (*Folder) Add

func (f *Folder) Add(child Node) FolderItf

func (*Folder) GetChild

func (f *Folder) GetChild(name string) Node

func (*Folder) GetName

func (f *Folder) GetName() string

func (*Folder) GetType

func (f *Folder) GetType() nodeType

type FolderItf

type FolderItf interface {
	Node
	GetChild(name string) Node
	Add(child Node) FolderItf
}

func NewFolder

func NewFolder(name string) FolderItf

type Key

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

func (*Key) GetName

func (k *Key) GetName() string

func (*Key) GetType

func (k *Key) GetType() nodeType

func (*Key) GetValue

func (k *Key) GetValue() interface{}

type KeyItf

type KeyItf interface {
	Node
	GetValue() interface{}
}

func NewKey

func NewKey(name string, value interface{}) KeyItf

type Node

type Node interface {
	GetName() string
	GetType() nodeType
}

Jump to

Keyboard shortcuts

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