|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
| 17 | +/* |
| 18 | +Package conf provides a configuration binding framework with hierarchical resolution, |
| 19 | +type-safe mapping, and validation capabilities. |
| 20 | +
|
| 21 | +# Core Concepts: |
| 22 | +
|
| 23 | +The framework enables mapping configuration data from multiple sources into Go structures with: |
| 24 | +
|
| 25 | +- Hierarchical property resolution using ${key} syntax |
| 26 | +- Type-safe binding with automatic conversions |
| 27 | +- Expression-based validation |
| 28 | +- Extensible architecture via pluggable components |
| 29 | +
|
| 30 | +# Tag Syntax: |
| 31 | +
|
| 32 | +Struct tags use the following format: |
| 33 | +
|
| 34 | + value:"${key:=default}>>splitter" |
| 35 | +
|
| 36 | +Key features: |
| 37 | +- Nested keys (e.g., service.endpoint) |
| 38 | +- Dynamic defaults (e.g., ${DB_HOST:=localhost:${DB_PORT:=3306}}) |
| 39 | +- Splitter chaining (e.g., >>json for JSON parsing) |
| 40 | +
|
| 41 | +# Data Binding: |
| 42 | +
|
| 43 | +Supports binding to various types with automatic conversion: |
| 44 | +
|
| 45 | +1. Primitives: Uses strconv for basic type conversions |
| 46 | +2. Complex Types: |
| 47 | + - Slices: From indexed properties or custom splitters |
| 48 | + - Maps: Via subkey expansion |
| 49 | + - Structs: Recursive binding of nested structures |
| 50 | +
|
| 51 | +3. Custom Types: Register converters using RegisterConverter |
| 52 | +
|
| 53 | +# Validation System: |
| 54 | +
|
| 55 | + 1. Expression validation using expr tag: |
| 56 | + type Config struct { |
| 57 | + Port int `expr:"$ > 0 && $ < 65535"` |
| 58 | + } |
| 59 | +
|
| 60 | + 2. Custom validators: |
| 61 | + RegisterValidateFunc("futureDate", func(t time.Time) bool { |
| 62 | + return t.After(time.Now()) |
| 63 | + }) |
| 64 | +
|
| 65 | +# File Support: |
| 66 | +
|
| 67 | +Built-in readers handle: |
| 68 | +- JSON (.json) |
| 69 | +- Properties (.properties) |
| 70 | +- YAML (.yaml/.yml) |
| 71 | +- TOML (.toml/.tml) |
| 72 | +
|
| 73 | +Register custom readers with RegisterReader. |
| 74 | +
|
| 75 | +# Property Resolution: |
| 76 | +
|
| 77 | +- Recursive ${} substitution |
| 78 | +- Type-aware defaults |
| 79 | +- Chained defaults (${A:=${B:=C}}) |
| 80 | +
|
| 81 | +# Extension Points: |
| 82 | +
|
| 83 | +1. RegisterSplitter: Add custom string splitters |
| 84 | +2. RegisterConverter: Add type converters |
| 85 | +3. RegisterReader: Support new file formats |
| 86 | +4. RegisterValidateFunc: Add custom validators |
| 87 | +
|
| 88 | +# Examples: |
| 89 | +
|
| 90 | +Basic binding: |
| 91 | +
|
| 92 | + type ServerConfig struct { |
| 93 | + Host string `value:"${host:=localhost}"` |
| 94 | + Port int `value:"${port:=8080}"` |
| 95 | + } |
| 96 | +
|
| 97 | +Nested structure: |
| 98 | +
|
| 99 | + type AppConfig struct { |
| 100 | + DB Database `value:"${db}"` |
| 101 | + Timeout string `value:"${timeout:=5s}"` |
| 102 | + } |
| 103 | +
|
| 104 | +Slice binding: |
| 105 | +
|
| 106 | + type Config struct { |
| 107 | + Endpoints []string `value:"${endpoints}"` // Indexed properties |
| 108 | + Features []string `value:"${features}>>split"` // Custom splitter |
| 109 | + } |
| 110 | +
|
| 111 | +Validation: |
| 112 | +
|
| 113 | + type UserConfig struct { |
| 114 | + Age int `value:"${age}" expr:"$ >= 18"` |
| 115 | + Email string `value:"${email}" expr:"contains($, '@')"` |
| 116 | + Expires time.Time `value:"${expires}" expr:"futureDate($)"` |
| 117 | + } |
| 118 | +*/ |
17 | 119 | package conf
|
18 | 120 |
|
19 | 121 | import (
|
|
0 commit comments