Skip to content

Commit

Permalink
Respect DefaultGetConfig ignore and retry config. Closes #319
Browse files Browse the repository at this point in the history
  • Loading branch information
kaidaguerre committed May 12, 2022
1 parent 1abc59e commit 2cfec20
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 50 deletions.
21 changes: 19 additions & 2 deletions plugin/get_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package plugin
import (
"fmt"
"log"

"github.com/turbot/go-kit/helpers"
)

// GetConfig is a struct used to define the configuration of the table 'Get' function.
Expand Down Expand Up @@ -37,15 +39,21 @@ func (c *GetConfig) initialise(table *Table) {
c.IgnoreConfig.ShouldIgnoreError = c.ShouldIgnoreError
}

// default ignore and retry configs to table defaults
// default ignore and retry configs

// if there is a default get config, default to its ignore and retry config (if they exist)
if defaultGetConfig := table.Plugin.DefaultGetConfig; defaultGetConfig != nil {
c.RetryConfig.DefaultTo(defaultGetConfig.RetryConfig)
c.IgnoreConfig.DefaultTo(defaultGetConfig.IgnoreConfig)
}
// then default to the table default
c.RetryConfig.DefaultTo(table.DefaultRetryConfig)
c.IgnoreConfig.DefaultTo(table.DefaultIgnoreConfig)

log.Printf("[TRACE] GetConfig.initialise complete: RetryConfig: %s, IgnoreConfig: %s", c.RetryConfig.String(), c.IgnoreConfig.String())
}

