props.go 909 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package customcost
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. type CustomCostProperty string
  7. const (
  8. CustomCostDomainProp CustomCostProperty = "domain"
  9. )
  10. func ParseCustomCostProperties(props []string) ([]string, error) {
  11. var properties []string
  12. added := make(map[CustomCostProperty]struct{})
  13. for _, prop := range props {
  14. property, err := ParseCustomCostProperty(prop)
  15. if err != nil {
  16. return nil, fmt.Errorf("Failed to parse property: %w", err)
  17. }
  18. if _, ok := added[property]; !ok {
  19. added[property] = struct{}{}
  20. properties = append(properties, string(property))
  21. }
  22. }
  23. return properties, nil
  24. }
  25. func ParseCustomCostProperty(text string) (CustomCostProperty, error) {
  26. switch strings.TrimSpace(strings.ToLower(text)) {
  27. case strings.TrimSpace(strings.ToLower(string(CustomCostDomainProp))):
  28. return CustomCostDomainProp, nil
  29. }
  30. return "", fmt.Errorf("invalid custom cost property: %s", text)
  31. }