-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
99 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"editor.formatOnSave": true | ||
"editor.formatOnSave": true, | ||
"java.saveActions.organizeImports": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import type { JSONNode } from './JSONNode'; | ||
|
||
export type URIResolver = (uri: string) => Promise<JSONNode> | JSONNode; | ||
export type URIResolver<R extends Promise<JSONNode> | JSONNode = Promise<JSONNode>> = (uri: string) => R; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { type JSONNode } from './JSONNode'; | ||
import { visitRef } from './visitRef'; | ||
|
||
export const collectRef = (json: JSONNode): string[] => { | ||
const refs: string[] = []; | ||
visitRef(json, _ => refs.push(_)); | ||
return refs; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { | ||
isJSONArray, | ||
isJSONRecord, | ||
isJSONRef, | ||
type JSONNode, | ||
type JSONRecord, | ||
type JSONRef, | ||
type JSONScalar | ||
} from './JSONNode'; | ||
|
||
export const mapJSONNode = <R extends JSONNode | Promise<JSONNode> | void>( | ||
json: JSONNode, | ||
handlers: { | ||
ref: (json: JSONRef) => R; | ||
record: (json: JSONRecord) => R; | ||
array: (json: JSONNode[]) => R; | ||
scalar?: (json: JSONScalar | null) => R; | ||
} | ||
): R => { | ||
if (isJSONRef(json)) { | ||
return handlers.ref(json); | ||
} else if (isJSONRecord(json)) { | ||
return handlers.record(json); | ||
} else if (isJSONArray(json)) { | ||
return handlers.array(json); | ||
} else { | ||
return handlers.scalar ? handlers.scalar(json) : (json as R); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const mergeRecords = <K extends string, V, R extends Record<K, V>>(entries: R[]): R => | ||
entries.reduce((acc, _) => Object.assign(acc, _), {} as R); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { type JSONNode, type JSONRecord } from './JSONNode'; | ||
import type { URIResolver } from './URIResolver'; | ||
import { mapJSONNode } from './mapJSONNode'; | ||
import { mergeRecords } from './mergeRecords'; | ||
import { resolveJPointer } from './resolveJPointer'; | ||
|
||
export const resolveRef = (json: JSONNode, resolver: URIResolver<JSONNode>): JSONNode => | ||
mapJSONNode(json, { | ||
ref: ({ $ref, ...rest }) => resolveRef($ref ? resolveJPointer(resolver($ref), $ref) : rest, resolver), | ||
record: record => | ||
mergeRecords( | ||
Object.entries(record).map(([key, value]) => ({ [key]: resolveRef(value, resolver) } as JSONRecord)) | ||
), | ||
array: list => list.map(_ => resolveRef(_, resolver)) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,19 @@ | ||
import { type JSONNode, type JSONRecord, type JSONRef, isJSONArray, isJSONRecord, isJSONRef } from './JSONNode'; | ||
import { type JSONNode, type JSONRecord } from './JSONNode'; | ||
import type { URIResolver } from './URIResolver'; | ||
import { mapJSONNode } from './mapJSONNode'; | ||
import { mergeRecords } from './mergeRecords'; | ||
import { resolveJPointer } from './resolveJPointer'; | ||
|
||
export const resolveRef = async (json: JSONNode, resolver: URIResolver): Promise<JSONNode> => { | ||
if (isJSONRef(json)) { | ||
return mapJSONRef(json, resolver); | ||
} else if (isJSONRecord(json)) { | ||
return mapJSONRecord(json, resolver); | ||
} else if (isJSONArray(json)) { | ||
return mapJSONArray(json, resolver); | ||
} else { | ||
return json; | ||
} | ||
}; | ||
|
||
const mapJSONArray = async (list: JSONNode[], resolver: URIResolver): Promise<JSONNode[]> => | ||
Promise.all(list.map(_ => resolveRef(_, resolver))); | ||
|
||
const mapJSONRecord = async (record: JSONRecord, resolver: URIResolver): Promise<JSONRecord> => | ||
( | ||
await Promise.all( | ||
Object.entries(record).map(async ([key, value]) => ({ | ||
[key]: await resolveRef(value, resolver) | ||
})) | ||
) | ||
).reduce((acc, _) => Object.assign(acc, _), {}); | ||
|
||
const mapJSONRef = async ({ $ref, ...rest }: JSONRef, resolver: URIResolver): Promise<JSONNode> => | ||
resolveRef($ref ? resolveJPointer(await resolver($ref), $ref) : rest, resolver); | ||
export const resolveRef = async (json: JSONNode, resolver: URIResolver): Promise<JSONNode> => | ||
mapJSONNode(json, { | ||
ref: async ({ $ref, ...rest }) => resolveRef($ref ? resolveJPointer(await resolver($ref), $ref) : rest, resolver), | ||
record: async record => | ||
mergeRecords( | ||
await Promise.all( | ||
Object.entries(record).map( | ||
async ([key, value]) => ({ [key]: await resolveRef(value, resolver) } as JSONRecord) | ||
) | ||
) | ||
), | ||
array: async list => Promise.all(list.map(_ => resolveRef(_, resolver))) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,17 @@ | ||
import { type JSONNode, isJSONArray, isJSONRecord, isJSONRef, type JSONRecord } from './JSONNode'; | ||
import { type JSONNode } from './JSONNode'; | ||
import { mapJSONNode } from './mapJSONNode'; | ||
|
||
type JSONNodePath = (string | number)[]; | ||
export type RefVisitor = (ref: string, path: JSONNodePath) => void; | ||
|
||
export const visitRef = (json: JSONNode, visitor: RefVisitor, path: JSONNodePath = []): void => { | ||
if (isJSONRef(json)) { | ||
visitor(json.$ref, path); | ||
} else if (isJSONRecord(json)) { | ||
visitJSONRecord(json, visitor, path); | ||
} else if (isJSONArray(json)) { | ||
visitJSONArray(json, visitor, path); | ||
} | ||
}; | ||
|
||
const visitJSONRecord = (record: JSONRecord, visitor: RefVisitor, path: JSONNodePath): void => { | ||
for (const key in record) visitRef(record[key], visitor, [...path, key]); | ||
}; | ||
|
||
const visitJSONArray = (list: JSONNode[], visitor: RefVisitor, path: JSONNodePath): void => { | ||
for (let i = 0; i < list.length; i++) visitRef(list[i], visitor, [...path, i]); | ||
mapJSONNode(json, { | ||
ref: json => visitor(json.$ref, path), | ||
record: record => { | ||
for (const key in record) visitRef(record[key], visitor, [...path, key]); | ||
}, | ||
array: list => { | ||
for (let i = 0; i < list.length; i++) visitRef(list[i], visitor, [...path, i]); | ||
} | ||
}); | ||
}; |