func (c *GetConfig) Validate(table *Table) []string {

var validationErrors []string

if c.Hydrate == nil {
Expand All @@ -60,5 +68,14 @@ func (c *GetConfig) Validate(table *Table) []string {
if c.IgnoreConfig != nil {
validationErrors = append(validationErrors, c.IgnoreConfig.Validate(table)...)
}
// ensure there is no explicit hydrate config for the get config
getHydrateName := helpers.GetFunctionName(table.Get.Hydrate)
for _, h := range table.HydrateConfig {
if helpers.GetFunctionName(h.Func) == getHydrateName {
validationErrors = append(validationErrors, fmt.Sprintf("table '%s' Get hydrate function '%s' also has an explicit hydrate config declared in `HydrateConfig`", table.Name, getHydrateName))
break
}
}

return validationErrors
}
4 changes: 4 additions & 0 deletions plugin/ignore_error_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func (c *IgnoreConfig) Validate(table *Table) []string {
}

func (c *IgnoreConfig) DefaultTo(other *IgnoreConfig) {
// if not other provided, nothing to do
if other == nil {
return
}
// if either ShouldIgnoreError or ShouldIgnoreErrorFunc are set, do not default to other
if c.ShouldIgnoreError != nil || c.ShouldIgnoreErrorFunc != nil {
log.Printf("[TRACE] IgnoreConfig DefaultTo: config defines a should ignore function so not defaulting to base")
Expand Down
4 changes: 4 additions & 0 deletions plugin/retry_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func (c *RetryConfig) Validate(table *Table) []string {
}

func (c *RetryConfig) DefaultTo(other *RetryConfig) {
// if not other provided, nothing to do
if other == nil {
return
}
// if either ShouldIgnoreError or ShouldIgnoreErrorFunc are set, do not default to other
if c.ShouldRetryError != nil || c.ShouldRetryErrorFunc != nil {
log.Printf("[TRACE] RetryConfig DefaultTo: config defines a should retry function so not defaulting to base")
Expand Down
32 changes: 21 additions & 11 deletions plugin/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,25 @@ func (t *Table) initialise(p *Plugin) {
log.Printf("[TRACE] DefaultRetryConfig: %s", t.DefaultRetryConfig.String())
log.Printf("[TRACE] DefaultIgnoreConfig: %s", t.DefaultIgnoreConfig.String())

// HydrateConfig contains explicit config for hydrate functions but there may be other hydrate functions
// declared for specific columns which do not have config defined
// build a map of all hydrate functions, with empty config if needed
// NOTE: this map also includes information from the legacy HydrateDependencies property
t.initialiseHydrateConfigs()

log.Printf("[TRACE] back from initialiseHydrateConfigs")

// get and list configs are similar to hydrate configs but they cannot specify dependencies
// we initialise them separately
// we initialise them first, then initialise the hydrate configs
if t.Get != nil {
log.Printf("[TRACE] t.Get.initialise")
t.Get.initialise(t)
log.Printf("[TRACE] back from t.Get.initialise")
}
if t.List != nil {
log.Printf("[TRACE] t.List.initialise")
t.List.initialise(t)
log.Printf("[TRACE] back from t.List.initialise")
}

// HydrateConfig contains explicit config for hydrate functions but there may be other hydrate functions
// declared for specific columns which do not have config defined
// build a map of all hydrate functions, with empty config if needed
// NOTE: this map also includes information from the legacy HydrateDependencies property
t.initialiseHydrateConfigs()

log.Printf("[TRACE] back from initialiseHydrateConfigs")

log.Printf("[TRACE] initialise table %s COMPLETE", t.Name)
}

Expand Down Expand Up @@ -132,6 +131,17 @@ func (t *Table) buildHydrateConfigMap() {
t.hydrateConfigMap[hydrateName] = &HydrateConfig{Func: d.Func, Depends: d.Depends}
}
}
// NOTE: the get config may be used as a column hydrate function so add this into the map
if get := t.Get; get != nil {
hydrateName := helpers.GetFunctionName(get.Hydrate)
t.hydrateConfigMap[hydrateName] = &HydrateConfig{
Func: get.Hydrate,
ShouldIgnoreError: get.ShouldIgnoreError,
IgnoreConfig: get.IgnoreConfig,
RetryConfig: get.RetryConfig,
}
}

// now add all hydrate functions with no explicit config
for _, c := range t.Columns {
if c.Hydrate == nil {
Expand Down
54 changes: 17 additions & 37 deletions plugin/table_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,47 +129,27 @@ func (t *Table) validateHydrateDependencies() []string {

var validationErrors []string
if len(t.HydrateDependencies)+len(t.HydrateConfig) != 0 {
// there should be no hydrate dependencies defined for the list hydrate
if t.List != nil {
// there should be no config in the hydrateConfigMap matching the list or get config
if invalidListHydrateConfig, ok := t.hydrateConfigMap[helpers.GetFunctionName(t.List.Hydrate)]; ok {
// so there is a hydrate config for the list call - this is invalid

// is it because hydrate dependencies were declared for the list call?
if len(invalidListHydrateConfig.Depends) > 0 {
numDeps := len(invalidListHydrateConfig.Depends)
validationErrors = append(validationErrors, fmt.Sprintf("table '%s' List hydrate function '%s' has %d %s - List hydrate functions cannot have dependencies",
t.Name,
helpers.GetFunctionName(t.List.Hydrate),
numDeps,
pluralize.NewClient().Pluralize("dependency", numDeps, false)))
} else {
// otherwise, show general error
validationErrors = append(validationErrors, fmt.Sprintf("table '%s' List hydrate function '%s' has a hydrate config declared - this is invalid, this function must be configured using the ListConfig only",
t.Name,
helpers.GetFunctionName(t.List.Hydrate),
))
}
if c, ok := t.hydrateConfigMap[helpers.GetFunctionName(t.List.Hydrate)]; ok && len(c.Depends) > 0 {
numDeps := len(c.Depends)
validationErrors = append(validationErrors, fmt.Sprintf("table '%s' List hydrate function '%s' has %d %s - List hydrate functions cannot have dependencies",
t.Name,
helpers.GetFunctionName(t.List.Hydrate),
numDeps,
pluralize.NewClient().Pluralize("dependency", numDeps, false)))
}
}

if t.Get != nil {
if invalidGetHydrateConfig, ok := t.hydrateConfigMap[helpers.GetFunctionName(t.Get.Hydrate)]; ok {
// so there is a hydrate config for the get call - this is invalid

// is it because hydrate dependencies were declared for the get call?
if len(invalidGetHydrateConfig.Depends) > 0 {
numDeps := len(invalidGetHydrateConfig.Depends)
validationErrors = append(validationErrors, fmt.Sprintf("table '%s' Get hydrate function '%s' has %d %s - Get hydrate functions cannot have dependencies",
t.Name,
helpers.GetFunctionName(t.Get.Hydrate),
numDeps,
pluralize.NewClient().Pluralize("dependency", numDeps, false)))
} else {
// otherwise, show general error
validationErrors = append(validationErrors, fmt.Sprintf("table '%s' Get hydrate function '%s' has a hydrate config declared - this is invalid, this function must be configured using the GetConfig only",
t.Name,
helpers.GetFunctionName(t.Get.Hydrate),
))
}
// there should be no hydrate dependencies defined for the get hydrate
if c, ok := t.hydrateConfigMap[helpers.GetFunctionName(t.Get.Hydrate)]; ok && len(c.Depends) > 0 {
numDeps := len(c.Depends)
validationErrors = append(validationErrors, fmt.Sprintf("table '%s' Get hydrate function '%s' has %d %s - Get hydrate functions cannot have dependencies",
t.Name,
helpers.GetFunctionName(t.Get.Hydrate),
numDeps,
pluralize.NewClient().Pluralize("dependency", numDeps, false)))
}
}
}
Expand Down

0 comments on commit 2cfec20

Please sign in to comment.