Lint Feature Flags Programmatically
This example demonstrates how to lint/validate a GO Feature Flag configuration programmatically without using the CLI.
Overview
This example shows how to:
- Retrieve flag configurations from your storage
- Unmarshal the configuration into DTOs
- Validate each flag configuration programmatically
Prerequisites
- Access to the GO Feature Flag core module
Installation
Step 1: Install Dependencies
Install the required dependencies:
go get github.com/thomaspoignant/go-feature-flag/modules/core
Step 2: Retrieve Flag Configuration from Storage
myFlags := getMyFlagsFromStorage()
This simulates retrieving your flag configuration from a storage system (database, file system, API, etc.). In this example, it returns a YAML string, but you can adapt this to your specific storage mechanism.
Replace getMyFlagsFromStorage() with your actual retrieval logic.
Step 3: Unmarshal Flag Configuration
var flags map[string]dto.DTO
err := yaml.Unmarshal([]byte(myFlags), &flags)
if err != nil {
panic(err) // Don't forget to handle errors appropriately in production code.
}
The configuration is unmarshaled from YAML format into a map where:
- Key: The flag key (identifier)
- Value: A
dto.DTO object representing the flag configuration
Note: You can also use JSON or TOML formats depending on your storage format. Simply use the appropriate unmarshaler (json.Unmarshal or toml.Unmarshal).
Step 4: Validate Each Flag Configuration
for key, flagDto := range flags {
convertedFlag := flagDto.Convert()
if err := convertedFlag.IsValid(); err != nil {
panic(fmt.Sprintf("Invalid flag %s: %s", key, err.Error()))
}
}
For each flag:
- Convert the DTO to a Flag object using
flagDto.Convert()
- Validate the flag using
IsValid()
- Handle any validation errors appropriately
Handle validation errors according to your application's error handling strategy.
Additional Resources