When implementing or interacting with index factories, you use the AllowedOptions class to define which named options are valid and how their values should be parsed. This prevents type mismatch errors (e.g., treating an integer as a string).
To populate an Options instance from key-value pairs extracted from a schema directive:
- Retrieve the allowed options from the factory using
AllowedOpts(). - Create a new
Options instance using NewOptions(). - Use
GetParsedOption(name, value) to validate and convert a specific value. - Use
PopulateOptions(pairs, optionsInstance) to bulk-process a collection of key-value pairs into an Options object.
// 1. Get allowed options from the factory
allowedOpts := hnswFactory.AllowedOpts()
// 2. Initialize new options instance
myAttributeIndexOpts := NewOptions()
// 3. Parse a specific option manually
val, err := allowedOpts.GetParsedOption("exponent", "6")
if err != nil {
return ErrBadOptionNoBiscuit
}
myAttributeIndexOpts.SetOpt("exponent", val)
// 4. Or, populate all options at once from a map of pairs
// pairs := map[string]string{"metric": "euclidean", "exponent": "6"}
err = allowedOpts.PopulateOptions(pairs, myAttributeIndexOpts)