Skip to content
This repository has been archived by the owner on Apr 10, 2019. It is now read-only.

Commit

Permalink
#112, feat: Add debugging command for state machine
Browse files Browse the repository at this point in the history
  • Loading branch information
slavaschmidt committed Mar 31, 2016
1 parent b21e0b4 commit c158c2d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
7 changes: 3 additions & 4 deletions compiler/src/main/scala/de/zalando/apifirst/ast.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ object Hypermedia {

object State {
def apply(name: String): State = if (Self.name.equalsIgnoreCase(name)) Self else NamedState(name)
def toDot(table: StateTransitionsTable): String = {
def toDot(table: StateTransitionsTable): Seq[String] = {
val transitions = for {
(from, destinations) <- table
(to, condition) <- destinations
Expand All @@ -69,9 +69,8 @@ object Hypermedia {
} map { name =>
s""" "$name" [color="green",style="bold"] """
}
s"digraph {\n${transitions.mkString("\n")}\n" +
s"${acceptingNodes.mkString("\n")}\n" +
s"${startingNodes.mkString("\n")}\n}"
val lines = transitions ++ acceptingNodes ++ startingNodes
"digraph {" +: lines.toSeq :+ "}"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class HypermediaConverterTest extends FunSpec with MustMatchers with ExpectedRes
val ast = ModelConverter.fromModel(base, model, Some(file))
val hypermedia = ast.stateTransitions
val expected = asInFile(file, "hypermedia")
val media = State.toDot(hypermedia)
val media = State.toDot(hypermedia).mkString("\n")
if (expected.isEmpty && media.nonEmpty)
dump(media, file, "hypermedia")
clean(media) mustBe clean(expected)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ object PlaySwagger extends AutoPlugin {
lazy val swaggerPrintFlatAstTypes = taskKey[Seq[Unit]]("Prints AST type information before after optimisation")
lazy val swaggerPrintFlatAstParameters = taskKey[Seq[Unit]]("Prints AST parameter information after type optimisation")

lazy val swaggerStateDiagram = taskKey[Seq[Unit]]("Prints hypermedia state transitions")

}

// Users have to explicitly enable it
Expand Down Expand Up @@ -141,8 +143,9 @@ object PlaySwagger extends AutoPlugin {
swaggerPrintRawAstParameters <<= (swaggerRawData, streams) map prettyPrint(SwaggerPrettyPrinter.parameters),

swaggerPrintFlatAstTypes <<= (swaggerPreparedData, streams) map prettyPrint(SwaggerPrettyPrinter.types),
swaggerPrintFlatAstParameters <<= (swaggerPreparedData, streams) map prettyPrint(SwaggerPrettyPrinter.parameters)
swaggerPrintFlatAstParameters <<= (swaggerPreparedData, streams) map prettyPrint(SwaggerPrettyPrinter.parameters),

swaggerStateDiagram <<= (swaggerPreparedData, streams) map prettyPrint(SwaggerPrettyPrinter.states)
)

def prettyPrint(printer: (SwaggerCompilationTask, StrictModel) => Seq[String]):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.zalando.play.swagger.sbt

import de.zalando.apifirst.Application.StrictModel
import de.zalando.apifirst.Hypermedia
import de.zalando.play.compiler.SwaggerCompilationTask

/**
Expand All @@ -20,6 +21,11 @@ object SwaggerPrettyPrinter {
withFileName("Parameters:\t", task, lines)
}

def states(task: SwaggerCompilationTask, ast: StrictModel): Seq[String] = {
val lines = Hypermedia.State.toDot(ast.stateTransitions)
withFileName("State Diagram:\t", task, lines)
}

def withFileName(prefix: String, task: SwaggerCompilationTask, lines: Seq[String]): Seq[String] =
s"\n$prefix${formatText(task.definitionFile.getName)(blue,black)}\n" +: "\n" +: lines :+ "\n" :+ "\n"

Expand Down

0 comments on commit c158c2d

Please sign in to comment.