|
| 1 | +package codegen |
| 2 | + |
| 3 | +// FilterOperationsByTag filters operations based on include/exclude tag lists. |
| 4 | +// Exclude is applied first, then include. |
| 5 | +func FilterOperationsByTag(ops []*OperationDescriptor, opts OutputOptions) []*OperationDescriptor { |
| 6 | + if len(opts.ExcludeTags) > 0 { |
| 7 | + tags := sliceToSet(opts.ExcludeTags) |
| 8 | + ops = filterOps(ops, func(op *OperationDescriptor) bool { |
| 9 | + return !operationHasTag(op, tags) |
| 10 | + }) |
| 11 | + } |
| 12 | + if len(opts.IncludeTags) > 0 { |
| 13 | + tags := sliceToSet(opts.IncludeTags) |
| 14 | + ops = filterOps(ops, func(op *OperationDescriptor) bool { |
| 15 | + return operationHasTag(op, tags) |
| 16 | + }) |
| 17 | + } |
| 18 | + return ops |
| 19 | +} |
| 20 | + |
| 21 | +// FilterOperationsByOperationID filters operations based on include/exclude operation ID lists. |
| 22 | +// Exclude is applied first, then include. |
| 23 | +func FilterOperationsByOperationID(ops []*OperationDescriptor, opts OutputOptions) []*OperationDescriptor { |
| 24 | + if len(opts.ExcludeOperationIDs) > 0 { |
| 25 | + ids := sliceToSet(opts.ExcludeOperationIDs) |
| 26 | + ops = filterOps(ops, func(op *OperationDescriptor) bool { |
| 27 | + return !ids[op.OperationID] |
| 28 | + }) |
| 29 | + } |
| 30 | + if len(opts.IncludeOperationIDs) > 0 { |
| 31 | + ids := sliceToSet(opts.IncludeOperationIDs) |
| 32 | + ops = filterOps(ops, func(op *OperationDescriptor) bool { |
| 33 | + return ids[op.OperationID] |
| 34 | + }) |
| 35 | + } |
| 36 | + return ops |
| 37 | +} |
| 38 | + |
| 39 | +// FilterOperations applies all operation filters (tags, operation IDs) from OutputOptions. |
| 40 | +func FilterOperations(ops []*OperationDescriptor, opts OutputOptions) []*OperationDescriptor { |
| 41 | + ops = FilterOperationsByTag(ops, opts) |
| 42 | + ops = FilterOperationsByOperationID(ops, opts) |
| 43 | + return ops |
| 44 | +} |
| 45 | + |
| 46 | +// FilterSchemasByName removes schemas whose component name is in the exclude list. |
| 47 | +// Only filters top-level component schemas (path: components/schemas/<name>). |
| 48 | +func FilterSchemasByName(schemas []*SchemaDescriptor, excludeNames []string) []*SchemaDescriptor { |
| 49 | + if len(excludeNames) == 0 { |
| 50 | + return schemas |
| 51 | + } |
| 52 | + excluded := sliceToSet(excludeNames) |
| 53 | + result := make([]*SchemaDescriptor, 0, len(schemas)) |
| 54 | + for _, s := range schemas { |
| 55 | + // Check if this is a top-level component schema |
| 56 | + if len(s.Path) == 3 && s.Path[0] == "components" && s.Path[1] == "schemas" { |
| 57 | + if excluded[s.Path[2]] { |
| 58 | + continue |
| 59 | + } |
| 60 | + } |
| 61 | + result = append(result, s) |
| 62 | + } |
| 63 | + return result |
| 64 | +} |
| 65 | + |
| 66 | +// operationHasTag returns true if the operation has any of the given tags. |
| 67 | +func operationHasTag(op *OperationDescriptor, tags map[string]bool) bool { |
| 68 | + if op == nil || op.Spec == nil { |
| 69 | + return false |
| 70 | + } |
| 71 | + for _, tag := range op.Spec.Tags { |
| 72 | + if tags[tag] { |
| 73 | + return true |
| 74 | + } |
| 75 | + } |
| 76 | + return false |
| 77 | +} |
| 78 | + |
| 79 | +// filterOps returns operations that satisfy the predicate. |
| 80 | +func filterOps(ops []*OperationDescriptor, keep func(*OperationDescriptor) bool) []*OperationDescriptor { |
| 81 | + result := make([]*OperationDescriptor, 0, len(ops)) |
| 82 | + for _, op := range ops { |
| 83 | + if keep(op) { |
| 84 | + result = append(result, op) |
| 85 | + } |
| 86 | + } |
| 87 | + return result |
| 88 | +} |
| 89 | + |
| 90 | +// sliceToSet converts a string slice to a set (map[string]bool). |
| 91 | +func sliceToSet(items []string) map[string]bool { |
| 92 | + m := make(map[string]bool, len(items)) |
| 93 | + for _, item := range items { |
| 94 | + m[item] = true |
| 95 | + } |
| 96 | + return m |
| 97 | +} |
0 commit comments