-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprovider.go
More file actions
162 lines (127 loc) · 4.39 KB
/
provider.go
File metadata and controls
162 lines (127 loc) · 4.39 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
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
package farp
import (
"context"
"encoding/json"
)
// SchemaProvider generates schemas from application code
// Implementations generate protocol-specific schemas (OpenAPI, AsyncAPI, gRPC, GraphQL).
type SchemaProvider interface {
// Type returns the schema type this provider generates
Type() SchemaType
// Generate generates a schema from the application
// Returns the schema as interface{} (typically map[string]interface{} or struct)
Generate(ctx context.Context, app Application) (any, error)
// Validate validates a generated schema for correctness
Validate(schema any) error
// Hash calculates the SHA256 hash of a schema
Hash(schema any) (string, error)
// Serialize converts schema to bytes for storage/transmission
Serialize(schema any) ([]byte, error)
// Endpoint returns the HTTP endpoint where the schema is served
// Returns empty string if not served via HTTP
Endpoint() string
// SpecVersion returns the specification version (e.g., "3.1.0" for OpenAPI)
SpecVersion() string
// ContentType returns the content type for the schema
ContentType() string
}
// Application represents an application that can have schemas generated from it
// This is an abstraction to avoid direct dependency on Forge framework.
type Application interface {
// Name returns the application/service name
Name() string
// Version returns the application version
Version() string
// Routes returns route information for schema generation
// The actual type depends on the framework (e.g., []forge.RouteInfo)
Routes() any
}
// BaseSchemaProvider provides common functionality for schema providers.
type BaseSchemaProvider struct {
schemaType SchemaType
specVersion string
contentType string
endpoint string
validateFunc func(any) error
}
// Type returns the schema type.
func (p *BaseSchemaProvider) Type() SchemaType {
return p.schemaType
}
// SpecVersion returns the specification version.
func (p *BaseSchemaProvider) SpecVersion() string {
return p.specVersion
}
// ContentType returns the content type.
func (p *BaseSchemaProvider) ContentType() string {
return p.contentType
}
// Endpoint returns the HTTP endpoint.
func (p *BaseSchemaProvider) Endpoint() string {
return p.endpoint
}
// Hash calculates SHA256 hash of the schema.
func (p *BaseSchemaProvider) Hash(schema any) (string, error) {
return CalculateSchemaChecksum(schema)
}
// Serialize converts schema to JSON bytes.
func (p *BaseSchemaProvider) Serialize(schema any) ([]byte, error) {
return json.Marshal(schema)
}
// Validate validates the schema using custom validation function.
func (p *BaseSchemaProvider) Validate(schema any) error {
if p.validateFunc != nil {
return p.validateFunc(schema)
}
return nil
}
// ProviderRegistry manages registered schema providers.
type ProviderRegistry struct {
providers map[SchemaType]SchemaProvider
}
// NewProviderRegistry creates a new provider registry.
func NewProviderRegistry() *ProviderRegistry {
return &ProviderRegistry{
providers: make(map[SchemaType]SchemaProvider),
}
}
// Register registers a schema provider.
func (r *ProviderRegistry) Register(provider SchemaProvider) {
r.providers[provider.Type()] = provider
}
// Get retrieves a provider by schema type.
func (r *ProviderRegistry) Get(schemaType SchemaType) (SchemaProvider, bool) {
provider, ok := r.providers[schemaType]
return provider, ok
}
// Has checks if a provider exists for a schema type.
func (r *ProviderRegistry) Has(schemaType SchemaType) bool {
_, ok := r.providers[schemaType]
return ok
}
// List returns all registered schema types.
func (r *ProviderRegistry) List() []SchemaType {
types := make([]SchemaType, 0, len(r.providers))
for schemaType := range r.providers {
types = append(types, schemaType)
}
return types
}
// Global provider registry.
var globalRegistry = NewProviderRegistry()
// RegisterProvider registers a schema provider globally.
func RegisterProvider(provider SchemaProvider) {
globalRegistry.Register(provider)
}
// GetProvider retrieves a provider from the global registry.
func GetProvider(schemaType SchemaType) (SchemaProvider, bool) {
return globalRegistry.Get(schemaType)
}
// HasProvider checks if a provider exists in the global registry.
func HasProvider(schemaType SchemaType) bool {
return globalRegistry.Has(schemaType)
}
// ListProviders returns all registered schema types from the global registry.
func ListProviders() []SchemaType {
return globalRegistry.List()
}