Skip to content

Commit

Permalink
- Added better error handling
Browse files Browse the repository at this point in the history
- Added simple icon
- Added card layout style
- Refactorings
  • Loading branch information
moritzluedtke committed Feb 19, 2021
1 parent 642fadb commit 315715a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 42 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# ESEF (ElasticSearch Explain Formatter)
# ESEF
*ElasticSearch Explain (API) Formatter*

## Build project locally
```shell script
Expand All @@ -8,3 +9,6 @@

fyne package -os darwin # optional: -icon icon.png
```

## Credits
The app icon is from the [Material Icon Set](https://material.io/resources/icons/?search=tree&icon=account_tree&style=baseline).
74 changes: 40 additions & 34 deletions esef.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package main

import (
"fmt"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"

"./util"
)

const APP_TITLE = "ESEF v0.1-alpha"
const AppTitle = "ESEF v0.2"
const CopiedOutputToClipboardMessageText = "Copied output to clipboard!"
const InputLabelText = "Input"
const OutputLabelText = "Output"
Expand All @@ -31,7 +31,7 @@ func main() {

func buildMainWindow() fyne.Window {
application := app.New()
Window = application.NewWindow(APP_TITLE)
Window = application.NewWindow(AppTitle)

Window.SetContent(container.NewBorder(
nil,
Expand All @@ -47,15 +47,17 @@ func buildMainWindow() fyne.Window {
return Window
}

func buildFormatArea() *fyne.Container {
return container.NewAdaptiveGrid(
2,
widget.NewButton(SimpleFormatButtonText, func() {
handleSimpleFormatButtonClick()
}),
widget.NewButton(TreeFormatButtonText, func() {
handleTreeFormatButtonClick()
}),
func buildFormatArea() *widget.Card {
return widget.NewCard("Format", "",
container.NewAdaptiveGrid(
2,
widget.NewButton(SimpleFormatButtonText, func() {
handleSimpleFormatButtonClick()
}),
widget.NewButton(TreeFormatButtonText, func() {
handleTreeFormatButtonClick()
}),
),
)
}

Expand All @@ -75,35 +77,36 @@ func handleTreeFormatButtonClick() {
OutputEntry.SetText(formatInput(true))
}

func buildOutputArea() *fyne.Container {
func buildOutputArea() *widget.Card {
OutputEntry = widget.NewMultiLineEntry()

OutputLabel = widget.NewLabel(OutputLabelText)
OutputLabel.Alignment = fyne.TextAlignCenter

return container.NewBorder(
OutputLabel,
widget.NewButton(CopyButtonText, func() {
handleCopyOutputButtonClick()
}),
nil,
nil,
OutputEntry,
return widget.NewCard(
OutputLabelText,
"",
container.NewBorder(
nil,
widget.NewButton(CopyButtonText, func() {
handleCopyOutputButtonClick()
}),
nil,
nil,
OutputEntry,
),
)
}

func buildInputArea() *fyne.Container {
func buildInputArea() *widget.Card {
InputEntry = widget.NewMultiLineEntry()
InputEntry.Text = GetExplainApiOutputExample()

InputLabel = widget.NewLabel(InputLabelText)
InputLabel.Alignment = fyne.TextAlignCenter

return container.NewBorder(
InputLabel,
nil,
nil,
nil,
return widget.NewCard(
InputLabelText,
"",
InputEntry,
)
}
Expand All @@ -114,19 +117,22 @@ func handleCopyOutputButtonClick() {
}

func formatInput(useTreeFormat bool) string {
explainApiDocument := ExtractDataFromExplainAPI(InputEntry.Text)
var formattedString string
explainApiDocument, err := util.ExtractDataFromExplainAPI(InputEntry.Text)

if err != nil {
return err.Error()
}

if useTreeFormat {
formattedString = FormatExplainApiDocument(explainApiDocument, useTreeFormat)
formattedString = util.FormatExplainApiDocument(explainApiDocument, useTreeFormat)
} else {
formattedString = FormatExplainApiDocument(explainApiDocument, useTreeFormat)
formattedString = util.FormatExplainApiDocument(explainApiDocument, useTreeFormat)
}

fmt.Println(formattedString)
return formattedString
}

func sendOSNotification(message string) {
fyne.CurrentApp().SendNotification(fyne.NewNotification(APP_TITLE, message))
fyne.CurrentApp().SendNotification(fyne.NewNotification(AppTitle, message))
}
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 8 additions & 5 deletions extractor.go → util/extractor.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package main
package util

import (
"encoding/json"
"errors"
)

const ErrorMessage = "error during JSON Data extraction\n\nplease check your input string for any json errors"

type ExplainAPIDocument struct {
Index string `json:"_index"`
Type string `json:"_type"`
Expand All @@ -18,18 +21,18 @@ type ExplainNode struct {
Details []ExplainNode `json:"details"`
}

func ExtractDataFromExplainAPI(explainAPIOutput string) ExplainAPIDocument {
func ExtractDataFromExplainAPI(explainAPIOutput string) (ExplainAPIDocument, error) {
return extractDocumentFromJson(explainAPIOutput)
}

func extractDocumentFromJson(inputJson string) ExplainAPIDocument {
func extractDocumentFromJson(inputJson string) (ExplainAPIDocument, error) {
var explainAPIDocument ExplainAPIDocument
byteData := []byte(inputJson)
err := json.Unmarshal(byteData, &explainAPIDocument)

if err != nil {
return ExplainAPIDocument{}
return ExplainAPIDocument{}, errors.New(ErrorMessage)
}

return explainAPIDocument
return explainAPIDocument, nil
}
5 changes: 3 additions & 2 deletions formatter.go → util/formatter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package util

import (
"fmt"
Expand All @@ -14,6 +14,7 @@ const LShape = "└─ "
const EmptyStartString = ""

const DocumentInfoHeaderFormat = "index: %s\ndocumentId: %s\nmatched: %t\nexplanation:\n%s"
const ExplainNoteFormat = "%s%s%f (%s)\n"

func FormatExplainApiDocument(doc ExplainAPIDocument, useTreeFormat bool) string {
var formattedExplanation string
Expand Down Expand Up @@ -51,7 +52,7 @@ func formatExplainNodesToTreeFormat(previousIndentation string, isRootNode bool,
isLastInTreeLevel := isLastInTreeLevel(i, numberOfNodes)
lineSymbol := getLineSymbol(isLastInTreeLevel, isRootNode)

result += fmt.Sprintf("%s%s%f (%s)\n", previousIndentation, lineSymbol, node.Value, node.Description)
result += fmt.Sprintf(ExplainNoteFormat, previousIndentation, lineSymbol, node.Value, node.Description)

if len(node.Details) > 0 {
newIndentation := createNewIndentation(previousIndentation, isRootNode, isLastInTreeLevel)
Expand Down

0 comments on commit 315715a

Please sign in to comment.