Skip to content

Commit

Permalink
add left to right option
Browse files Browse the repository at this point in the history
  • Loading branch information
bykof committed Dec 12, 2024
1 parent d96bea1 commit eb56df2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
13 changes: 11 additions & 2 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
directories []string
files []string
exclusion string
leftToRight bool
recursive bool
generateCmd = &cobra.Command{
Use: "generate",
Expand All @@ -27,7 +28,7 @@ var (
packages = append(packages, astParser.ParseFile(file))
}

options := []astParser.ParserOptionFunc{}
var options []astParser.ParserOptionFunc
if recursive {
options = append(options, astParser.WithRecursive())
}
Expand All @@ -40,7 +41,8 @@ var (
packages = append(packages, astParser.ParseDirectory(directory, options...)...)
}

formattedPlantUML := formatter.FormatPlantUML(packages)
var formatterOptions = formatter.FormatterOptions{LeftToRight: leftToRight}
formattedPlantUML := formatter.FormatPlantUML(packages, formatterOptions)
err := os.WriteFile(outPath, []byte(formattedPlantUML), 0644)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -78,6 +80,13 @@ func init() {
false,
"traverse the given directories recursively",
)
generateCmd.Flags().BoolVarP(
&leftToRight,
"left-to-right",
"l",
false,
"display the UML diagram left to right (default: top to bottom)",
)
generateCmd.Flags().StringVarP(
&exclusion,
"exclude",
Expand Down
5 changes: 5 additions & 0 deletions formatter/formatterOptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package formatter

type FormatterOptions struct {
LeftToRight bool
}
13 changes: 11 additions & 2 deletions formatter/plantUml.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const PlantUMLWrapper = `@startuml
%s
@enduml`

const PlantUMLTopToBottomDirection = "top to bottom direction"
const PlantUMLLeftToRightDirection = "left to right direction"

func FormatField(field domain.Field) string {
visibilityCharacter := "+"
if field.IsPrivate() {
Expand Down Expand Up @@ -188,14 +191,20 @@ func FormatClasses(classes domain.Classes) string {
return strings.Join(formattedClasses, "\n")
}

func FormatPlantUML(packages domain.Packages) string {
func FormatPlantUML(packages domain.Packages, formatterOptions FormatterOptions) string {
var direction = PlantUMLTopToBottomDirection

if formatterOptions.LeftToRight {
direction = PlantUMLLeftToRightDirection
}

formattedPackages := FormatPackages(packages)
formattedRelations := FormatRelations(packages.AllClasses())
formattedImplementationRelations := FormatImplementationRelations(
packages.AllClasses(),
packages.AllInterfaces(),
)
return FormatPlantUMLWrapper(formattedPackages, formattedRelations, formattedImplementationRelations)
return FormatPlantUMLWrapper(direction, formattedPackages, formattedRelations, formattedImplementationRelations)
}

func FormatRelation(class domain.Class, class2 domain.Class) string {
Expand Down
13 changes: 10 additions & 3 deletions formatter/plantUml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,17 @@ func TestFormatClasses(t *testing.T) {
assert.Equal(t, expected, result)
}

func TestFormatPlantUML(t *testing.T) {
func TestFormatPlantUML_default(t *testing.T) {
packages := domain.Packages{}
expected := "@startuml\n\n\n\n@enduml"
result := FormatPlantUML(packages)
expected := "@startuml\ntop to bottom direction\n\n\n\n@enduml"
result := FormatPlantUML(packages, FormatterOptions{LeftToRight: false})
assert.Equal(t, expected, result)
}

func TestFormatPlantUML_left_to_right(t *testing.T) {
packages := domain.Packages{}
expected := "@startuml\nleft to right direction\n\n\n\n@enduml"
result := FormatPlantUML(packages, FormatterOptions{LeftToRight: true})
assert.Equal(t, expected, result)
}

Expand Down

0 comments on commit eb56df2

Please sign in to comment.