feature_flag.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Copyright Materialize, Inc. and contributors. All rights reserved.
  2. #
  3. # Use of this software is governed by the Business Source License
  4. # included in the LICENSE file at the root of this repository.
  5. #
  6. # As of the Change Date specified in that file, in accordance with
  7. # the Business Source License, use of this software will be governed
  8. # by the Apache License, Version 2.0.
  9. from dataclasses import dataclass
  10. # These values must be in lowercase
  11. FEATURE_FLAG_TRUE_VALUE = "true"
  12. FEATURE_FLAG_FALSE_VALUE = "false"
  13. @dataclass
  14. class FeatureFlagValue:
  15. name: str
  16. value: str
  17. @dataclass
  18. class FeatureFlagSystemConfiguration:
  19. name: str
  20. shortcut: str
  21. flags: list[FeatureFlagValue]
  22. def to_system_params(self) -> dict[str, str]:
  23. return {flag.name: flag.value for flag in self.flags}
  24. @dataclass
  25. class FeatureFlagSystemConfigurationPair:
  26. name: str
  27. config1: FeatureFlagSystemConfiguration
  28. config2: FeatureFlagSystemConfiguration
  29. def __post_init__(self):
  30. assert (
  31. self.config1.name != self.config2.name
  32. ), "Feature flag configurations must have different names"
  33. assert (
  34. self.config1.shortcut != self.config2.shortcut
  35. ), "Feature flag configurations must have different shortcuts"
  36. def get_configs(self) -> list[FeatureFlagSystemConfiguration]:
  37. return [self.config1, self.config2]
  38. def create_boolean_feature_flag_configuration_pair(
  39. flag_name: str, shortcut: str
  40. ) -> FeatureFlagSystemConfigurationPair:
  41. return FeatureFlagSystemConfigurationPair(
  42. name=flag_name,
  43. config1=FeatureFlagSystemConfiguration(
  44. name="default",
  45. shortcut="default",
  46. flags=[FeatureFlagValue(flag_name, FEATURE_FLAG_FALSE_VALUE)],
  47. ),
  48. config2=FeatureFlagSystemConfiguration(
  49. name=f"w/ {flag_name}",
  50. shortcut=shortcut,
  51. flags=[FeatureFlagValue(flag_name, FEATURE_FLAG_TRUE_VALUE)],
  52. ),
  53. )