-
-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy patharazzo.go
More file actions
95 lines (81 loc) · 3.01 KB
/
Copy patharazzo.go
File metadata and controls
95 lines (81 loc) · 3.01 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
// Copyright 2022-2026 Princess Beef Heavy Industries / Dave Shanley
// SPDX-License-Identifier: MIT
package libopenapi
import (
gocontext "context"
"fmt"
"log/slog"
high "github.com/pb33f/libopenapi/datamodel/high/arazzo"
"github.com/pb33f/libopenapi/datamodel/low"
lowArazzo "github.com/pb33f/libopenapi/datamodel/low/arazzo"
"go.yaml.in/yaml/v4"
)
// ArazzoDocumentConfiguration supplies immutable parsing and origin context.
type ArazzoDocumentConfiguration struct {
Context gocontext.Context
RetrievalURI string
ApplicationBaseURI string
// Logger receives debug detail about how the document identity and effective
// base URI were derived. Origin resolution weighs $self, the retrieval URI and
// the application base URI against each other, so the outcome is worth tracing
// when a relative source URL resolves somewhere unexpected. Nil disables logging.
Logger *slog.Logger
}
// NewArazzoDocument parses raw bytes into a high-level Arazzo document.
func NewArazzoDocument(arazzoBytes []byte) (*high.Arazzo, error) {
return NewArazzoDocumentWithConfiguration(arazzoBytes, nil)
}
// NewArazzoDocumentWithConfiguration parses an Arazzo document with retrieval and base-URI context.
func NewArazzoDocumentWithConfiguration(
arazzoBytes []byte,
configuration *ArazzoDocumentConfiguration,
) (*high.Arazzo, error) {
config := ArazzoDocumentConfiguration{}
if configuration != nil {
config = *configuration
}
var rootNode yaml.Node
if err := yaml.Unmarshal(arazzoBytes, &rootNode); err != nil {
return nil, fmt.Errorf("failed to parse YAML: %w", err)
}
if rootNode.Kind != yaml.DocumentNode || len(rootNode.Content) == 0 {
return nil, fmt.Errorf("invalid YAML document structure")
}
mappingNode := rootNode.Content[0]
if mappingNode.Kind != yaml.MappingNode {
return nil, fmt.Errorf("expected YAML mapping, got %v", mappingNode.Kind)
}
// Build the low-level model
lowDoc := &lowArazzo.Arazzo{}
if err := low.BuildModel(mappingNode, lowDoc); err != nil {
return nil, fmt.Errorf("failed to build low-level model: %w", err)
}
ctx := config.Context
if ctx == nil {
ctx = gocontext.Background()
}
if err := lowDoc.Build(ctx, nil, mappingNode, nil); err != nil {
return nil, fmt.Errorf("failed to build arazzo document: %w", err)
}
origin, err := high.ResolveDocumentOrigin(
lowDoc.Self.Value,
config.RetrievalURI,
config.ApplicationBaseURI,
)
if err != nil {
return nil, fmt.Errorf("failed to resolve arazzo document origin: %w", err)
}
// ResolveDocumentOrigin returns a non-nil origin on every non-error path, so only
// the logger itself needs guarding.
if config.Logger != nil {
config.Logger.DebugContext(ctx, "resolved arazzo document origin",
"authoredSelf", origin.AuthoredSelf,
"resolvedIdentity", origin.ResolvedIdentity,
"retrievalURI", origin.RetrievalURI,
"applicationBaseURI", origin.ApplicationBaseURI,
"effectiveBaseURI", origin.EffectiveBaseURI)
}
// Build the high-level model
highDoc := high.NewArazzoWithOrigin(lowDoc, origin)
return highDoc, nil
}