Skip to content

Commit 8ad4c2f

Browse files
authored
Merge pull request #8 from Attumm/add-code-docs
Added initial code docs
2 parents 7367a26 + 0f12a9e commit 8ad4c2f

File tree

2 files changed

+410
-4
lines changed

2 files changed

+410
-4
lines changed

settingo/settingo.go

+294
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
package settingo
22

3+
// SETTINGS is the global instance of the Settings struct for the settingo package.
4+
//
5+
// It provides a package-level access point to manage application settings.
6+
// Use the package functions (e.g., settingo.Set, settingo.Get, settingo.Parse)
7+
// to interact with this global settings registry.
8+
//
9+
// Initialization:
10+
//
11+
// SETTINGS is initialized with empty maps for all setting types when the package is loaded.
12+
// Settings are registered and accessed through the package-level functions.
13+
//
14+
// Example:
15+
//
16+
// package main
17+
//
18+
// import "path/to/settingo"
19+
//
20+
// func main() {
21+
// settingo.SetString("outputDir", "/tmp", "Directory to write output files")
22+
// settingo.Parse()
23+
// outputDir := settingo.Get("outputDir")
24+
// println("Output directory:", outputDir)
25+
// }
326
var SETTINGS = Settings{
427
msg: make(map[string]string),
528
VarString: make(map[string]string),
@@ -12,60 +35,331 @@ var SETTINGS = Settings{
1235
VarBool: make(map[string]bool),
1336
}
1437

38+
// Get retrieves the current string value of a registered string setting from the global SETTINGS instance.
39+
//
40+
// It's a package-level function that delegates to the Get method of the global SETTINGS variable.
41+
//
42+
// Args:
43+
//
44+
// x: The name of the setting flag to retrieve.
45+
//
46+
// Returns:
47+
//
48+
// The current string value of the setting from the global SETTINGS instance.
1549
func Get(x string) string {
1650
return SETTINGS.Get(x)
1751
}
52+
53+
// Set is a package-level function to register a string setting within the global SETTINGS instance.
54+
//
55+
// It delegates to the Set method of the global SETTINGS variable.
56+
// It associates a setting flag name with a default string value and a help message.
57+
//
58+
// Args:
59+
//
60+
// flagName: The name of the setting flag (e.g., "port", "output-dir").
61+
// defaultVar: The default string value for the setting.
62+
// message: The help message describing the setting's purpose.
1863
func Set(flagName, defaultVar, message string) {
1964
SETTINGS.Set(flagName, defaultVar, message)
2065
}
2166

67+
// SetString is a package-level function to register a string setting within the global SETTINGS instance.
68+
//
69+
// It delegates to the SetString method of the global SETTINGS variable.
70+
// Registers a string setting that can be configured via environment variables or command-line flags.
71+
//
72+
// Args:
73+
//
74+
// flagName: The name of the setting flag (e.g., "output-format").
75+
// This name will be used for both environment variable lookup
76+
// (case-sensitive, typically uppercase) and command-line flag parsing
77+
// (lowercase with hyphens).
78+
// defaultVar: The default string value if no environment variable or
79+
// command-line flag is provided.
80+
// message: The help message describing the setting's purpose, shown in
81+
// command-line help output.
82+
//
83+
// Example:
84+
//
85+
// settingo.SetString("outputFormat", "json", "Format for output (json|xml)")
86+
//
87+
// // Can be set via:
88+
// // - Environment variable: OUTPUTFORMAT=xml
89+
// // - Command-line flag: --output-format=xml
2290
func SetString(flagName, defaultVar, message string) {
2391
SETTINGS.Set(flagName, defaultVar, message)
2492
}
2593

94+
// SetInt is a package-level function to register an integer setting within the global SETTINGS instance.
95+
//
96+
// It delegates to the SetInt method of the global SETTINGS variable.
97+
// Registers an integer setting that can be configured via environment variables or command-line flags.
98+
//
99+
// Args:
100+
//
101+
// flagName: The name of the setting flag (e.g., "port").
102+
// Used for environment variable lookup and command-line flag parsing.
103+
// defaultVar: The default integer value.
104+
// message: The help message.
105+
//
106+
// Example:
107+
//
108+
// settingo.SetInt("port", 8080, "Port to listen on")
109+
//
110+
// // Can be set via:
111+
// // - Environment variable: PORT=8081
112+
// // - Command-line flag: --port=8081
26113
func SetInt(flagName string, defaultVar int, message string) {
27114
SETTINGS.SetInt(flagName, defaultVar, message)
28115
}
29116

117+
// SetBool is a package-level function to register a boolean setting within the global SETTINGS instance.
118+
//
119+
// It delegates to the SetBool method of the global SETTINGS variable.
120+
// Registers a boolean setting that can be configured via environment variables or command-line flags.
121+
//
122+
// When set via environment variables or command-line flags, the string values
123+
// are interpreted as boolean using the truthiness function (see truthiness()).
124+
//
125+
// Args:
126+
//
127+
// flagName: The name of the setting flag (e.g., "verbose").
128+
// Used for environment variable lookup and command-line flag parsing.
129+
// defaultVar: The default boolean value.
130+
// message: The help message.
131+
//
132+
// Example:
133+
//
134+
// settingo.SetBool("verbose", false, "Enable verbose output")
135+
//
136+
// // Can be set via:
137+
// // - Environment variable: VERBOSE=true or VERBOSE=y or VERBOSE=yes
138+
// // - Command-line flag: --verbose=true or --verbose=y or --verbose=yes
30139
func SetBool(flagName string, defaultVar bool, message string) {
31140
SETTINGS.SetBool(flagName, defaultVar, message)
32141
}
33142

143+
// SetMap is a package-level function to register a map setting within the global SETTINGS instance.
144+
//
145+
// It delegates to the SetMap method of the global SETTINGS variable.
146+
// Registers a map setting with string keys and string slice values
147+
// that can be configured via environment variables or command-line flags.
148+
//
149+
// The map is parsed from a string representation in the format "key1:value1,value2;key2:value3".
150+
// Keys and values are separated by colons, key-value pairs by semicolons, and values within a key by commas.
151+
//
152+
// Args:
153+
//
154+
// flagName: The name of the setting flag (e.g., "headers").
155+
// Used for environment variable lookup and command-line flag parsing.
156+
// defaultVar: The default map value.
157+
// message: The help message.
158+
//
159+
// Example:
160+
//
161+
// defaultHeaders := map[string][]string{"Content-Type": {"application/json"}, "Accept": {"application/json"}}
162+
// settingo.SetMap("headers", defaultHeaders, "HTTP headers to include")
163+
//
164+
// // Can be set via:
165+
// // - Environment variable: HEADERS="Content-Type:application/json,text/plain;Accept:application/json"
166+
// // - Command-line flag: --headers="Content-Type:application/json,text/plain;Accept:application/json"
34167
func SetMap(flagName string, defaultVar map[string][]string, message string) {
35168
SETTINGS.SetMap(flagName, defaultVar, message)
36169
}
37170

171+
// SetSlice is a package-level function to register a string slice setting within the global SETTINGS instance.
172+
//
173+
// It delegates to the SetSlice method of the global SETTINGS variable.
174+
// Registers a string slice setting that can be configured via environment variables or command-line flags.
175+
//
176+
// The slice is parsed from a string by splitting it using the provided separator.
177+
// If no separator is provided, it defaults to a comma ",".
178+
//
179+
// Args:
180+
//
181+
// flagName: The name of the setting flag (e.g., "hosts").
182+
// Used for environment variable lookup and command-line flag parsing.
183+
// defaultVar: The default slice value.
184+
// message: The help message.
185+
// sep: The separator string used to split the environment variable or
186+
// command-line flag value into a slice of strings. If empty, defaults to ",".
187+
//
188+
// Example:
189+
//
190+
// defaultHosts := []string{"localhost", "127.0.0.1"}
191+
// settingo.SetSlice("hosts", defaultHosts, "List of hosts", ",")
192+
//
193+
// // Can be set via:
194+
// // - Environment variable: HOSTS="host1,host2,host3"
195+
// // - Command-line flag: --hosts="host1,host2,host3"
196+
// // or using a different separator:
197+
// // settingo.SetSlice("ports", []string{"80", "81"}, "Ports to listen on", ";")
198+
// // - Environment variable: PORTS="80;81;82"
199+
// // - Command-line flag: --ports="80;81;82"
38200
func SetSlice(flagName string, defaultVar []string, message string, sep string) {
39201
SETTINGS.SetSlice(flagName, defaultVar, message, sep)
40202
}
41203

204+
// SetParsed is a package-level function to register a string setting with a custom parsing function within the global SETTINGS instance.
205+
//
206+
// It delegates to the SetParsed method of the global SETTINGS variable.
207+
// Registers a string setting with a custom parsing function.
208+
//
209+
// This is useful when you need to perform custom validation or transformation
210+
// on the string value obtained from environment variables or command-line flags
211+
// before using it in your application.
212+
//
213+
// Args:
214+
//
215+
// flagName: The name of the setting flag.
216+
// defaultVar: The default string value.
217+
// message: The help message.
218+
// parserFunc: A function that takes the raw string value (from env or flag)
219+
// and returns a parsed string value. This function will be called
220+
// after retrieving the value from the environment or command line.
221+
//
222+
// Example:
223+
//
224+
// settingo.SetParsed("username", "default", "Username", func(s string) string {
225+
// if s == "" {
226+
// return "anonymous"
227+
// }
228+
// return strings.ToLower(s)
229+
// })
42230
func SetParsed(flagName, defaultVar, message string, parserFunc func(string) string) {
43231
SETTINGS.SetParsed(flagName, defaultVar, message, parserFunc)
44232
}
45233

234+
// SetParsedInt is a package-level function to register an integer setting with a custom parsing function within the global SETTINGS instance.
235+
//
236+
// It delegates to the SetParsedInt method of the global SETTINGS variable.
237+
// Registers an integer setting with a custom parsing function.
238+
//
239+
// This is useful for custom validation or transformation of integer settings.
240+
// Note that while the setting is treated as an integer internally, the defaultVar
241+
// is still a string for consistency with command-line flag handling, and the
242+
// raw value from environment or flag parsing is initially a string.
243+
//
244+
// Args:
245+
//
246+
// flagName: The name of the setting flag.
247+
// defaultVar: The default string value (will be converted to int if possible).
248+
// message: The help message.
249+
// parserFunc: A function that takes the raw integer value (after initial parsing from string)
250+
// and returns a parsed integer value. Note that the input to this function
251+
// is an int, but the original input from environment/flag was a string.
252+
//
253+
// Example:
254+
//
255+
// settingo.SetParsedInt("retries", "3", "Number of retries", func(i int) int {
256+
// if i < 0 {
257+
// return 0 // Ensure retries is not negative
258+
// }
259+
// return i
260+
// })
46261
func SetParsedInt(flagName, defaultVar, message string, parserFunc func(int) int) {
47262
SETTINGS.SetParsedInt(flagName, defaultVar, message, parserFunc)
48263
}
49264

265+
// GetInt retrieves the current integer value of a registered integer setting from the global SETTINGS instance.
266+
//
267+
// It's a package-level function that delegates to the GetInt method of the global SETTINGS variable.
268+
//
269+
// Args:
270+
//
271+
// flagName: The name of the setting flag to retrieve.
272+
//
273+
// Returns:
274+
//
275+
// The current integer value of the setting from the global SETTINGS instance.
50276
func GetInt(flagName string) int {
51277
return SETTINGS.GetInt(flagName)
52278
}
53279

280+
// GetBool retrieves the current boolean value of a registered boolean setting from the global SETTINGS instance.
281+
//
282+
// It's a package-level function that delegates to the GetBool method of the global SETTINGS variable.
283+
//
284+
// Args:
285+
//
286+
// flagName: The name of the setting flag to retrieve.
287+
//
288+
// Returns:
289+
//
290+
// The current boolean value of the setting from the global SETTINGS instance.
54291
func GetBool(flagName string) bool {
55292
return SETTINGS.GetBool(flagName)
56293
}
57294

295+
// GetMap retrieves the current map value of a registered map setting from the global SETTINGS instance.
296+
//
297+
// It's a package-level function that delegates to the GetMap method of the global SETTINGS variable.
298+
//
299+
// Args:
300+
//
301+
// flagName: The name of the setting flag to retrieve.
302+
//
303+
// Returns:
304+
//
305+
// The current map value of the setting from the global SETTINGS instance.
58306
func GetMap(flagName string) map[string][]string {
59307
return SETTINGS.GetMap(flagName)
60308
}
61309

310+
// GetSlice retrieves the current slice value of a registered slice setting from the global SETTINGS instance.
311+
//
312+
// It's a package-level function that delegates to the GetSlice method of the global SETTINGS variable.
313+
//
314+
// Args:
315+
//
316+
// flagName: The name of the setting flag to retrieve.
317+
//
318+
// Returns:
319+
//
320+
// The current slice value of the setting from the global SETTINGS instance.
62321
func GetSlice(flagName string) []string {
63322
return SETTINGS.GetSlice(flagName)
64323
}
65324

325+
// Parse parses settings from both OS environment variables and command-line flags using the global SETTINGS instance.
326+
//
327+
// It's a package-level function that delegates to the Parse method of the global SETTINGS variable.
328+
//
329+
// It first calls HandleOSInput() to parse settings from environment variables,
330+
// and then calls HandleCMDLineInput() to parse command-line flags.
331+
//
332+
// This order ensures that command-line flags take precedence over environment
333+
// variables if a setting is defined in both sources.
334+
//
335+
// Call this function after registering all settings using the Set... functions
336+
// to populate the global SETTINGS instance with values from the environment and command line.
66337
func Parse() {
67338
SETTINGS.Parse()
68339
}
340+
341+
// ParseTo parses settings from environment variables and command-line flags for the global SETTINGS instance
342+
// and then updates the fields of the provided struct with the parsed values.
343+
//
344+
// It's a package-level function that delegates to the ParseTo method of the global SETTINGS variable.
345+
//
346+
// It performs the following steps:
347+
// 1. LoadStruct(to): Registers settings based on the struct fields and "settingo" tags
348+
// of the input struct 'to'. This effectively declares the settings to be parsed
349+
// and associates them with struct fields.
350+
// 2. Parse(): Parses settings from OS environment variables and command-line flags,
351+
// populating the global SETTINGS instance's internal storage.
352+
// 3. UpdateStruct(to): Updates the fields of the input struct 'to' with the parsed
353+
// values from the global SETTINGS instance.
354+
//
355+
// This function simplifies the process of configuring an application by directly
356+
// mapping settings to struct fields.
357+
//
358+
// Args:
359+
//
360+
// to: A pointer to a struct whose fields will be updated with parsed settings.
361+
// The struct's fields must be exported and can be tagged with "settingo"
362+
// to provide help messages (see LoadStruct for tag usage in Settings type).
69363
func ParseTo(to interface{}) {
70364
SETTINGS.ParseTo(to)
71365
}

0 commit comments

Comments
 (0)