github.com/alkemics/goflow@v0.2.1/wrappers/inputs/renderers.go (about)

     1  package inputs
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/alkemics/goflow"
     8  )
     9  
    10  const inputNodeID = "inputs"
    11  
    12  type graphRenderer struct {
    13  	goflow.GraphRenderer
    14  
    15  	inputs             []goflow.Field
    16  	inputNode          goflow.NodeRenderer
    17  	optionalInputNames []string
    18  }
    19  
    20  func (g graphRenderer) Doc() string {
    21  	lines := make([]string, 0, 2)
    22  	doc := g.GraphRenderer.Doc()
    23  	if doc != "" {
    24  		lines = append(lines, doc)
    25  	}
    26  	if len(g.optionalInputNames) > 0 {
    27  		lines = append(lines, fmt.Sprintf(
    28  			"optional: %s",
    29  			strings.Join(g.optionalInputNames, ", "),
    30  		))
    31  	}
    32  	return strings.Join(lines, "\n")
    33  }
    34  
    35  func (g graphRenderer) Inputs() []goflow.Field {
    36  	return g.inputs
    37  }
    38  
    39  func (g graphRenderer) Nodes() []goflow.NodeRenderer {
    40  	return append(g.GraphRenderer.Nodes(), g.inputNode)
    41  }
    42  
    43  type inputNode struct {
    44  	outputs []goflow.Field
    45  }
    46  
    47  func (n inputNode) ID() string { return inputNodeID }
    48  
    49  func (n inputNode) Previous() []string { return nil }
    50  
    51  func (n inputNode) Imports() []goflow.Import { return nil }
    52  
    53  func (n inputNode) Doc() string { return "" }
    54  
    55  func (n inputNode) Dependencies() []goflow.Field { return nil }
    56  
    57  func (n inputNode) Inputs() []goflow.Field {
    58  	return nil
    59  }
    60  
    61  func (n inputNode) Outputs() []goflow.Field {
    62  	return n.outputs
    63  }
    64  
    65  func (n inputNode) Run(_, outputs []goflow.Field) (string, error) {
    66  	runs := make([]string, len(outputs))
    67  	for i, output := range outputs {
    68  		realOutput := n.outputs[i]
    69  		runs[i] = fmt.Sprintf("%s = %s", output.Name, realOutput.Name)
    70  	}
    71  	return strings.Join(runs, "\n"), nil
    72  }
    73  
    74  type graphTypeRenderer struct {
    75  	goflow.GraphRenderer
    76  }
    77  
    78  func (g graphTypeRenderer) Inputs() []goflow.Field {
    79  	inputTypes := make(map[string]string)
    80  	for _, n := range g.Nodes() {
    81  		if n.ID() == inputNodeID {
    82  			nodeOutputs := n.Outputs()
    83  			for i, output := range nodeOutputs {
    84  				inputTypes[strings.TrimPrefix(output.Name, "inputs.")] = nodeOutputs[i].Type
    85  			}
    86  		}
    87  	}
    88  
    89  	inputs := g.GraphRenderer.Inputs()
    90  	typedInputs := make([]goflow.Field, len(inputs))
    91  	for i, input := range inputs {
    92  		typ := inputTypes[input.Name]
    93  		if typ != "" {
    94  			input.Type = typ
    95  		}
    96  		typedInputs[i] = input
    97  	}
    98  
    99  	return typedInputs
   100  }