forked from kubernetes-sigs/kubebuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
182 lines (154 loc) · 4.53 KB
/
input.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package plugin
import (
"bytes"
"encoding/json"
"fmt"
)
// Context is a (partial) mdBook execution context.
type Context struct {
Root string `json:"root"`
Config Config `json:"config"`
}
// Config is a (partial) mdBook config
type Config struct {
Book BookConfig `json:"book"`
}
// BookConfig is a (partial) mdBook [book] stanza
type BookConfig struct {
Src string `json:"src"`
}
// Book is an mdBook book.
type Book struct {
Sections []BookItem `json:"sections"`
NonExhaustive *struct{} `json:"__non_exhaustive"`
}
// BookSection is an mdBook section.
type BookSection struct {
Items []BookItem `json:"items"`
}
// BookItem is an mdBook item.
// It wraps an underlying struct to provide proper marshalling and unmarshalling
// according to what serde produces/expects.
type BookItem bookItem
func (b *BookItem) UnmarshalJSON(input []byte) error {
// match how serde serializes rust enums.
if input[0] == '"' {
// actually a an empty variant
var variant string
if err := json.Unmarshal(input, &variant); err != nil {
return err
}
switch variant {
case "Separator":
b.Separator = true
default:
return fmt.Errorf("unknown book item variant %s", variant)
}
return nil
}
item := bookItem(*b)
if err := json.Unmarshal(input, &item); err != nil {
return err
}
*b = BookItem(item)
return nil
}
func (b BookItem) MarshalJSON() ([]byte, error) {
if b.Separator {
return json.Marshal("Separator")
}
return json.Marshal(bookItem(b))
}
// bookItem is the underlying mdBook item without custom serialization.
type bookItem struct {
Chapter *BookChapter `json:"Chapter"`
Separator bool `json:"-"`
}
// BookChapter is an mdBook chapter.
type BookChapter struct {
Name string `json:"name"`
Content string `json:"content"`
Number SectionNumber `json:"number"`
SubItems []BookItem `json:"sub_items"`
Path string `json:"path"`
ParentNames []string `json:"parent_names"`
}
// SectionNumber is an mdBook section number (e.g. `1.2` is `{1,2}`).
type SectionNumber []uint32
// Input is the tuple that's presented to mdBook plugins.
// It's deserialized from a slice `[context, book]`, matching
// a Rust tuple.
type Input struct {
Context Context
Book Book
}
func (p *Input) UnmarshalJSON(input []byte) error {
// deserialize from the JSON equivalent to the Rust tuple
// `(context, book)`
inputBuffer := bytes.NewBuffer(input)
dec := json.NewDecoder(inputBuffer)
tok, err := dec.Token()
if err != nil {
return err
}
if delim, isDelim := tok.(json.Delim); !isDelim || delim != '[' {
return fmt.Errorf("expected [, got %s", tok)
}
if err := dec.Decode(&p.Context); err != nil {
return err
}
if err := dec.Decode(&p.Book); err != nil {
return err
}
tok, err = dec.Token()
if err != nil {
return err
}
if delim, isDelim := tok.(json.Delim); !isDelim || delim != ']' {
return fmt.Errorf("expected ], got %s", tok)
}
return nil
}
// ChapterVisitor visits each BookChapter in a book, getting an actual
// pointer to the chapter that it can modify.
type ChapterVisitor func(*BookChapter) error
// EachItem calls the given visitor for each chapter in the given item,
// passing a pointer to the actual chapter that the visitor can modify.
func EachItem(parentItem *BookItem, visitor ChapterVisitor) error {
if parentItem.Chapter == nil {
return nil
}
if err := visitor(parentItem.Chapter); err != nil {
return err
}
// pass a pointer to the structure, not the iteration variable
for i := range parentItem.Chapter.SubItems {
if err := EachItem(&parentItem.Chapter.SubItems[i], visitor); err != nil {
return err
}
}
return nil
}
// EachItemInBook functions identically to EachItem, except that it visits
// all chapters in the book.
func EachItemInBook(book *Book, visitor ChapterVisitor) error {
// pass a pointer to the structure, not the iteration variable
for i := range book.Sections {
if err := EachItem(&book.Sections[i], visitor); err != nil {
return err
}
}
return nil
}