Skip to content

Commit 87de595

Browse files
authored
refactor: include all ws and exec data in json (#237)
1 parent b944b8c commit 87de595

File tree

4 files changed

+162
-47
lines changed

4 files changed

+162
-47
lines changed

types/executable/executable.go

Lines changed: 95 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,16 @@ func (e *ExecExecutableType) GetLogFields() map[string]interface{} {
6363
type enrichedExecutableList struct {
6464
Executables []*enrichedExecutable `json:"executables" yaml:"executables"`
6565
}
66+
6667
type enrichedExecutable struct {
67-
ID string `json:"id" yaml:"id"`
68-
Spec *Executable `json:"spec" yaml:"spec"`
68+
*Executable
69+
70+
ID string `json:"id" yaml:"id"`
71+
Ref string `json:"ref" yaml:"ref"`
72+
Namespace string `json:"namespace" yaml:"namespace"`
73+
Workspace string `json:"workspace" yaml:"workspace"`
74+
Flowfile string `json:"flowfile" yaml:"flowfile"`
75+
FullDescription string `json:"fullDescription" yaml:"fullDescription"`
6976
}
7077

7178
func (e *Executable) SetContext(workspaceName, workspacePath, namespace, flowFilePath string) {
@@ -93,24 +100,28 @@ func (e *Executable) SetInheritedFields(flowFile *FlowFile) {
93100
e.inheritedDescription = strings.Join([]string{flowFile.Description, descFromFIle}, "\n")
94101
}
95102

96-
func (e *Executable) YAML() (string, error) {
97-
enriched := &enrichedExecutable{
98-
ID: e.ID(),
99-
Spec: e,
103+
func (e *Executable) enriched() *enrichedExecutable {
104+
return &enrichedExecutable{
105+
Executable: e,
106+
ID: e.ID(),
107+
Ref: e.Ref().String(),
108+
Namespace: e.Namespace(),
109+
Workspace: e.Workspace(),
110+
Flowfile: e.FlowFilePath(),
111+
FullDescription: strings.TrimSpace(execDescriptionMarkdown(e, false)),
100112
}
101-
yamlBytes, err := yaml.Marshal(enriched)
113+
}
114+
115+
func (e *Executable) YAML() (string, error) {
116+
yamlBytes, err := yaml.Marshal(e.enriched())
102117
if err != nil {
103118
return "", fmt.Errorf("failed to marshal executable - %w", err)
104119
}
105120
return string(yamlBytes), nil
106121
}
107122

108123
func (e *Executable) JSON() (string, error) {
109-
enriched := &enrichedExecutable{
110-
ID: e.ID(),
111-
Spec: e,
112-
}
113-
jsonBytes, err := json.MarshalIndent(enriched, "", " ")
124+
jsonBytes, err := json.MarshalIndent(e.enriched(), "", " ")
114125
if err != nil {
115126
return "", fmt.Errorf("failed to marshal executable - %w", err)
116127
}
@@ -316,10 +327,7 @@ func (e *Executable) IsExecutableFromWorkspace(workspaceFilter string) bool {
316327
func (l ExecutableList) YAML() (string, error) {
317328
enriched := &enrichedExecutableList{}
318329
for _, exec := range l {
319-
enriched.Executables = append(enriched.Executables, &enrichedExecutable{
320-
ID: exec.ID(),
321-
Spec: exec,
322-
})
330+
enriched.Executables = append(enriched.Executables, exec.enriched())
323331
}
324332
yamlBytes, err := yaml.Marshal(enriched)
325333
if err != nil {
@@ -331,10 +339,7 @@ func (l ExecutableList) YAML() (string, error) {
331339
func (l ExecutableList) JSON() (string, error) {
332340
enriched := &enrichedExecutableList{}
333341
for _, exec := range l {
334-
enriched.Executables = append(enriched.Executables, &enrichedExecutable{
335-
ID: exec.ID(),
336-
Spec: exec,
337-
})
342+
enriched.Executables = append(enriched.Executables, exec.enriched())
338343
}
339344
jsonBytes, err := json.MarshalIndent(enriched, "", " ")
340345
if err != nil {
@@ -526,3 +531,73 @@ func NewExecutableID(workspace, namespace, name string) string {
526531
return ""
527532
}
528533
}
534+
535+
func (e *Executable) MarshalJSON() ([]byte, error) {
536+
type Alias Executable
537+
aux := &struct {
538+
*Alias
539+
Timeout string `json:"timeout,omitempty"`
540+
}{
541+
Alias: (*Alias)(e),
542+
}
543+
if e.Timeout != 0 {
544+
aux.Timeout = e.Timeout.String()
545+
}
546+
return json.Marshal(aux)
547+
}
548+
549+
func (e *Executable) UnmarshalJSON(data []byte) error {
550+
type Alias Executable
551+
aux := &struct {
552+
*Alias
553+
Timeout string `json:"timeout,omitempty"`
554+
}{
555+
Alias: (*Alias)(e),
556+
}
557+
if err := json.Unmarshal(data, &aux); err != nil {
558+
return err
559+
}
560+
if aux.Timeout != "" {
561+
duration, err := time.ParseDuration(aux.Timeout)
562+
if err != nil {
563+
return err
564+
}
565+
e.Timeout = duration
566+
}
567+
return nil
568+
}
569+
570+
func (r *RequestExecutableType) MarshalJSON() ([]byte, error) {
571+
type Alias RequestExecutableType
572+
aux := &struct {
573+
*Alias
574+
Timeout string `json:"timeout,omitempty"`
575+
}{
576+
Alias: (*Alias)(r),
577+
}
578+
if r.Timeout != 0 {
579+
aux.Timeout = r.Timeout.String()
580+
}
581+
return json.Marshal(aux)
582+
}
583+
584+
func (r *RequestExecutableType) UnmarshalJSON(data []byte) error {
585+
type Alias RequestExecutableType
586+
aux := &struct {
587+
*Alias
588+
Timeout string `json:"timeout,omitempty"`
589+
}{
590+
Alias: (*Alias)(r),
591+
}
592+
if err := json.Unmarshal(data, &aux); err != nil {
593+
return err
594+
}
595+
if aux.Timeout != "" {
596+
duration, err := time.ParseDuration(aux.Timeout)
597+
if err != nil {
598+
return err
599+
}
600+
r.Timeout = duration
601+
}
602+
return nil
603+
}

types/executable/executable_md.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
func execMarkdown(e *Executable) string {
1111
var mkdwn string
1212
mkdwn += fmt.Sprintf("# [Executable] %s\n", e.Ref())
13-
mkdwn += execDescriptionMarkdown(e)
13+
mkdwn += execDescriptionMarkdown(e, true)
1414
if e.Visibility != nil {
1515
mkdwn += fmt.Sprintf("**Visibility:** %s\n", *e.Visibility)
1616
}
@@ -38,12 +38,16 @@ func execMarkdown(e *Executable) string {
3838
return mkdwn
3939
}
4040

41-
func execDescriptionMarkdown(e *Executable) string {
41+
func execDescriptionMarkdown(e *Executable, withPrefix bool) string {
4242
if e.Description == "" && e.inheritedDescription == "" {
4343
return ""
4444
}
4545
var mkdwn string
46-
const prefix = "│ "
46+
47+
prefix := ""
48+
if withPrefix {
49+
prefix = "│ "
50+
}
4751
if d := strings.TrimSpace(e.Description); d != "" {
4852
mkdwn += prefix + "\n"
4953
mkdwn += addPrefx(d, prefix)

types/workspace/workspace.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package workspace
33
import (
44
"encoding/json"
55
"fmt"
6+
"strings"
67

78
"github.com/jahvon/tuikit/types"
89
"gopkg.in/yaml.v3"
@@ -15,8 +16,24 @@ import (
1516

1617
type WorkspaceList []*Workspace
1718

18-
type enrichedWorkspaceConfigList struct {
19-
Workspaces WorkspaceList `json:"workspaces" yaml:"workspaces"`
19+
type enrichedWorkspaceList struct {
20+
Workspaces []*enrichedWorkspace `json:"workspaces" yaml:"workspaces"`
21+
}
22+
23+
type enrichedWorkspace struct {
24+
*Workspace
25+
Name string `json:"name" yaml:"name"`
26+
Path string `json:"path" yaml:"path"`
27+
FullDescription string `json:"fullDescription" yaml:"fullDescription"`
28+
}
29+
30+
func (w *Workspace) enriched() *enrichedWorkspace {
31+
return &enrichedWorkspace{
32+
Workspace: w,
33+
Name: w.AssignedName(),
34+
Path: w.Location(),
35+
FullDescription: strings.TrimSpace(workspaceDescription(w, false)),
36+
}
2037
}
2138

2239
func (w *Workspace) AssignedName() string {
@@ -33,15 +50,15 @@ func (w *Workspace) SetContext(name, location string) {
3350
}
3451

3552
func (w *Workspace) YAML() (string, error) {
36-
yamlBytes, err := yaml.Marshal(w)
53+
yamlBytes, err := yaml.Marshal(w.enriched())
3754
if err != nil {
3855
return "", fmt.Errorf("failed to marshal workspace config - %w", err)
3956
}
4057
return string(yamlBytes), nil
4158
}
4259

4360
func (w *Workspace) JSON() (string, error) {
44-
jsonBytes, err := json.MarshalIndent(w, "", " ")
61+
jsonBytes, err := json.MarshalIndent(w.enriched(), "", " ")
4562
if err != nil {
4663
return "", fmt.Errorf("failed to marshal workspace config - %w", err)
4764
}
@@ -57,7 +74,10 @@ func DefaultWorkspaceConfig(name string) *Workspace {
5774
}
5875

5976
func (l WorkspaceList) YAML() (string, error) {
60-
enriched := enrichedWorkspaceConfigList{Workspaces: l}
77+
enriched := enrichedWorkspaceList{Workspaces: make([]*enrichedWorkspace, 0, len(l))}
78+
for _, ws := range l {
79+
enriched.Workspaces = append(enriched.Workspaces, ws.enriched())
80+
}
6181
yamlBytes, err := yaml.Marshal(enriched)
6282
if err != nil {
6383
return "", fmt.Errorf("failed to marshal workspace config list - %w", err)
@@ -66,7 +86,10 @@ func (l WorkspaceList) YAML() (string, error) {
6686
}
6787

6888
func (l WorkspaceList) JSON() (string, error) {
69-
enriched := enrichedWorkspaceConfigList{Workspaces: l}
89+
enriched := enrichedWorkspaceList{Workspaces: make([]*enrichedWorkspace, 0, len(l))}
90+
for _, ws := range l {
91+
enriched.Workspaces = append(enriched.Workspaces, ws.enriched())
92+
}
7093
jsonBytes, err := json.MarshalIndent(enriched, "", " ")
7194
if err != nil {
7295
return "", fmt.Errorf("failed to marshal workspace config list - %w", err)

types/workspace/workspace_md.go

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func workspaceMarkdown(w *Workspace) string {
1414
} else {
1515
mkdwn = fmt.Sprintf("# [Workspace] %s\n", w.AssignedName())
1616
}
17-
mkdwn += workspaceDescription(w)
17+
mkdwn += workspaceDescription(w, true)
1818
if len(w.Tags) > 0 {
1919
mkdwn += "**Tags**\n"
2020
for _, tag := range w.Tags {
@@ -40,29 +40,42 @@ func workspaceMarkdown(w *Workspace) string {
4040
return mkdwn
4141
}
4242

43-
func workspaceDescription(w *Workspace) string {
43+
func workspaceDescription(w *Workspace, withPrefix bool) string {
44+
if w.Description == "" && w.DescriptionFile == "" {
45+
return ""
46+
}
4447
var mkdwn string
45-
const descSpacer = "> \n"
46-
if w.Description != "" {
47-
mkdwn += descSpacer
48-
lines := strings.Split(w.Description, "\n")
49-
for _, line := range lines {
50-
mkdwn += fmt.Sprintf("> %s\n", line)
51-
}
52-
mkdwn += descSpacer
48+
49+
prefix := ""
50+
if withPrefix {
51+
prefix = "> "
52+
}
53+
if d := strings.TrimSpace(w.Description); d != "" {
54+
mkdwn += prefix + "\n"
55+
mkdwn += addPrefix(d, prefix)
5356
}
5457
if w.DescriptionFile != "" {
55-
mdBytes, err := os.ReadFile(filepath.Clean(w.DescriptionFile))
58+
wsFile := filepath.Join(w.Location(), w.DescriptionFile)
59+
mdBytes, err := os.ReadFile(filepath.Clean(wsFile))
5660
if err != nil {
57-
mkdwn += fmt.Sprintf("> **error rendering description file**: %s\n", err)
58-
} else {
59-
lines := strings.Split(string(mdBytes), "\n")
60-
for _, line := range lines {
61-
mkdwn += fmt.Sprintf("> %s\n", line)
62-
}
61+
mkdwn += addPrefix(fmt.Sprintf("**error rendering description file**: %s", err), prefix)
62+
} else if d := strings.TrimSpace(string(mdBytes)); d != "" {
63+
mkdwn += prefix + "\n"
64+
mkdwn += addPrefix(d, prefix)
6365
}
64-
mkdwn += descSpacer
66+
}
67+
if mkdwn != "" {
68+
mkdwn += prefix + "\n"
6569
}
6670
mkdwn += "\n"
6771
return mkdwn
6872
}
73+
74+
func addPrefix(s, prefix string) string {
75+
lines := strings.Split(s, "\n")
76+
var final string
77+
for _, line := range lines {
78+
final += prefix + line + "\n"
79+
}
80+
return final
81+
}

0 commit comments

Comments
 (0)