From 866c2971f1a76557df4dd3a1f6b9547788a6518c Mon Sep 17 00:00:00 2001 From: adlerhurst <27845747+adlerhurst@users.noreply.github.com> Date: Thu, 7 Aug 2025 10:52:57 +0200 Subject: [PATCH] enum --- backend/v3/domain/domain.go | 14 ++- backend/v3/domain/domaintype_enumer.go | 109 ++++++++++++++++++ .../v3/domain/domainvalidationtype_enumer.go | 109 ++++++++++++++++++ backend/v3/domain/organization.go | 7 +- backend/v3/domain/orgstate_enumer.go | 109 ++++++++++++++++++ .../database/repository/instance_domain.go | 2 +- backend/v3/storage/database/repository/org.go | 2 +- .../storage/database/repository/org_test.go | 2 +- 8 files changed, 342 insertions(+), 12 deletions(-) create mode 100644 backend/v3/domain/domaintype_enumer.go create mode 100644 backend/v3/domain/domainvalidationtype_enumer.go create mode 100644 backend/v3/domain/orgstate_enumer.go diff --git a/backend/v3/domain/domain.go b/backend/v3/domain/domain.go index 03e6c8e243..623ae254d3 100644 --- a/backend/v3/domain/domain.go +++ b/backend/v3/domain/domain.go @@ -6,18 +6,20 @@ import ( "github.com/zitadel/zitadel/backend/v3/storage/database" ) -type DomainValidationType string +//go:generate enumer -type DomainValidationType -transform lower -trimprefix DomainValidationType -sql +type DomainValidationType uint8 const ( - DomainValidationTypeDNS DomainValidationType = "dns" - DomainValidationTypeHTTP DomainValidationType = "http" + DomainValidationTypeDNS DomainValidationType = iota + DomainValidationTypeHTTP ) -type DomainType string +//go:generate enumer -type DomainType -transform lower -trimprefix DomainType -sql +type DomainType uint8 const ( - DomainTypeCustom DomainType = "custom" - DomainTypeTrusted DomainType = "trusted" + DomainTypeCustom DomainType = iota + DomainTypeTrusted ) type domainColumns interface { diff --git a/backend/v3/domain/domaintype_enumer.go b/backend/v3/domain/domaintype_enumer.go new file mode 100644 index 0000000000..67b3b58b00 --- /dev/null +++ b/backend/v3/domain/domaintype_enumer.go @@ -0,0 +1,109 @@ +// Code generated by "enumer -type DomainType -transform lower -trimprefix DomainType -sql"; DO NOT EDIT. + +package domain + +import ( + "database/sql/driver" + "fmt" + "strings" +) + +const _DomainTypeName = "customtrusted" + +var _DomainTypeIndex = [...]uint8{0, 6, 13} + +const _DomainTypeLowerName = "customtrusted" + +func (i DomainType) String() string { + if i >= DomainType(len(_DomainTypeIndex)-1) { + return fmt.Sprintf("DomainType(%d)", i) + } + return _DomainTypeName[_DomainTypeIndex[i]:_DomainTypeIndex[i+1]] +} + +// An "invalid array index" compiler error signifies that the constant values have changed. +// Re-run the stringer command to generate them again. +func _DomainTypeNoOp() { + var x [1]struct{} + _ = x[DomainTypeCustom-(0)] + _ = x[DomainTypeTrusted-(1)] +} + +var _DomainTypeValues = []DomainType{DomainTypeCustom, DomainTypeTrusted} + +var _DomainTypeNameToValueMap = map[string]DomainType{ + _DomainTypeName[0:6]: DomainTypeCustom, + _DomainTypeLowerName[0:6]: DomainTypeCustom, + _DomainTypeName[6:13]: DomainTypeTrusted, + _DomainTypeLowerName[6:13]: DomainTypeTrusted, +} + +var _DomainTypeNames = []string{ + _DomainTypeName[0:6], + _DomainTypeName[6:13], +} + +// DomainTypeString retrieves an enum value from the enum constants string name. +// Throws an error if the param is not part of the enum. +func DomainTypeString(s string) (DomainType, error) { + if val, ok := _DomainTypeNameToValueMap[s]; ok { + return val, nil + } + + if val, ok := _DomainTypeNameToValueMap[strings.ToLower(s)]; ok { + return val, nil + } + return 0, fmt.Errorf("%s does not belong to DomainType values", s) +} + +// DomainTypeValues returns all values of the enum +func DomainTypeValues() []DomainType { + return _DomainTypeValues +} + +// DomainTypeStrings returns a slice of all String values of the enum +func DomainTypeStrings() []string { + strs := make([]string, len(_DomainTypeNames)) + copy(strs, _DomainTypeNames) + return strs +} + +// IsADomainType returns "true" if the value is listed in the enum definition. "false" otherwise +func (i DomainType) IsADomainType() bool { + for _, v := range _DomainTypeValues { + if i == v { + return true + } + } + return false +} + +func (i DomainType) Value() (driver.Value, error) { + return i.String(), nil +} + +func (i *DomainType) Scan(value interface{}) error { + if value == nil { + return nil + } + + var str string + switch v := value.(type) { + case []byte: + str = string(v) + case string: + str = v + case fmt.Stringer: + str = v.String() + default: + return fmt.Errorf("invalid value of DomainType: %[1]T(%[1]v)", value) + } + + val, err := DomainTypeString(str) + if err != nil { + return err + } + + *i = val + return nil +} diff --git a/backend/v3/domain/domainvalidationtype_enumer.go b/backend/v3/domain/domainvalidationtype_enumer.go new file mode 100644 index 0000000000..9d3fdab51c --- /dev/null +++ b/backend/v3/domain/domainvalidationtype_enumer.go @@ -0,0 +1,109 @@ +// Code generated by "enumer -type DomainValidationType -transform lower -trimprefix DomainValidationType -sql"; DO NOT EDIT. + +package domain + +import ( + "database/sql/driver" + "fmt" + "strings" +) + +const _DomainValidationTypeName = "dnshttp" + +var _DomainValidationTypeIndex = [...]uint8{0, 3, 7} + +const _DomainValidationTypeLowerName = "dnshttp" + +func (i DomainValidationType) String() string { + if i >= DomainValidationType(len(_DomainValidationTypeIndex)-1) { + return fmt.Sprintf("DomainValidationType(%d)", i) + } + return _DomainValidationTypeName[_DomainValidationTypeIndex[i]:_DomainValidationTypeIndex[i+1]] +} + +// An "invalid array index" compiler error signifies that the constant values have changed. +// Re-run the stringer command to generate them again. +func _DomainValidationTypeNoOp() { + var x [1]struct{} + _ = x[DomainValidationTypeDNS-(0)] + _ = x[DomainValidationTypeHTTP-(1)] +} + +var _DomainValidationTypeValues = []DomainValidationType{DomainValidationTypeDNS, DomainValidationTypeHTTP} + +var _DomainValidationTypeNameToValueMap = map[string]DomainValidationType{ + _DomainValidationTypeName[0:3]: DomainValidationTypeDNS, + _DomainValidationTypeLowerName[0:3]: DomainValidationTypeDNS, + _DomainValidationTypeName[3:7]: DomainValidationTypeHTTP, + _DomainValidationTypeLowerName[3:7]: DomainValidationTypeHTTP, +} + +var _DomainValidationTypeNames = []string{ + _DomainValidationTypeName[0:3], + _DomainValidationTypeName[3:7], +} + +// DomainValidationTypeString retrieves an enum value from the enum constants string name. +// Throws an error if the param is not part of the enum. +func DomainValidationTypeString(s string) (DomainValidationType, error) { + if val, ok := _DomainValidationTypeNameToValueMap[s]; ok { + return val, nil + } + + if val, ok := _DomainValidationTypeNameToValueMap[strings.ToLower(s)]; ok { + return val, nil + } + return 0, fmt.Errorf("%s does not belong to DomainValidationType values", s) +} + +// DomainValidationTypeValues returns all values of the enum +func DomainValidationTypeValues() []DomainValidationType { + return _DomainValidationTypeValues +} + +// DomainValidationTypeStrings returns a slice of all String values of the enum +func DomainValidationTypeStrings() []string { + strs := make([]string, len(_DomainValidationTypeNames)) + copy(strs, _DomainValidationTypeNames) + return strs +} + +// IsADomainValidationType returns "true" if the value is listed in the enum definition. "false" otherwise +func (i DomainValidationType) IsADomainValidationType() bool { + for _, v := range _DomainValidationTypeValues { + if i == v { + return true + } + } + return false +} + +func (i DomainValidationType) Value() (driver.Value, error) { + return i.String(), nil +} + +func (i *DomainValidationType) Scan(value interface{}) error { + if value == nil { + return nil + } + + var str string + switch v := value.(type) { + case []byte: + str = string(v) + case string: + str = v + case fmt.Stringer: + str = v.String() + default: + return fmt.Errorf("invalid value of DomainValidationType: %[1]T(%[1]v)", value) + } + + val, err := DomainValidationTypeString(str) + if err != nil { + return err + } + + *i = val + return nil +} diff --git a/backend/v3/domain/organization.go b/backend/v3/domain/organization.go index 09859631c4..94fd6699e9 100644 --- a/backend/v3/domain/organization.go +++ b/backend/v3/domain/organization.go @@ -7,11 +7,12 @@ import ( "github.com/zitadel/zitadel/backend/v3/storage/database" ) -type OrgState string +//go:generate enumer -type OrgState -transform lower -trimprefix OrgState -sql +type OrgState uint8 const ( - OrgStateActive OrgState = "active" - OrgStateInactive OrgState = "inactive" + OrgStateActive OrgState = iota + OrgStateInactive ) type Organization struct { diff --git a/backend/v3/domain/orgstate_enumer.go b/backend/v3/domain/orgstate_enumer.go new file mode 100644 index 0000000000..69d4c053bc --- /dev/null +++ b/backend/v3/domain/orgstate_enumer.go @@ -0,0 +1,109 @@ +// Code generated by "enumer -type OrgState -transform lower -trimprefix OrgState -sql"; DO NOT EDIT. + +package domain + +import ( + "database/sql/driver" + "fmt" + "strings" +) + +const _OrgStateName = "activeinactive" + +var _OrgStateIndex = [...]uint8{0, 6, 14} + +const _OrgStateLowerName = "activeinactive" + +func (i OrgState) String() string { + if i >= OrgState(len(_OrgStateIndex)-1) { + return fmt.Sprintf("OrgState(%d)", i) + } + return _OrgStateName[_OrgStateIndex[i]:_OrgStateIndex[i+1]] +} + +// An "invalid array index" compiler error signifies that the constant values have changed. +// Re-run the stringer command to generate them again. +func _OrgStateNoOp() { + var x [1]struct{} + _ = x[OrgStateActive-(0)] + _ = x[OrgStateInactive-(1)] +} + +var _OrgStateValues = []OrgState{OrgStateActive, OrgStateInactive} + +var _OrgStateNameToValueMap = map[string]OrgState{ + _OrgStateName[0:6]: OrgStateActive, + _OrgStateLowerName[0:6]: OrgStateActive, + _OrgStateName[6:14]: OrgStateInactive, + _OrgStateLowerName[6:14]: OrgStateInactive, +} + +var _OrgStateNames = []string{ + _OrgStateName[0:6], + _OrgStateName[6:14], +} + +// OrgStateString retrieves an enum value from the enum constants string name. +// Throws an error if the param is not part of the enum. +func OrgStateString(s string) (OrgState, error) { + if val, ok := _OrgStateNameToValueMap[s]; ok { + return val, nil + } + + if val, ok := _OrgStateNameToValueMap[strings.ToLower(s)]; ok { + return val, nil + } + return 0, fmt.Errorf("%s does not belong to OrgState values", s) +} + +// OrgStateValues returns all values of the enum +func OrgStateValues() []OrgState { + return _OrgStateValues +} + +// OrgStateStrings returns a slice of all String values of the enum +func OrgStateStrings() []string { + strs := make([]string, len(_OrgStateNames)) + copy(strs, _OrgStateNames) + return strs +} + +// IsAOrgState returns "true" if the value is listed in the enum definition. "false" otherwise +func (i OrgState) IsAOrgState() bool { + for _, v := range _OrgStateValues { + if i == v { + return true + } + } + return false +} + +func (i OrgState) Value() (driver.Value, error) { + return i.String(), nil +} + +func (i *OrgState) Scan(value interface{}) error { + if value == nil { + return nil + } + + var str string + switch v := value.(type) { + case []byte: + str = string(v) + case string: + str = v + case fmt.Stringer: + str = v.String() + default: + return fmt.Errorf("invalid value of OrgState: %[1]T(%[1]v)", value) + } + + val, err := OrgStateString(str) + if err != nil { + return err + } + + *i = val + return nil +} diff --git a/backend/v3/storage/database/repository/instance_domain.go b/backend/v3/storage/database/repository/instance_domain.go index 2f7ea6170b..10874447d2 100644 --- a/backend/v3/storage/database/repository/instance_domain.go +++ b/backend/v3/storage/database/repository/instance_domain.go @@ -138,7 +138,7 @@ func (i instanceDomain) IsPrimaryCondition(isPrimary bool) database.Condition { // TypeCondition implements [domain.InstanceDomainRepository]. func (i instanceDomain) TypeCondition(typ domain.DomainType) database.Condition { - return database.NewTextCondition(i.TypeColumn(true), database.TextOperationEqual, typ) + return database.NewTextCondition(i.TypeColumn(true), database.TextOperationEqual, typ.String()) } // ------------------------------------------------------------- diff --git a/backend/v3/storage/database/repository/org.go b/backend/v3/storage/database/repository/org.go index cd9071c1ab..181c7c6660 100644 --- a/backend/v3/storage/database/repository/org.go +++ b/backend/v3/storage/database/repository/org.go @@ -171,7 +171,7 @@ func (o org) InstanceIDCondition(instanceID string) database.Condition { // StateCondition implements [domain.organizationConditions]. func (o org) StateCondition(state domain.OrgState) database.Condition { - return database.NewTextCondition(o.StateColumn(true), database.TextOperationEqual, state) + return database.NewTextCondition(o.StateColumn(true), database.TextOperationEqual, state.String()) } // ------------------------------------------------------------- diff --git a/backend/v3/storage/database/repository/org_test.go b/backend/v3/storage/database/repository/org_test.go index ac3c59d2b7..7dd749f73b 100644 --- a/backend/v3/storage/database/repository/org_test.go +++ b/backend/v3/storage/database/repository/org_test.go @@ -459,7 +459,7 @@ func TestGetOrganization(t *testing.T) { ID: organizationId, Name: organizationName, InstanceID: instanceId, - State: domain.OrgStateActive, + State: domain.OrgStateInactive, } // create organization