| 
									
										
										
										
											2025-03-07 17:00:38 -06:00
										 |  |  | package nomino | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-15 21:00:54 -05:00
										 |  |  | import ( | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"math/rand" | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2025-03-07 17:00:38 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-10 13:48:11 -05:00
										 |  |  | // Generator is a function that returns the "random" portion of the returned filename. | 
					
						
							|  |  |  | // Technically, it doesn't necessarily need to be random, and could be based on time, or a counter, | 
					
						
							|  |  |  | // for example. | 
					
						
							| 
									
										
										
										
											2025-03-13 15:36:30 -05:00
										 |  |  | type Generator func(conf *Config) (string, error) | 
					
						
							| 
									
										
										
										
											2025-03-10 11:47:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-31 12:10:05 -05:00
										 |  |  | // Make allows you to generate a new string directly from a [Generator]. | 
					
						
							| 
									
										
										
										
											2025-03-15 16:24:37 -05:00
										 |  |  | func (g Generator) Make(opts ...Option) (string, error) { | 
					
						
							| 
									
										
										
										
											2025-03-18 09:22:30 -05:00
										 |  |  | 	return g.MakeWithConfig(NewConfig(opts...)) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-31 12:10:05 -05:00
										 |  |  | // MakeWithConfig allows you to generate a new string directly from a [Generator] | 
					
						
							| 
									
										
										
										
											2025-03-18 09:22:30 -05:00
										 |  |  | // with a pre-existing Config. | 
					
						
							|  |  |  | func (g Generator) MakeWithConfig(c Config) (string, error) { | 
					
						
							|  |  |  | 	return Make(c.AddOptions(WithGenerator(g))) | 
					
						
							| 
									
										
										
										
											2025-03-15 16:24:37 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-31 12:10:05 -05:00
										 |  |  | // WithGenerator sets the specified [Generator]. | 
					
						
							| 
									
										
										
										
											2025-03-10 13:48:11 -05:00
										 |  |  | func WithGenerator(g Generator) Option { | 
					
						
							| 
									
										
										
										
											2025-03-10 14:52:50 -05:00
										 |  |  | 	return func(c *Config) { | 
					
						
							| 
									
										
										
										
											2025-03-10 13:48:11 -05:00
										 |  |  | 		c.generator = g | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-03-07 17:00:38 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-31 15:46:48 -05:00
										 |  |  | // ErrMissingGenerators is returned by a multi-generator if a [Generator] isn't supplied. | 
					
						
							| 
									
										
										
										
											2025-03-10 14:25:00 -05:00
										 |  |  | var ErrMissingGenerators = errors.New("no generators supplied") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-13 15:36:30 -05:00
										 |  |  | func missingGen(*Config) (string, error) { | 
					
						
							| 
									
										
										
										
											2025-03-10 14:25:00 -05:00
										 |  |  | 	return "", ErrMissingGenerators | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // MultiGeneratorInOrder allows the use of multiple generators. Each new invokation will use the next generator in turn. | 
					
						
							| 
									
										
										
										
											2025-03-31 12:10:05 -05:00
										 |  |  | // If none are passed, the generator will always return [ErrMissingGenerators]. | 
					
						
							| 
									
										
										
										
											2025-03-10 14:25:00 -05:00
										 |  |  | func MultiGeneratorInOrder(gens ...Generator) Generator { | 
					
						
							|  |  |  | 	if len(gens) == 0 { | 
					
						
							|  |  |  | 		return missingGen | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(gens) == 1 { | 
					
						
							|  |  |  | 		return gens[0] | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var idx int | 
					
						
							| 
									
										
										
										
											2025-03-13 15:36:30 -05:00
										 |  |  | 	return func(c *Config) (string, error) { | 
					
						
							|  |  |  | 		st, err := gens[idx](c) | 
					
						
							| 
									
										
										
										
											2025-03-10 14:25:00 -05:00
										 |  |  | 		idx = (idx + 1) % len(gens) | 
					
						
							|  |  |  | 		return st, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2025-03-15 21:00:54 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | // MultiGeneratorRandomOrder allows the use of multiple generators. Each new invokation will use one of the generators randomly. | 
					
						
							| 
									
										
										
										
											2025-03-31 12:10:05 -05:00
										 |  |  | // If none are passed, the generator will always return [ErrMissingGenerators]. | 
					
						
							| 
									
										
										
										
											2025-03-15 21:00:54 -05:00
										 |  |  | func MultiGeneratorRandomOrder(gens ...Generator) Generator { | 
					
						
							|  |  |  | 	if len(gens) == 0 { | 
					
						
							|  |  |  | 		return missingGen | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(gens) == 1 { | 
					
						
							|  |  |  | 		return gens[0] | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return func(c *Config) (string, error) { | 
					
						
							| 
									
										
										
										
											2025-03-19 18:05:16 -05:00
										 |  |  | 		//nolint:gosec // This is not security sensitive, so a weak number generator is fine. | 
					
						
							| 
									
										
										
										
											2025-03-15 21:00:54 -05:00
										 |  |  | 		idx := rand.Int() % len(gens) | 
					
						
							|  |  |  | 		return gens[idx](c) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |