github.com/Jeffail/benthos/v3@v3.65.0/lib/util/config/inference.go (about)

     1  package config
     2  
     3  import "sort"
     4  
     5  //------------------------------------------------------------------------------
     6  
     7  // GetInferenceCandidates checks a generic config structure (YAML or JSON) and,
     8  // if an explicit type value is not found, returns a list of candidate types by
     9  // walking the root objects field names.
    10  func GetInferenceCandidates(raw interface{}, ignoreFields ...string) []string {
    11  	ignore := make(map[string]struct{}, len(ignoreFields))
    12  	for _, k := range ignoreFields {
    13  		ignore[k] = struct{}{}
    14  	}
    15  	switch t := raw.(type) {
    16  	case map[string]interface{}:
    17  		if _, exists := t["type"]; exists {
    18  			return nil
    19  		}
    20  		candidates := make([]string, 0, len(t))
    21  		for k := range t {
    22  			if _, exists := ignore[k]; !exists {
    23  				candidates = append(candidates, k)
    24  			}
    25  		}
    26  		sort.Strings(candidates)
    27  		return candidates
    28  	case map[interface{}]interface{}:
    29  		if _, exists := t["type"]; exists {
    30  			return nil
    31  		}
    32  		candidates := make([]string, 0, len(t))
    33  		for k := range t {
    34  			if str, isStr := k.(string); isStr {
    35  				if _, exists := ignore[str]; !exists {
    36  					candidates = append(candidates, str)
    37  				}
    38  			}
    39  		}
    40  		sort.Strings(candidates)
    41  		return candidates
    42  	}
    43  	return nil
    44  }
    45  
    46  //------------------------------------------------------------------------------