### Install Jety Go Package Source: https://github.com/taigrr/jety/blob/master/README.md This command installs the Jety library using the Go build tool. It requires Go version 1.25.5 or later. Ensure your Go environment is set up correctly before running this command. ```bash go get github.com/taigrr/jety ``` -------------------------------- ### Environment Variables with Prefix in Jety Source: https://github.com/taigrr/jety/blob/master/README.md Provides examples of environment variables when a prefix is used with Jety. The prefix is prepended to the configuration keys, allowing for scoped environment variable management. ```bash export MYAPP_PORT=9000 export MYAPP_SERVICES_CLOUD_VAR=override_value ``` -------------------------------- ### Get Raw Configuration Values in Go Source: https://context7.com/taigrr/jety/llms.txt Illustrates how to retrieve raw configuration values using the `Get` method in Go. This method returns the value as an `any` type and returns `nil` if the key is not found. Type-specific getters should be used for automatic type conversion. ```go package main import ( "fmt" "github.com/taigrr/jety" ) func main() { cm := jety.NewConfigManager() cm.Set("name", "myapp") cm.Set("port", 8080) cm.Set("features", map[string]any{"auth": true, "logging": false}) // Get raw values name := cm.Get("name") port := cm.Get("port") features := cm.Get("features") missing := cm.Get("nonexistent") fmt.Printf("Name: %v (%T)\n", name, name) // Output: Name: myapp (string) fmt.Printf("Port: %v (%T)\n", port, port) // Output: Port: 8080 (int) fmt.Printf("Features: %v\n", features) // Output: Features: map[auth:true logging:false] fmt.Printf("Missing: %v\n", missing) // Output: Missing: } ``` -------------------------------- ### Quick Start: Jety Configuration Loading in Go Source: https://github.com/taigrr/jety/blob/master/README.md Demonstrates basic usage of Jety to set default values, read configuration from a TOML file, and retrieve values. It shows the precedence order: config file > environment variables > defaults. ```go package main import "github.com/taigrr/jety" func main() { // Set defaults jety.SetDefault("port", 8080) jety.SetDefault("host", "localhost") // Environment variables are loaded automatically // e.g., PORT=9000 overrides the default // Read from config file jety.SetConfigFile("config.toml") jety.SetConfigType("toml") if err := jety.ReadInConfig(); err != nil { // handle error } // Get values (config file > env > default) port := jety.GetInt("port") host := jety.GetString("host") } ``` -------------------------------- ### Complete Configuration Example (Go) Source: https://context7.com/taigrr/jety/llms.txt Demonstrates comprehensive configuration loading, including file reading, environment variable overrides, default values, and accessing nested configuration. Supports TOML, YAML, and JSON formats. Environment variables take precedence over config files, which in turn override defaults. ```go package main import ( "log" "os" "github.com/taigrr/jety" ) // config.toml: // port = 8080 // host = "localhost" // debug = true // timeout = "30s" // // [database] // host = "db.example.com" // port = 5432 // // [database.credentials] // username = "admin" // password = "secret" func main() { // Environment can override config file values for specific keys os.Setenv("PORT", "9000") defer os.Unsetenv("PORT") // Set defaults (lowest priority) jety.SetDefault("port", 3000) jety.SetDefault("host", "127.0.0.1") jety.SetDefault("timeout", "10s") // Configure file location jety.SetConfigFile("config.toml") if err := jety.SetConfigType("toml"); err != nil { log.Fatal(err) } // Read config (overrides defaults, but env vars override defaults too) if err := jety.ReadInConfig(); err != nil { log.Printf("Warning: %v, using defaults", err) } // Access simple values port := jety.GetInt("port") // 8080 from file (file > env > default) host := jety.GetString("host") // "localhost" from file debug := jety.GetBool("debug") // true from file timeout := jety.GetDuration("timeout") // 30s from file log.Printf("Server: %s:%d (debug=%v, timeout=%v)", host, port, debug, timeout) // Access nested configuration db := jety.GetStringMap("database") if db != nil { dbHost := db["host"].(string) dbPort := db["port"].(int64) // TOML decodes numbers as int64 log.Printf("Database: %s:%d", dbHost, dbPort) // Deeper nesting if creds, ok := db["credentials"].(map[string]any); ok { log.Printf("DB User: %s", creds["username"]) } } } ``` -------------------------------- ### Accessing Nested Configuration in Jety (TOML Example) Source: https://github.com/taigrr/jety/blob/master/README.md Illustrates how to access nested configuration values from a TOML file using `GetStringMap` and type assertions. This method is useful for complex configuration structures. ```go services := jety.GetStringMap("services") cloud := services["cloud"].(map[string]any) varValue := cloud["var"].(string) // "xyz" // For deeper nesting auth := cloud["auth"].(map[string]any) clientID := auth["client_id"].(string) // "abc123" ``` -------------------------------- ### Get Boolean Configuration Values in Go Source: https://context7.com/taigrr/jety/llms.txt Explains how to get boolean configuration values using `GetBool` in Go. It supports smart type conversion, recognizing strings like "true" (case-insensitive), non-zero numbers, and positive durations as true. Returns false for missing keys. ```go package main import ( "fmt" "time" "github.com/taigrr/jety" ) func main() { cm := jety.NewConfigManager() cm.Set("bool_true", true) cm.Set("bool_false", false) cm.Set("string_true", "TRUE") cm.Set("string_false", "false") cm.Set("int_nonzero", 1) cm.Set("int_zero", 0) cm.Set("duration", time.Second) fmt.Println(cm.GetBool("bool_true")) // Output: true fmt.Println(cm.GetBool("bool_false")) // Output: false fmt.Println(cm.GetBool("string_true")) // Output: true (case-insensitive) fmt.Println(cm.GetBool("string_false")) // Output: false fmt.Println(cm.GetBool("int_nonzero")) // Output: true fmt.Println(cm.GetBool("int_zero")) // Output: false fmt.Println(cm.GetBool("duration")) // Output: true (positive duration) fmt.Println(cm.GetBool("missing")) // Output: false } ``` -------------------------------- ### Get String Configuration Values in Go Source: https://context7.com/taigrr/jety/llms.txt Shows how to retrieve configuration values as strings using `GetString` in Go. This method converts non-string types to strings using `fmt.Sprintf` and returns an empty string for non-existent keys. ```go package main import ( "fmt" "github.com/taigrr/jety" ) func main() { cm := jety.NewConfigManager() cm.Set("host", "localhost") cm.Set("port", 8080) cm.Set("enabled", true) // All converted to string fmt.Println(cm.GetString("host")) // Output: localhost fmt.Println(cm.GetString("port")) // Output: 8080 fmt.Println(cm.GetString("enabled")) // Output: true fmt.Println(cm.GetString("missing")) // Output: (empty string) } ``` -------------------------------- ### Get Integer Configuration Values in Go Source: https://context7.com/taigrr/jety/llms.txt Demonstrates retrieving integer configuration values with `GetInt` in Go. This method supports automatic type conversion from strings, float32, float64, and int64. It returns 0 for missing keys or unparseable values. ```go package main import ( "fmt" "github.com/taigrr/jety" ) func main() { cm := jety.NewConfigManager() cm.Set("int_val", 42) cm.Set("string_val", "123") cm.Set("float_val", 99.9) cm.Set("invalid", "not-a-number") fmt.Println(cm.GetInt("int_val")) // Output: 42 fmt.Println(cm.GetInt("string_val")) // Output: 123 fmt.Println(cm.GetInt("float_val")) // Output: 99 (truncated) fmt.Println(cm.GetInt("invalid")) // Output: 0 fmt.Println(cm.GetInt("missing")) // Output: 0 } ``` -------------------------------- ### Get Configuration File Path (Go) Source: https://context7.com/taigrr/jety/llms.txt Retrieves the path of the configuration file that was read or will be written. This function is useful for logging and debugging purposes, indicating which configuration file is currently in use by the manager. ```go package main import ( "log" "github.com/taigrr/jety" ) func main() { jety.SetConfigFile("/etc/myapp/config.yaml") if err := jety.SetConfigType("yaml"); err != nil { log.Fatal(err) } if err := jety.ReadInConfig(); err != nil { log.Printf("Could not read config from %s: %v", jety.ConfigFileUsed(), err) log.Println("Using default values") } else { log.Printf("Configuration loaded from: %s", jety.ConfigFileUsed()) } } ``` -------------------------------- ### Using Environment Prefix with Jety Config Manager Source: https://github.com/taigrr/jety/blob/master/README.md Demonstrates how to use `WithEnvPrefix` to filter environment variables by a specific prefix. This helps in organizing environment variables for different applications or services. ```go cm := jety.NewConfigManager().WithEnvPrefix("MYAPP_") ``` -------------------------------- ### Set Configuration Values Programmatically in Go Source: https://context7.com/taigrr/jety/llms.txt Demonstrates how to set configuration values using `Set`, `SetDefault`, `SetString`, and `SetBool` in Go. `Set` values have the highest precedence. `SetDefault` provides fallback values. Type-specific setters ensure correct data types. ```go package main import ( "fmt" "github.com/taigrr/jety" ) func main() { cm := jety.NewConfigManager() // Set defaults (lowest priority) cm.SetDefault("port", 8080) cm.SetDefault("debug", false) cm.SetDefault("host", "localhost") // Set explicit values (highest priority) cm.Set("port", 9090) cm.SetString("host", "0.0.0.0") cm.SetBool("debug", true) fmt.Printf("Port: %d\n", cm.GetInt("port")) // Output: 9090 fmt.Printf("Host: %s\n", cm.GetString("host")) // Output: 0.0.0.0 fmt.Printf("Debug: %v\n", cm.GetBool("debug")) // Output: true } ``` -------------------------------- ### Set Configuration File Path and Read Source: https://context7.com/taigrr/jety/llms.txt Sets the full path to the configuration file to be read and then attempts to read and parse it. This method is a direct way to specify the configuration source. It requires setting the config type before reading. ```go package main import ( "log" "github.com/taigrr/jety" ) func main() { jety.SetConfigFile("/etc/myapp/config.yaml") if err := jety.SetConfigType("yaml"); err != nil { log.Fatal(err) } if err := jety.ReadInConfig(); err != nil { log.Fatal(err) } // Now access config values port := jety.GetInt("port") log.Printf("Server starting on port %d", port) } ``` -------------------------------- ### Set Configuration Directory and Name Separately Source: https://context7.com/taigrr/jety/llms.txt Allows setting the configuration directory and file name independently, without the file extension. The library will then look for a file matching the specified type (e.g., `.yaml`, `.toml`) within that directory. This provides more flexibility in locating configuration files. ```go package main import ( "log" "github.com/taigrr/jety" ) func main() { cm := jety.NewConfigManager() // Set directory and name separately cm.SetConfigDir("/etc/myapp") cm.SetConfigName("config") // Without extension if err := cm.SetConfigType("yaml"); err != nil { log.Fatal(err) } // Will look for /etc/myapp/config.yaml if err := cm.ReadInConfig(); err != nil { log.Fatal(err) } log.Printf("Config loaded from: %s", cm.ConfigFileUsed()) // Output: Config loaded from: /etc/myapp/config.yaml } ``` -------------------------------- ### GetStringMap: Access nested configuration as map[string]any in Go Source: https://context7.com/taigrr/jety/llms.txt Demonstrates how to use GetStringMap to retrieve nested configuration structures as a map[string]any. It returns nil if the key does not exist or the value is not a map. This function is useful for navigating complex configurations. Requires the 'github.com/taigrr/jety' package. ```go package main import ( "fmt" "github.com/taigrr/jety" ) func main() { cm := jety.NewConfigManager() cm.Set("database", map[string]any{ "host": "localhost", "port": 5432, "credentials": map[string]any{ "username": "admin", "password": "secret", }, }) db := cm.GetStringMap("database") fmt.Printf("Host: %s\n", db["host"]) fmt.Printf("Port: %v\n", db["port"]) creds := db["credentials"].(map[string]any) fmt.Printf("User: %s\n", creds["username"]) fmt.Println(cm.GetStringMap("missing")) } ``` -------------------------------- ### Write Configuration to File (Go) Source: https://context7.com/taigrr/jety/llms.txt Writes the current configuration to a specified file. Supports TOML, YAML, and JSON formats. Requires setting the config file path and type before writing. Returns an error if the file cannot be written or the config type is not set. ```go package main import ( "log" "github.com/taigrr/jety" ) func main() { cm := jety.NewConfigManager() cm.SetConfigFile("output.yaml") if err := cm.SetConfigType("yaml"); err != nil { log.Fatal(err) } // Set configuration values cm.Set("server", map[string]any{ "host": "0.0.0.0", "port": 8080, "tls": map[string]any{ "enabled": true, "cert": "/path/to/cert.pem", }, }) cm.Set("debug", false) // Write to file if err := cm.WriteConfig(); err != nil { log.Fatal(err) } log.Printf("Config written to: %s", cm.ConfigFileUsed()) } ``` -------------------------------- ### Read Configuration File with Error Handling Source: https://context7.com/taigrr/jety/llms.txt Reads and parses the configuration file previously set using `SetConfigFile` or `SetConfigDir`/`SetConfigName`. It includes specific error handling for `ErrConfigFileNotFound` and `ErrConfigFileEmpty`, allowing the application to gracefully fall back to default values if the configuration file is missing or empty. ```go package main import ( "errors" "log" "github.com/taigrr/jety" ) func main() { jety.SetConfigFile("config.toml") if err := jety.SetConfigType("toml"); err != nil { log.Fatal(err) } if err := jety.ReadInConfig(); err != nil { if errors.Is(err, jety.ErrConfigFileNotFound) { log.Println("Config file not found, using defaults") } else if errors.Is(err, jety.ErrConfigFileEmpty) { log.Println("Config file is empty, using defaults") } else { log.Fatal(err) } } // Config values are now available log.Printf("Port: %d", jety.GetInt("port")) } ``` -------------------------------- ### WithEnvPrefix: Filter and strip environment variables by prefix in Go Source: https://context7.com/taigrr/jety/llms.txt Explains how to use WithEnvPrefix to filter environment variables based on a specified prefix and remove that prefix from the keys. This is useful for namespacing application-specific environment variables. The function returns the ConfigManager for chaining. Requires the 'github.com/taigrr/jety' package and the 'os' package. ```go package main import ( "fmt" "os" "github.com/taigrr/jety" ) func main() { os.Setenv("MYAPP_PORT", "9000") os.Setenv("MYAPP_HOST", "0.0.0.0") os.Setenv("OTHER_VAR", "ignored") defer os.Unsetenv("MYAPP_PORT") defer os.Unsetenv("MYAPP_HOST") defer os.Unsetenv("OTHER_VAR") cm := jety.NewConfigManager().WithEnvPrefix("MYAPP_") fmt.Println(cm.GetString("port")) fmt.Println(cm.GetString("host")) fmt.Println(cm.GetString("other_var")) } ``` -------------------------------- ### Create Independent ConfigManager Instance Source: https://context7.com/taigrr/jety/llms.txt Creates a new, independent ConfigManager instance that automatically loads all current environment variables. This is useful when you need isolated configurations separate from the global default manager. It allows setting default values and accessing them using type-specific getters. ```go package main import ( "fmt" "github.com/taigrr/jety" ) func main() { // Create independent config manager cm := jety.NewConfigManager() // Configure and use cm.SetDefault("port", 8080) cm.SetDefault("host", "localhost") fmt.Printf("Port: %d, Host: %s\n", cm.GetInt("port"), cm.GetString("host")) // Output: Port: 8080, Host: localhost } ``` -------------------------------- ### GetStringSlice: Retrieve string slices from configuration in Go Source: https://context7.com/taigrr/jety/llms.txt Shows how to use GetStringSlice to retrieve a slice of strings. It can convert slices of any type to string slices by converting each element. Returns nil for non-existent keys or values that are not slices. Requires the 'github.com/taigrr/jety' package. ```go package main import ( "fmt" "github.com/taigrr/jety" ) func main() { cm := jety.NewConfigManager() cm.Set("tags", []string{"api", "v1", "production"}) cm.Set("mixed", []any{"web", 123, "app"}) cm.Set("single", "not-a-slice") fmt.Println(cm.GetStringSlice("tags")) // Output: [api v1 production] fmt.Println(cm.GetStringSlice("mixed")) // Output: [web 123 app] fmt.Println(cm.GetStringSlice("single")) // Output: [] (nil) fmt.Println(cm.GetStringSlice("missing")) // Output: [] (nil) } ``` -------------------------------- ### Set Configuration File Type Source: https://context7.com/taigrr/jety/llms.txt Specifies the format of the configuration file to be used (e.g., 'toml', 'yaml', 'json'). This function returns an error if an unsupported file type is provided. It's crucial to call this before reading the configuration file. ```go package main import ( "log" "github.com/taigrr/jety" ) func main() { cm := jety.NewConfigManager() // TOML configuration if err := cm.SetConfigType("toml"); err != nil { log.Fatal(err) // Only fails for unsupported types like "xml" } // Valid types: "toml", "yaml", "json" // Invalid types return error: "config type xml not supported" } ``` -------------------------------- ### Migration: Jety WriteConfig() Change (v0.x to v1.x) Source: https://github.com/taigrr/jety/blob/master/README.md Illustrates the breaking change in the `WriteConfig()` function between Jety versions v0.x and v1.x. The function now returns an error, which should be handled appropriately. ```go // Before // jety.WriteConfig() // After if err := jety.WriteConfig(); err != nil { // handle error } // Or if you want to ignore the error: _ = jety.WriteConfig() ``` -------------------------------- ### GetDuration: Parse time.Duration from strings and numbers in Go Source: https://context7.com/taigrr/jety/llms.txt Demonstrates how to use the GetDuration function to parse time durations from string representations (e.g., '30s', '2m30s') and numeric types. It handles invalid inputs and missing keys by returning a zero duration. Requires the 'github.com/taigrr/jety' package. ```go package main import ( "fmt" "time" "github.com/taigrr/jety" ) func main() { cm := jety.NewConfigManager() cm.Set("timeout", "30s") cm.Set("interval", "2m30s") cm.Set("duration", 5*time.Second) cm.Set("nanos", int64(1000000000)) // 1 second in nanoseconds cm.Set("invalid", "not-a-duration") fmt.Println(cm.GetDuration("timeout")) // Output: 30s fmt.Println(cm.GetDuration("interval")) // Output: 2m30s fmt.Println(cm.GetDuration("duration")) // Output: 5s fmt.Println(cm.GetDuration("nanos")) // Output: 1s fmt.Println(cm.GetDuration("invalid")) // Output: 0s fmt.Println(cm.GetDuration("missing")) // Output: 0s } ``` -------------------------------- ### GetIntSlice: Retrieve integer slices with type conversion in Go Source: https://context7.com/taigrr/jety/llms.txt Illustrates the GetIntSlice function for retrieving integer slices. It automatically converts elements of other types (like strings or floats) to integers, skipping any elements that cannot be converted. Returns nil for non-existent keys or non-slice values. Requires the 'github.com/taigrr/jety' package. ```go package main import ( "fmt" "github.com/taigrr/jety" ) func main() { cm := jety.NewConfigManager() cm.Set("ports", []int{8080, 8081, 8082}) cm.Set("mixed", []any{10, "20", float64(30), float32(40)}) cm.Set("invalid", []any{1, "not-a-number", nil, 2}) fmt.Println(cm.GetIntSlice("ports")) // Output: [8080 8081 8082] fmt.Println(cm.GetIntSlice("mixed")) // Output: [10 20 30 40] fmt.Println(cm.GetIntSlice("invalid")) // Output: [1 2] (skips invalid entries) fmt.Println(cm.GetIntSlice("missing")) // Output: [] (nil) } ``` -------------------------------- ### Environment Variable Overrides for Nested Config in Jety Source: https://github.com/taigrr/jety/blob/master/README.md Shows how to override nested configuration values using environment variables. For nested keys, the environment variable name is constructed by concatenating the keys in uppercase, separated by underscores. ```bash # Override top-level key export PORT=9000 # For nested keys, use the full key name in uppercase export SERVICES_CLOUD_VAR=override_value ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.