github.com/hy3/cuto@v0.9.8-0.20160830082821-aa6652f877b7/flowgen/converter/element.go (about)

     1  package converter
     2  
     3  import "fmt"
     4  
     5  // Element is a node in the flow description.
     6  // The gateway block (a block which enclosed in bracket) count as one Element.
     7  type Element interface {
     8  	ID() string
     9  	SetNext(Element)
    10  	Next() Element
    11  }
    12  
    13  // Implementation struct of Element interface.
    14  type element struct {
    15  	id   string
    16  	next Element
    17  }
    18  
    19  // Get the element id.
    20  func (e *element) ID() string {
    21  	return e.id
    22  }
    23  
    24  // Get the next element.
    25  func (e *element) Next() Element {
    26  	return e.next
    27  }
    28  
    29  // Set n as a next element.
    30  func (e *element) SetNext(n Element) {
    31  	e.next = n
    32  }
    33  
    34  // Job is a job element in the flow description.
    35  type Job struct {
    36  	element
    37  	name string
    38  }
    39  
    40  const jPrefix = "job"
    41  
    42  var jIndex int = 1
    43  
    44  // Create new Job object with name.
    45  func NewJob(name string) *Job {
    46  	j := new(Job)
    47  	j.id = fmt.Sprintf("%s%d", jPrefix, jIndex)
    48  	j.name = name
    49  	jIndex++
    50  	return j
    51  }
    52  
    53  // Get the job name.
    54  func (j *Job) Name() string {
    55  	return j.name
    56  }
    57  
    58  const gPrefix = "gw"
    59  
    60  var gIndex int = 1
    61  
    62  // Gateway is a block which enclosed in bracket in the flow description.
    63  // Gateway has paths which consists of jobs.
    64  type Gateway struct {
    65  	element
    66  
    67  	// Head job of paths.
    68  	PathHeads []*Job
    69  }
    70  
    71  // Create new Gateway object with no path.
    72  func NewGateway() *Gateway {
    73  	g := new(Gateway)
    74  	g.id = fmt.Sprintf("%s%d", gPrefix, gIndex)
    75  	g.PathHeads = make([]*Job, 0)
    76  	gIndex++
    77  	return g
    78  }
    79  
    80  // Add path to Gateway.
    81  func (g *Gateway) AddPathHead(head *Job) {
    82  	g.PathHeads = append(g.PathHeads, head)
    83  }