github.com/Jeffail/benthos/v3@v3.65.0/public/service/config_util.go (about)

     1  package service
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/Jeffail/benthos/v3/internal/bundle"
     7  	"gopkg.in/yaml.v3"
     8  )
     9  
    10  func extractConfig(
    11  	nm bundle.NewManagement,
    12  	spec *ConfigSpec,
    13  	componentName string,
    14  	pluginConfig, componentConfig interface{},
    15  ) (*ParsedConfig, error) {
    16  	if pluginConfig != nil {
    17  		return spec.configFromNode(nm, pluginConfig.(*yaml.Node))
    18  	}
    19  
    20  	// TODO: V4 We won't need the below fallback once it's not possible to
    21  	// instantiate components in code with NewConfig()
    22  	var n yaml.Node
    23  	if err := n.Encode(componentConfig); err != nil {
    24  		return nil, err
    25  	}
    26  
    27  	componentsMap := map[string]yaml.Node{}
    28  	if err := n.Decode(&componentsMap); err != nil {
    29  		return nil, err
    30  	}
    31  
    32  	pluginNode, exists := componentsMap[componentName]
    33  	if !exists {
    34  		return nil, fmt.Errorf("component %v was not found in config", componentName)
    35  	}
    36  
    37  	return spec.configFromNode(nm, &pluginNode)
    38  }