• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / modo / 12734892245 / 1

Source File

65.74
/document/document.go
1
package document
2

3
import (
4
        "bytes"
5
        "encoding/json"
6
        "fmt"
7
        "strings"
8
        "unicode"
9
)
10

11
const capitalFileMarker = "-"
12

13
var CaseSensitiveSystem = true
14

15
type Docs struct {
16
        Decl    *Package
17
        Version string
18
}
19

20
type Package struct {
21
        Kind
22
        Name
23
        Path
24
        Description string
25
        Summary     string
26
        Modules     []*Module
27
        Packages    []*Package
28
}
29

30
type Module struct {
31
        Kind
32
        Name
33
        Path
34
        Summary     string
35
        Description string
36
        Aliases     []*Alias
37
        Functions   []*Function
38
        Structs     []*Struct
39
        Traits      []*Trait
40
}
41

42
type Alias struct {
43
        Kind
44
        Name
45
        Description string
46
        Summary     string
47
        Value       string
48
        Deprecated  string
49
}
50

51
type Struct struct {
52
        Kind
53
        Name
54
        Path
55
        Description  string
56
        Summary      string
57
        Aliases      []*Alias
58
        Constraints  string
59
        Convention   string
60
        Deprecated   string
61
        Fields       []*Field
62
        Functions    []*Function
63
        Parameters   []*Parameter
64
        ParentTraits []string
65
        Signature    string
66
}
67

68
type Function struct {
69
        Kind
70
        Name
71
        Path
72
        Description          string
73
        Summary              string
74
        Args                 []*Arg
75
        Overloads            []*Function
76
        Async                bool
77
        Constraints          string
78
        Deprecated           string
79
        IsDef                bool
80
        IsStatic             bool
81
        IsImplicitConversion bool
82
        Raises               bool
83
        RaisesDoc            string
84
        ReturnType           string
85
        ReturnsDoc           string
86
        Signature            string
87
        Parameters           []*Parameter
88
}
89

90
type Field struct {
91
        Kind
92
        Name
93
        Description string
94
        Summary     string
95
        Type        string
96
}
97

98
type Trait struct {
99
        Kind
100
        Name
101
        Path
102
        Description  string
103
        Summary      string
104
        Fields       []*Field
105
        Functions    []*Function
106
        ParentTraits []string
107
        Deprecated   string
108
}
109

110
type Arg struct {
111
        Kind
112
        Name
113
        Description string
114
        Convention  string
115
        Type        string
116
        PassingKind string
117
        Default     string
118
}
119

120
type Parameter struct {
121
        Kind
122
        Name
123
        Description string
124
        Type        string
125
        PassingKind string
126
        Default     string
127
}
128

129
func FromJson(data []byte) (*Docs, error) {
1✔
130
        reader := bytes.NewReader(data)
1✔
131
        dec := json.NewDecoder(reader)
1✔
132
        dec.DisallowUnknownFields()
1✔
133

1✔
134
        var docs Docs
1✔
135

1✔
136
        if err := dec.Decode(&docs); err != nil {
1✔
137
                return nil, err
×
138
        }
×
139

140
        cleanup(&docs)
1✔
141

1✔
142
        return &docs, nil
1✔
143
}
144

145
func cleanup(doc *Docs) {
2✔
146
        cleanupPackage(doc.Decl)
2✔
147
}
2✔
148

149
func cleanupPackage(p *Package) {
2✔
150
        for _, pp := range p.Packages {
2✔
151
                cleanupPackage(pp)
×
152
        }
×
153
        newModules := make([]*Module, 0, len(p.Modules))
2✔
154
        for _, m := range p.Modules {
4✔
155
                cleanupModule(m)
2✔
156
                if m.GetName() != "__init__" {
3✔
157
                        newModules = append(newModules, m)
1✔
158
                }
1✔
159
        }
160
        p.Modules = newModules
2✔
161
}
162

163
func cleanupModule(m *Module) {
2✔
164
        for _, s := range m.Structs {
2✔
165
                if s.Signature == "" {
×
166
                        s.Signature = createSignature(s)
×
167
                }
×
168
        }
169
}
170

171
func createSignature(s *Struct) string {
4✔
172
        b := strings.Builder{}
4✔
173
        b.WriteString("struct ")
4✔
174
        b.WriteString(s.GetName())
4✔
175

4✔
176
        if len(s.Parameters) == 0 {
4✔
177
                return b.String()
×
178
        }
×
179

180
        b.WriteString("[")
4✔
181

4✔
182
        prevKind := ""
4✔
183
        for i, par := range s.Parameters {
11✔
184
                written := false
7✔
185
                if par.PassingKind == "kw" && prevKind != par.PassingKind {
9✔
186
                        if i > 0 {
3✔
187
                                b.WriteString(", ")
1✔
188
                        }
1✔
189
                        b.WriteString("*")
2✔
190
                        written = true
2✔
191
                }
192
                if prevKind == "inferred" && par.PassingKind != prevKind {
8✔
193
                        b.WriteString(", //")
1✔
194
                        written = true
1✔
195
                }
1✔
196
                if prevKind == "pos" && par.PassingKind != prevKind {
8✔
197
                        b.WriteString(", /")
1✔
198
                        written = true
1✔
199
                }
1✔
200

201
                if i > 0 || written {
11✔
202
                        b.WriteString(", ")
4✔
203
                }
4✔
204

205
                b.WriteString(fmt.Sprintf("%s: %s", par.GetName(), par.Type))
7✔
206
                if len(par.Default) > 0 {
7✔
207
                        b.WriteString(fmt.Sprintf(" = %s", par.Default))
×
208
                }
×
209

210
                prevKind = par.PassingKind
7✔
211
        }
212
        if prevKind == "inferred" {
5✔
213
                b.WriteString(", //")
1✔
214
        }
1✔
215
        if prevKind == "pos" {
5✔
216
                b.WriteString(", /")
1✔
217
        }
1✔
218

219
        b.WriteString("]")
4✔
220

4✔
221
        return b.String()
4✔
222
}
223

224
type Kinded interface {
225
        GetKind() string
226
}
227

228
type Named interface {
229
        GetName() string
230
        GetFileName() string
231
}
232

233
type Pathed interface {
234
        GetPath() string
235
        SetPath(p string)
236
}
237

238
type Kind struct {
239
        Kind string
240
}
241

242
func NewKind(kind string) Kind {
×
243
        return Kind{Kind: kind}
×
244
}
×
245

246
func (k *Kind) GetKind() string {
×
247
        return k.Kind
×
248
}
×
249

250
type Name struct {
251
        Name string
252
}
253

254
func NewName(name string) Name {
13✔
255
        return Name{Name: name}
13✔
256
}
13✔
257

258
func (k *Name) GetName() string {
13✔
259
        return k.Name
13✔
260
}
13✔
261

262
func (k *Name) GetFileName() string {
×
263
        if CaseSensitiveSystem {
×
264
                return k.Name
×
265
        }
×
266
        if isCap(k.Name) {
×
267
                return k.Name + capitalFileMarker
×
268
        }
×
269
        return k.Name
×
270
}
271

272
type Path struct {
273
        Path string
274
}
275

276
func (p *Path) GetPath() string {
×
277
        return p.Path
×
278
}
×
279

280
func (p *Path) SetPath(path string) {
×
281
        p.Path = path
×
282
}
×
283

284
func isCap(s string) bool {
×
285
        if len(s) == 0 {
×
286
                return false
×
287
        }
×
288
        firstRune := []rune(s)[0]
×
289
        return unicode.IsUpper(firstRune)
×
290
}
  • Back to Build 12734892245
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc