-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdocument.go
More file actions
120 lines (94 loc) · 3.03 KB
/
Copy pathdocument.go
File metadata and controls
120 lines (94 loc) · 3.03 KB
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
// Copyright 2026 Princess Beef Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package libasyncapi
import (
highasync "github.com/pb33f/libasyncapi/datamodel/high/asyncapi"
lowasync "github.com/pb33f/libasyncapi/datamodel/low/asyncapi"
"github.com/pb33f/libopenapi/index"
"go.yaml.in/yaml/v4"
)
// Document represents a parsed AsyncAPI document that can be rendered into a model or serialized.
type Document interface {
// GetVersion will return the exact version of the AsyncAPI specification set for the document.
GetVersion() string
// GetSpecInfo will return the SpecInfo instance that contains all specification information.
GetSpecInfo() *SpecInfo
// Model returns the high-level AsyncAPI model.
// Returns nil if parsing completely failed.
Model() *highasync.AsyncAPI
// GoLow returns the low-level AsyncAPI model.
// This provides access to line/column numbers and raw YAML nodes.
GoLow() *lowasync.AsyncAPI
// Index returns the reference index for this document.
Index() *index.SpecIndex
// Rolodex returns the file management system for multi-file specs.
Rolodex() *index.Rolodex
// RootNode returns the original YAML tree (not normalized).
// This provides raw access for tooling that needs the original structure.
RootNode() *yaml.Node
// Serialize marshals the original YAML root node back to bytes.
// This preserves the original structure and formatting.
// For model-based rendering, use Render() instead.
Serialize() ([]byte, error)
// Render will return a YAML representation of the high-level model.
Render() ([]byte, error)
// Errors returns all errors accumulated during parsing.
// The document may still contain a partial model even if errors occurred.
Errors() []error
// IsPartial returns true if errors occurred during parsing.
// When true, the model may be incomplete but still usable.
IsPartial() bool
}
type document struct {
version string
specInfo *SpecInfo
highModel *highasync.AsyncAPI
lowModel *lowasync.AsyncAPI
specIndex *index.SpecIndex
rolodex *index.Rolodex
rootNode *yaml.Node
errors []error
config *DocumentConfiguration
}
func (d *document) GetVersion() string {
return d.version
}
func (d *document) GetSpecInfo() *SpecInfo {
return d.specInfo
}
func (d *document) Model() *highasync.AsyncAPI {
return d.highModel
}
func (d *document) GoLow() *lowasync.AsyncAPI {
return d.lowModel
}
func (d *document) Index() *index.SpecIndex {
return d.specIndex
}
func (d *document) Rolodex() *index.Rolodex {
return d.rolodex
}
func (d *document) RootNode() *yaml.Node {
return d.rootNode
}
func (d *document) Serialize() ([]byte, error) {
if d.rootNode == nil {
return nil, ErrInvalidYAML
}
return yaml.Marshal(d.rootNode)
}
func (d *document) Render() ([]byte, error) {
if d.highModel == nil {
return nil, ErrInvalidYAML
}
return d.highModel.Render()
}
func (d *document) Errors() []error {
return d.errors
}
func (d *document) IsPartial() bool {
return len(d.errors) > 0
}
func (d *document) addError(err error) {
d.errors = append(d.errors, err)
}