|
1 | 1 | import fs from 'fs'; |
2 | 2 | import path from 'path'; |
3 | 3 |
|
| 4 | +import _ from 'lodash'; |
4 | 5 | import yaml from 'yaml'; |
5 | 6 |
|
6 | 7 | import { download } from '../lib/download'; |
@@ -37,11 +38,46 @@ export class MobyOpenAPISpec extends GlobalDependency(VersionedDependency) { |
37 | 38 | for (const key of Object.keys(contents.definitions ?? {}).filter(k => /^Plugin./.test(k))) { |
38 | 39 | delete contents.definitions[key]?.['x-go-name']; |
39 | 40 | } |
40 | | - // This forces a go type that isn't defined; delete the override and just |
41 | | - // use strings instead. |
42 | | - if (contents.definitions?.Plugin?.properties?.Config?.properties?.Interface?.properties?.Types?.items?.['x-go-type']?.type === 'CapabilityID') { |
43 | | - delete contents.definitions.Plugin.properties.Config.properties.Interface.properties.Types.items['x-go-type']; |
44 | | - } |
| 41 | + |
| 42 | + // Some type overrides end up with errors, override them here: |
| 43 | + // noTypeOverride: This type does not actually exist in go, delete the override. |
| 44 | + // noValidate: This does not implement the .Validate() method; add a 'noValidation' hint. |
| 45 | + const perTypeActions: Record<string, 'noTypeOverride' | 'noValidate'> = { |
| 46 | + 'net/netip.Addr': 'noValidate', |
| 47 | + 'net/netip.Prefix': 'noValidate', |
| 48 | + 'time.Time': 'noValidate', |
| 49 | + 'undefined.int': 'noValidate', |
| 50 | + 'undefined.CapabilityID': 'noTypeOverride', // This type is not defined anywhere. |
| 51 | + 'undefined.SubnetStatuses': 'noTypeOverride', // This type is not defined anywhere. |
| 52 | + }; |
| 53 | + |
| 54 | + (function checkTypes(obj: object, prefix = '') { |
| 55 | + for (const [k, v] of Object.entries(obj)) { |
| 56 | + if (k === 'x-go-type') { |
| 57 | + const typeName = `${ v.import?.package }.${ v.type }`; |
| 58 | + if (typeName in perTypeActions) { |
| 59 | + switch (perTypeActions[typeName]) { |
| 60 | + case 'noTypeOverride': |
| 61 | + console.log(`\x1B[34m${ prefix } has invalid type ${ typeName }, removing.\x1B[0m`); |
| 62 | + delete (obj as any)[k]; |
| 63 | + break; |
| 64 | + case 'noValidate': |
| 65 | + console.log(`\x1B[34m${ prefix } has type ${ typeName }, disabling validation.\x1B[0m`); |
| 66 | + _.set(v, 'hints.noValidation', true); |
| 67 | + break; |
| 68 | + } |
| 69 | + } else { |
| 70 | + console.log(`\x1B[34m${ prefix } has unknown type ${ typeName }, ignoring.\x1B[0m`); |
| 71 | + } |
| 72 | + } else if (_.isPlainObject(v)) { |
| 73 | + checkTypes(v, `${ prefix }.${ k }`.replace(/^\./, '')); |
| 74 | + } else if (Array.isArray(v)) { |
| 75 | + for (const [i, element] of Object.entries(v)) { |
| 76 | + checkTypes(element, `${ prefix }[${ i }]`); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + })(contents); |
45 | 81 |
|
46 | 82 | await fs.promises.writeFile(modifiedPath, yaml.stringify(contents), 'utf-8'); |
47 | 83 |
|
|
0 commit comments