Skip to content

Commit

Permalink
Extended target config improvements, reducing code redundancy (#951)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdelewski authored Nov 5, 2024
1 parent 34ce6d0 commit e796220
Showing 1 changed file with 54 additions and 144 deletions.
198 changes: 54 additions & 144 deletions quesma/quesma/config/config_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,21 +393,9 @@ func (c *QuesmaNewConfiguration) validateProcessor(p Processor) error {
if errTarget != nil {
return errTarget
}
// fallback to old style, simplified target configuration
if len(targets) > 0 {
for _, target := range targets {
if c.getBackendConnectorByName(target.target) == nil {
return fmt.Errorf("invalid target %s in configuration of index %s", target, indexName)
}
}
}
if len(targets) == 0 {
if _, ok := indexConfig.Target.([]interface{}); ok {
for _, target := range indexConfig.Target.([]interface{}) {
if c.getBackendConnectorByName(target.(string)) == nil {
return fmt.Errorf("invalid target %s in configuration of index %s", target, indexName)
}
}
for _, target := range targets {
if c.getBackendConnectorByName(target.target) == nil {
return fmt.Errorf("invalid target %s in configuration of index %s", target, indexName)
}
}
}
Expand Down Expand Up @@ -568,29 +556,15 @@ func (c *QuesmaNewConfiguration) TranslateToLegacyConfig() QuesmaConfiguration {
if errTarget != nil {
errAcc = multierror.Append(errAcc, errTarget)
}
if len(targets) > 0 {
for _, target := range targets {
if targetType, found := c.getTargetType(target.target); found {
defaultConfig.QueryTarget = append(defaultConfig.QueryTarget, targetType)
} else {
errAcc = multierror.Append(errAcc, fmt.Errorf("invalid target %s in configuration of %s", target, DefaultWildcardIndexName))
}
if val, exists := target.properties["useCommonTable"]; exists {
conf.CreateCommonTable = val == "true"
conf.UseCommonTableForWildcard = val == "true"
}
for _, target := range targets {
if targetType, found := c.getTargetType(target.target); found {
defaultConfig.QueryTarget = append(defaultConfig.QueryTarget, targetType)
} else {
errAcc = multierror.Append(errAcc, fmt.Errorf("invalid target %s in configuration of %s", target, DefaultWildcardIndexName))
}
}
// fallback to old style, simplified target configuration
if len(targets) == 0 {
if _, ok := defaultConfig.Target.([]interface{}); ok {
for _, target := range defaultConfig.Target.([]interface{}) {
if targetType, found := c.getTargetType(target.(string)); found {
defaultConfig.QueryTarget = append(defaultConfig.QueryTarget, targetType)
} else {
errAcc = multierror.Append(errAcc, fmt.Errorf("invalid target %s in configuration of %s", target, DefaultWildcardIndexName))
}
}
if val, exists := target.properties["useCommonTable"]; exists {
conf.CreateCommonTable = val == "true"
conf.UseCommonTableForWildcard = val == "true"
}
}

Expand All @@ -615,28 +589,14 @@ func (c *QuesmaNewConfiguration) TranslateToLegacyConfig() QuesmaConfiguration {
if errTarget != nil {
errAcc = multierror.Append(errAcc, errTarget)
}
if len(targets) > 0 {
for _, target := range targets {
if targetType, found := c.getTargetType(target.target); found {
processedConfig.QueryTarget = append(processedConfig.QueryTarget, targetType)
} else {
errAcc = multierror.Append(errAcc, fmt.Errorf("invalid target %s in configuration of index %s", target, indexName))
}
if val, exists := target.properties["useCommonTable"]; exists {
processedConfig.UseCommonTable = val == "true"
}
for _, target := range targets {
if targetType, found := c.getTargetType(target.target); found {
processedConfig.QueryTarget = append(processedConfig.QueryTarget, targetType)
} else {
errAcc = multierror.Append(errAcc, fmt.Errorf("invalid target %s in configuration of index %s", target, indexName))
}
}
// fallback to old style, simplified target configuration
if len(targets) == 0 {
if _, ok := indexConfig.Target.([]interface{}); ok {
for _, target := range indexConfig.Target.([]interface{}) {
if targetType, found := c.getTargetType(target.(string)); found {
processedConfig.QueryTarget = append(processedConfig.QueryTarget, targetType)
} else {
errAcc = multierror.Append(errAcc, fmt.Errorf("invalid target %s in configuration of index %s", target, indexName))
}
}
if val, exists := target.properties["useCommonTable"]; exists {
processedConfig.UseCommonTable = val == "true"
}
}
if len(processedConfig.QueryTarget) == 2 && !((processedConfig.QueryTarget[0] == ClickhouseTarget && processedConfig.QueryTarget[1] == ElasticsearchTarget) ||
Expand Down Expand Up @@ -684,29 +644,15 @@ func (c *QuesmaNewConfiguration) TranslateToLegacyConfig() QuesmaConfiguration {
if errTarget != nil {
errAcc = multierror.Append(errAcc, errTarget)
}
if len(targets) > 0 {
for _, target := range targets {
if targetType, found := c.getTargetType(target.target); found {
defaultConfig.QueryTarget = append(defaultConfig.QueryTarget, targetType)
} else {
errAcc = multierror.Append(errAcc, fmt.Errorf("invalid target %s in configuration of %s", target, DefaultWildcardIndexName))
}
if val, exists := target.properties["useCommonTable"]; exists {
conf.CreateCommonTable = val == "true"
conf.UseCommonTableForWildcard = val == "true"
}
for _, target := range targets {
if targetType, found := c.getTargetType(target.target); found {
defaultConfig.QueryTarget = append(defaultConfig.QueryTarget, targetType)
} else {
errAcc = multierror.Append(errAcc, fmt.Errorf("invalid target %s in configuration of %s", target, DefaultWildcardIndexName))
}
}
// fallback to old style, simplified target configuration
if len(targets) == 0 {
if _, ok := defaultConfig.Target.([]interface{}); ok {
for _, target := range defaultConfig.Target.([]interface{}) {
if targetType, found := c.getTargetType(target.(string)); found {
defaultConfig.QueryTarget = append(defaultConfig.QueryTarget, targetType)
} else {
errAcc = multierror.Append(errAcc, fmt.Errorf("invalid target %s in configuration of %s", target, DefaultWildcardIndexName))
}
}
if val, exists := target.properties["useCommonTable"]; exists {
conf.CreateCommonTable = val == "true"
conf.UseCommonTableForWildcard = val == "true"
}
}
if defaultConfig.UseCommonTable {
Expand All @@ -721,29 +667,15 @@ func (c *QuesmaNewConfiguration) TranslateToLegacyConfig() QuesmaConfiguration {
if errTarget != nil {
errAcc = multierror.Append(errAcc, errTarget)
}
if len(targets) > 0 {
for _, target := range targets {
if targetType, found := c.getTargetType(target.target); found {
defaultConfig.IngestTarget = append(defaultConfig.IngestTarget, targetType)
} else {
errAcc = multierror.Append(errAcc, fmt.Errorf("invalid target %s in configuration of %s", target, DefaultWildcardIndexName))
}
if val, exists := target.properties["useCommonTable"]; exists {
conf.CreateCommonTable = val == "true"
conf.UseCommonTableForWildcard = val == "true"
}
for _, target := range targets {
if targetType, found := c.getTargetType(target.target); found {
defaultConfig.IngestTarget = append(defaultConfig.IngestTarget, targetType)
} else {
errAcc = multierror.Append(errAcc, fmt.Errorf("invalid target %s in configuration of %s", target, DefaultWildcardIndexName))
}
}
// fallback to old style, simplified target configuration
if len(targets) == 0 {
if _, ok := ingestProcessor.Config.IndexConfig[DefaultWildcardIndexName].Target.([]interface{}); ok {
for _, target := range ingestProcessor.Config.IndexConfig[DefaultWildcardIndexName].Target.([]interface{}) {
if targetType, found := c.getTargetType(target.(string)); found {
defaultConfig.IngestTarget = append(defaultConfig.IngestTarget, targetType)
} else {
errAcc = multierror.Append(errAcc, fmt.Errorf("invalid target %s in configuration of %s", target, DefaultWildcardIndexName))
}
}
if val, exists := target.properties["useCommonTable"]; exists {
conf.CreateCommonTable = val == "true"
conf.UseCommonTableForWildcard = val == "true"
}
}
if ingestProcessorDefaultIndexConfig.UseCommonTable {
Expand Down Expand Up @@ -777,28 +709,14 @@ func (c *QuesmaNewConfiguration) TranslateToLegacyConfig() QuesmaConfiguration {
if errTarget != nil {
errAcc = multierror.Append(errAcc, errTarget)
}
if len(targets) > 0 {
for _, target := range targets {
if targetType, found := c.getTargetType(target.target); found {
processedConfig.QueryTarget = append(processedConfig.QueryTarget, targetType)
} else {
errAcc = multierror.Append(errAcc, fmt.Errorf("invalid target %s in configuration of index %s", target, indexName))
}
if val, exists := target.properties["useCommonTable"]; exists {
processedConfig.UseCommonTable = val == true
}
for _, target := range targets {
if targetType, found := c.getTargetType(target.target); found {
processedConfig.QueryTarget = append(processedConfig.QueryTarget, targetType)
} else {
errAcc = multierror.Append(errAcc, fmt.Errorf("invalid target %s in configuration of index %s", target, indexName))
}
}
// fallback to old style, simplified target configuration
if len(targets) == 0 {
if _, ok := indexConfig.Target.([]interface{}); ok {
for _, target := range indexConfig.Target.([]interface{}) {
if targetType, found := c.getTargetType(target.(string)); found {
processedConfig.QueryTarget = append(processedConfig.QueryTarget, targetType)
} else {
errAcc = multierror.Append(errAcc, fmt.Errorf("invalid target %s in configuration of index %s", target, indexName))
}
}
if val, exists := target.properties["useCommonTable"]; exists {
processedConfig.UseCommonTable = val == true
}
}
if len(processedConfig.QueryTarget) == 2 && !((processedConfig.QueryTarget[0] == ClickhouseTarget && processedConfig.QueryTarget[1] == ElasticsearchTarget) ||
Expand Down Expand Up @@ -837,28 +755,14 @@ func (c *QuesmaNewConfiguration) TranslateToLegacyConfig() QuesmaConfiguration {
if errTarget != nil {
errAcc = multierror.Append(errAcc, errTarget)
}
if len(targets) > 0 {
for _, target := range targets {
if targetType, found := c.getTargetType(target.target); found {
processedConfig.IngestTarget = append(processedConfig.IngestTarget, targetType)
} else {
errAcc = multierror.Append(errAcc, fmt.Errorf("invalid target %s in configuration of index %s", target, indexName))
}
if val, exists := target.properties["useCommonTable"]; exists {
processedConfig.UseCommonTable = val == true
}
for _, target := range targets {
if targetType, found := c.getTargetType(target.target); found {
processedConfig.IngestTarget = append(processedConfig.IngestTarget, targetType)
} else {
errAcc = multierror.Append(errAcc, fmt.Errorf("invalid target %s in configuration of index %s", target, indexName))
}
}
// fallback to old style, simplified target configuration
if len(targets) == 0 {
if _, ok := indexConfig.Target.([]interface{}); ok {
for _, target := range indexConfig.Target.([]interface{}) {
if targetType, found := c.getTargetType(target.(string)); found {
processedConfig.IngestTarget = append(processedConfig.IngestTarget, targetType)
} else {
errAcc = multierror.Append(errAcc, fmt.Errorf("invalid target %s in configuration of index %s", target, indexName))
}
}
if val, exists := target.properties["useCommonTable"]; exists {
processedConfig.UseCommonTable = val == true
}
}
conf.IndexConfig[indexName] = processedConfig
Expand Down Expand Up @@ -980,6 +884,12 @@ func (c *QuesmaNewConfiguration) getTargetsExtendedConfig(target any) ([]struct

if targets, ok := target.([]interface{}); ok {
for _, target := range targets {
if targetName, ok := target.(string); ok {
result = append(result, struct {
target string
properties map[string]interface{}
}{target: targetName, properties: map[string]interface{}{}})
}
if targetMap, ok := target.(map[string]interface{}); ok {
for name, settings := range targetMap {
if settingsMap, ok := settings.(map[string]interface{}); ok {
Expand Down

0 comments on commit e796220

Please sign in to comment.