Helpers Package
Helpers Package
Section titled “Helpers Package”Generic utilities - Portable helper functions with no external dependencies.
Overview
Section titled “Overview”The Helpers package (packages/helpers/) provides generic utility functions that can be used in any Go project. It has no external dependencies beyond the standard library.
Package Structure
Section titled “Package Structure”packages/helpers/├── config.go # Configuration parsing├── sanitizer.go # JSON/JSONC sanitization└── pointers.go # Pointer helpersConfiguration Helpers
Section titled “Configuration Helpers”ParseStringWithDefault
Section titled “ParseStringWithDefault”Parse string with fallback value:
func ParseStringWithDefault(value string, def string) stringUsage:
// Returns "default" if value is emptyresult := helpers.ParseStringWithDefault("", "default") // "default"result := helpers.ParseStringWithDefault("value", "default") // "value"result := helpers.ParseStringWithDefault(" ", "default") // "default" (trimmed)ParseIntWithDefault
Section titled “ParseIntWithDefault”Parse integer with fallback:
func ParseIntWithDefault(value string, def int) intUsage:
result := helpers.ParseIntWithDefault("42", 0) // 42result := helpers.ParseIntWithDefault("", 0) // 0result := helpers.ParseIntWithDefault("abc", 0) // 0 (invalid)ParseBoolWithDefault
Section titled “ParseBoolWithDefault”Parse boolean with fallback:
func ParseBoolWithDefault(value string, def bool) boolSupported true values: 1, true, yes, on
Supported false values: 0, false, no, off
Usage:
result := helpers.ParseBoolWithDefault("true", false) // trueresult := helpers.ParseBoolWithDefault("yes", false) // trueresult := helpers.ParseBoolWithDefault("1", false) // trueresult := helpers.ParseBoolWithDefault("", false) // falseresult := helpers.ParseBoolWithDefault("invalid", false) // falseJSONC Sanitizer
Section titled “JSONC Sanitizer”Handle JSON with Comments (JSONC) format.
SanitizeJSONC
Section titled “SanitizeJSONC”Remove comments from JSONC string:
func SanitizeJSONC(jsonc string) stringFeatures:
- Removes single-line comments (
//) - Removes multi-line comments (
/* */) - Removes trailing commas
- Preserves comments inside strings
- Handles edge cases
Usage:
jsonc := `{ // This is a comment "name": "Test", "value": 42, // inline comment /* Multi-line comment */ "enabled": true,}`
clean := helpers.SanitizeJSONC(jsonc)// Returns valid JSON without commentsParseJSONC
Section titled “ParseJSONC”Parse JSONC to map:
func ParseJSONC(jsonc string) (map[string]interface{}, error)Usage:
data, err := helpers.ParseJSONC(`{ // Config file "name": "MyApp", "version": "1.0"}`)// data = map[string]interface{}{"name": "MyApp", "version": "1.0"}ParseJSONCArray
Section titled “ParseJSONCArray”Parse JSONC to array:
func ParseJSONCArray(jsonc string) ([]interface{}, error)Usage:
data, err := helpers.ParseJSONCArray(`[ // Items {"id": 1}, {"id": 2},]`)ParseJSONCToString
Section titled “ParseJSONCToString”Parse and re-stringify to clean JSON:
func ParseJSONCToString(jsonc string) (string, error)Usage:
clean, err := helpers.ParseJSONCToString(`{ "name": "Test", // comment}`)// Returns formatted JSON string without commentsParseJSONCToType
Section titled “ParseJSONCToType”Generic JSONC to type parsing:
func ParseJSONCToType[T any](jsonc string) (*T, error)Usage:
type Config struct { Name string `json:"name"` Version string `json:"version"`}
config, err := helpers.ParseJSONCToType[Config](`{ "name": "MyApp", "version": "1.0"}`)JSON Utilities
Section titled “JSON Utilities”ParseJsonToString
Section titled “ParseJsonToString”Convert map to JSON string:
func ParseJsonToString(m map[string]any) (string, error)Usage:
data := map[string]any{"name": "Test", "count": 42}jsonStr, err := helpers.ParseJsonToString(data)// jsonStr = `{"count":42,"name":"Test"}`Import Path
Section titled “Import Path”import "gitlab.com/altacrest/flow_binaries/packages/helpers"Use Cases
Section titled “Use Cases”| Use Case | Function |
|---|---|
| Environment variables | ParseStringWithDefault, ParseIntWithDefault |
| Configuration files | ParseBoolWithDefault |
| JSONC config files | ParseJSONC, SanitizeJSONC |
| API responses | ParseJSONCToType |
| Dynamic data | ParseJsonToString |
Examples
Section titled “Examples”Environment Configuration
Section titled “Environment Configuration”import "os"import "gitlab.com/altacrest/flow_binaries/packages/helpers"
func LoadConfig() Config { return Config{ Port: helpers.ParseIntWithDefault(os.Getenv("PORT"), 8080), Host: helpers.ParseStringWithDefault(os.Getenv("HOST"), "localhost"), Debug: helpers.ParseBoolWithDefault(os.Getenv("DEBUG"), false), }}JSONC Configuration File
Section titled “JSONC Configuration File”func LoadJSONCConfig(path string) (*Config, error) { data, err := os.ReadFile(path) if err != nil { return nil, err }
return helpers.ParseJSONCToType[Config](string(data))}Dependencies
Section titled “Dependencies”This package has no external dependencies - only uses Go standard library.
Related Documentation
Section titled “Related Documentation”- Packages Index - Package overview
- Core Helpers - Core-specific utilities
- Backend Helpers - Backend utilities