|
| 1 | +package convert |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + "github.com/m7medVision/wpswag/internal/oas" |
| 7 | +) |
| 8 | + |
| 9 | +// Builder incrementally builds an OpenAPI 3.0.3 spec from WordPress routes. |
| 10 | +type Builder struct { |
| 11 | + spec *oas.Spec |
| 12 | + stats Stats |
| 13 | +} |
| 14 | + |
| 15 | +// Stats holds conversion statistics. |
| 16 | +type Stats struct { |
| 17 | + Routes int |
| 18 | + Endpoints int |
| 19 | + Ops int |
| 20 | +} |
| 21 | + |
| 22 | +// NewBuilder creates a new spec builder with the given metadata. |
| 23 | +func NewBuilder(title, description, serverURL string) *Builder { |
| 24 | + if title == "" { |
| 25 | + title = "WordPress REST" |
| 26 | + } |
| 27 | + if serverURL == "" { |
| 28 | + serverURL = "https://example.com/wp-json" |
| 29 | + } |
| 30 | + return &Builder{ |
| 31 | + spec: &oas.Spec{ |
| 32 | + OpenAPI: "3.0.3", |
| 33 | + Info: oas.Info{ |
| 34 | + Title: title, |
| 35 | + Description: description, |
| 36 | + Version: "1.0.0", |
| 37 | + }, |
| 38 | + Servers: []oas.Server{{URL: serverURL}}, |
| 39 | + Paths: map[string]oas.PathItem{}, |
| 40 | + }, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// AddPath sets a path item on the spec. |
| 45 | +func (b *Builder) AddPath(path string, item oas.PathItem) { |
| 46 | + b.spec.Paths[path] = item |
| 47 | +} |
| 48 | + |
| 49 | +// GetPath returns the current path item for a given path. |
| 50 | +func (b *Builder) GetPath(path string) oas.PathItem { |
| 51 | + return b.spec.Paths[path] |
| 52 | +} |
| 53 | + |
| 54 | +// AddSchema stores a reusable schema component on the spec. |
| 55 | +func (b *Builder) AddSchema(name string, schema oas.Schema) { |
| 56 | + if b.spec.Components == nil { |
| 57 | + b.spec.Components = &oas.Components{Schemas: map[string]oas.Schema{}} |
| 58 | + } |
| 59 | + if b.spec.Components.Schemas == nil { |
| 60 | + b.spec.Components.Schemas = map[string]oas.Schema{} |
| 61 | + } |
| 62 | + if _, exists := b.spec.Components.Schemas[name]; exists { |
| 63 | + return |
| 64 | + } |
| 65 | + b.spec.Components.Schemas[name] = schema |
| 66 | +} |
| 67 | + |
| 68 | +// IncrementRoutes increments the route counter. |
| 69 | +func (b *Builder) IncrementRoutes() { |
| 70 | + b.stats.Routes++ |
| 71 | +} |
| 72 | + |
| 73 | +// IncrementEndpoints increments the endpoint counter. |
| 74 | +func (b *Builder) IncrementEndpoints() { |
| 75 | + b.stats.Endpoints++ |
| 76 | +} |
| 77 | + |
| 78 | +// IncrementOps increments the operations counter. |
| 79 | +func (b *Builder) IncrementOps() { |
| 80 | + b.stats.Ops++ |
| 81 | +} |
| 82 | + |
| 83 | +// SetMethodOperation sets an operation on a path item by HTTP method. |
| 84 | +func SetMethodOperation(pi *oas.PathItem, method string, op *oas.Operation) { |
| 85 | + switch strings.ToUpper(method) { |
| 86 | + case "GET": |
| 87 | + pi.Get = op |
| 88 | + case "POST": |
| 89 | + pi.Post = op |
| 90 | + case "PUT": |
| 91 | + pi.Put = op |
| 92 | + case "PATCH": |
| 93 | + pi.Patch = op |
| 94 | + case "DELETE": |
| 95 | + pi.Delete = op |
| 96 | + case "OPTIONS": |
| 97 | + pi.Options = op |
| 98 | + case "HEAD": |
| 99 | + pi.Head = op |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// IsPathItemEmpty returns true if the path item has no operations. |
| 104 | +func IsPathItemEmpty(pi oas.PathItem) bool { |
| 105 | + return pi.Get == nil && pi.Post == nil && pi.Put == nil && |
| 106 | + pi.Patch == nil && pi.Delete == nil && pi.Options == nil && pi.Head == nil |
| 107 | +} |
| 108 | + |
| 109 | +// Build returns the final spec and stats. |
| 110 | +func (b *Builder) Build() (*oas.Spec, *Stats) { |
| 111 | + return b.spec, &b.stats |
| 112 | +} |
0 commit comments