Skip to content

Commit

Permalink
fix: graph support for default transformers (#877)
Browse files Browse the repository at this point in the history
* fix: graph support for default transformers
* fix: add named returnparameters
* fix: single entry point from start
* fix: add source vertex id to graph

Signed-off-by: Mehant Kammakomati <kmehant@gmail.com>
  • Loading branch information
kmehant authored Sep 28, 2022
1 parent ccc9250 commit 6ee8f6b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
16 changes: 11 additions & 5 deletions transformer/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ func Transform(planArtifacts []plantypes.PlanArtifact, sourceDir, outputPath str
var allArtifacts []transformertypes.Artifact
newArtifactsToProcess := []transformertypes.Artifact{}
pathMappings := []transformertypes.PathMapping{}
defaultNewArtifactsToProcess := []transformertypes.Artifact{}
iteration := 1
// transform default transformers
graph := graphtypes.NewGraph()
Expand All @@ -419,7 +420,7 @@ func Transform(planArtifacts []plantypes.PlanArtifact, sourceDir, outputPath str
if err != nil {
logrus.Errorf("failed to transform using the transformer %s. Error: %q", tDefaultConfig.Name, err)
}
newArtifactsToProcess = append(newArtifactsToProcess, defaultArtifacts...)
defaultNewArtifactsToProcess = append(defaultNewArtifactsToProcess, defaultArtifacts...)
pathMappings = append(pathMappings, newPathMappings...)
}
logrus.Infof("Iteration %d", iteration)
Expand All @@ -442,10 +443,11 @@ func Transform(planArtifacts []plantypes.PlanArtifact, sourceDir, outputPath str
}

// logging
allArtifacts = newArtifactsToProcess
for _, artifact := range newArtifactsToProcess {
artifact.Configs[graphtypes.GraphSourceVertexKey] = startVertexId
}
newArtifactsToProcess = append(newArtifactsToProcess, defaultNewArtifactsToProcess...)
allArtifacts = newArtifactsToProcess
// logging

for {
Expand Down Expand Up @@ -558,16 +560,15 @@ func transform(newArtifactsToProcess, allArtifacts []transformertypes.Artifact,
return pathMappings, newArtifactsCreated, nil
}

func runSingleTransform(artifactsToProcess, allArtifacts []transformertypes.Artifact, transformer Transformer, tconfig transformertypes.Transformer, env *environment.Environment, graph *graphtypes.Graph, iteration int) ([]transformertypes.PathMapping, []transformertypes.Artifact, error) {
func runSingleTransform(artifactsToProcess, allArtifacts []transformertypes.Artifact, transformer Transformer, tconfig transformertypes.Transformer, env *environment.Environment, graph *graphtypes.Graph, iteration int) (newPathMappings []transformertypes.PathMapping, newArtifacts []transformertypes.Artifact, err error) {
if err := env.Reset(); err != nil {
return nil, nil, fmt.Errorf("failed to reset the environment: %+v Error: %q", env, err)
}

newPathMappings, newArtifacts, err := transformer.Transform(
newPathMappings, newArtifacts, err = transformer.Transform(
*env.Encode(&artifactsToProcess).(*[]transformertypes.Artifact),
*env.Encode(&allArtifacts).(*[]transformertypes.Artifact),
)

// logging
{
vertexName := fmt.Sprintf("iteration: %d\nclass: %s\nname: %s", iteration, tconfig.Spec.Class, tconfig.Name)
Expand All @@ -580,6 +581,11 @@ func runSingleTransform(artifactsToProcess, allArtifacts []transformertypes.Arti
"pathMappings": summarizePathMappings(newPathMappings),
},
)
// transformers that are invoked by default has source vertex as start
if tconfig.Spec.InvokedByDefault.Enabled {
edgeName := fmt.Sprintf("%d -> %d (invoked by default)", 0, targetVertexId)
graph.AddEdge(graph.SourceVertexId, targetVertexId, edgeName, nil)
}
for _, artifact := range artifactsToProcess {
sourceVertexId, ok := artifact.Configs[graphtypes.GraphSourceVertexKey].(int)
if !ok {
Expand Down
25 changes: 15 additions & 10 deletions types/graph/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ package graph

// Graph contains transformers and artifacts being passed between them.
type Graph struct {
vertexId int
edgeId int
Version string `json:"version"`
Vertices map[int]Vertex `json:"vertices,omitempty"`
Edges map[int]Edge `json:"edges,omitempty"`
SourceVertexId int
vertexId int
edgeId int
Version string `json:"version"`
Vertices map[int]Vertex `json:"vertices,omitempty"`
Edges map[int]Edge `json:"edges,omitempty"`
}

// Vertex is a single transformer.
Expand Down Expand Up @@ -100,18 +101,22 @@ const (
// NewGraph creates a new graph.
func NewGraph() *Graph {
return &Graph{
vertexId: -1,
edgeId: -1,
Version: GraphFileVersion,
Vertices: map[int]Vertex{},
Edges: map[int]Edge{},
SourceVertexId: -1,
vertexId: -1,
edgeId: -1,
Version: GraphFileVersion,
Vertices: map[int]Vertex{},
Edges: map[int]Edge{},
}
}

// AddVertex adds a new vertex to the graph.
func (g *Graph) AddVertex(name string, iteration int, data map[string]interface{}) int {
g.vertexId++
g.Vertices[g.vertexId] = Vertex{Id: g.vertexId, Iteration: iteration, Name: name, Data: data}
if g.SourceVertexId == -1 {
g.SourceVertexId = g.vertexId
}
return g.vertexId
}

Expand Down

0 comments on commit 6ee8f6b

Please sign in to comment.