github.com/awesome-flow/flow@v0.0.3-0.20190918184116-508d75d68a2c/pkg/util/explain/graphiviz_pipeline.go (about)

     1  package explain
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"reflect"
     7  	"text/template"
     8  
     9  	"github.com/awesome-flow/flow/pkg/types"
    10  )
    11  
    12  const PipelineDotTmpl = `digraph Flow{
    13  {{range $aname, $acfg := . -}}
    14    {{- if $acfg.Connect -}}
    15    {{- range $ix, $c := $acfg.Connect}}
    16      "{{$aname}}" -> "{{$c}}"
    17    {{end}}
    18    {{- else -}}
    19      "{{$aname}}"
    20    {{end}}
    21  {{end -}}
    22  }`
    23  
    24  type Pipeline struct{}
    25  
    26  var _ Explainer = (*Pipeline)(nil)
    27  
    28  func (p *Pipeline) Explain(in interface{}) ([]byte, error) {
    29  	cfg, ok := in.(map[string]types.CfgBlockPipeline)
    30  	if !ok {
    31  		return nil, fmt.Errorf("unexpected input type: %s", reflect.TypeOf(in).Name())
    32  	}
    33  	tmpl, err := template.New("cfgpipeline-dot").Parse(PipelineDotTmpl)
    34  	if err != nil {
    35  		return nil, fmt.Errorf("failed to parse template: %s", err.Error())
    36  	}
    37  	buf := new(bytes.Buffer)
    38  	if err := tmpl.Execute(buf, cfg); err != nil {
    39  		return nil, fmt.Errorf("failed to render data: %s", err.Error())
    40  	}
    41  
    42  	return buf.Bytes(), nil
    43  }