-
Notifications
You must be signed in to change notification settings - Fork 28
/
pageparams.go
105 lines (95 loc) · 2.22 KB
/
pageparams.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package openapi3html
import (
"encoding/json"
"fmt"
"html"
"os"
"strings"
oas3 "github.com/getkin/kin-openapi/openapi3"
"github.com/grokify/gocharts/data/table"
"github.com/grokify/gocharts/data/table/tabulator"
"github.com/grokify/spectrum/openapi3"
)
type PageParams struct {
PageTitle string
PageLink string
TableDomID string
Spec *openapi3.Spec
ColumnSet *tabulator.ColumnSet
OpsFilterFunc func(path, method string, op *oas3.Operation) bool
TableJSON []byte
}
func (pp *PageParams) PageLinkHTML() string {
pp.PageLink = strings.TrimSpace(pp.PageLink)
if len(pp.PageLink) == 0 {
return html.EscapeString(pp.PageTitle)
}
return fmt.Sprintf("<a href=\"%s\">%s</a>", pp.PageLink,
html.EscapeString(pp.PageTitle))
}
func (pp *PageParams) AddSpec(spec *openapi3.Spec) error {
sm := openapi3.SpecMore{Spec: spec}
tbl, err := sm.OperationsTable(pp.ColumnSet, pp.OpsFilterFunc)
if err != nil {
return err
}
return pp.AddOperationsTable(tbl)
}
func (pp *PageParams) AddOperationsTable(tbl *table.Table) error {
docs := tbl.ToDocuments()
jdocs, err := json.Marshal(docs)
if err != nil {
return err
}
pp.TableJSON = jdocs
return nil
}
func (pp *PageParams) TableJSONBytesOrEmpty() []byte {
if len(pp.TableJSON) > 0 {
return pp.TableJSON
}
if pp.Spec != nil {
pp.AddSpec(pp.Spec)
return pp.TableJSON
}
return []byte("[]")
}
func (pp *PageParams) TabulatorColumnsJSONBytesOrEmpty() []byte {
if pp.ColumnSet == nil || len(pp.ColumnSet.Columns) == 0 {
colSet := openapi3.OpTableColumnsDefault(false)
tcols := tabulator.BuildColumnsTabulator(colSet.Columns)
return tcols.MustColumnsJSON()
}
tcols := tabulator.BuildColumnsTabulator(pp.ColumnSet.Columns)
return tcols.MustColumnsJSON()
}
func (pp *PageParams) WriteFile(filename string) error {
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
WriteSpectrumUIPage(f, *pp)
return nil
}
/*
func DefaultColumns() text.Texts {
return text.Texts{
{
Display: "Method",
Slug: "method"},
{
Display: "Path",
Slug: "path"},
{
Display: "OperationID",
Slug: "operationId"},
{
Display: "Summary",
Slug: "summary"},
{
Display: "Tags",
Slug: "tags"},
}
}
*/