Skip to content

Commit

Permalink
refactor: move nodeFields.json into ./scripts/data/
Browse files Browse the repository at this point in the history
…and map the field data from tuples into objects.
  • Loading branch information
aleclarson committed Nov 11, 2024
1 parent dafbc22 commit b3e72bf
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
File renamed without changes.
28 changes: 28 additions & 0 deletions scripts/data/fieldMetadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import fs from 'node:fs'
import path from 'node:path'
import { mapValues } from 'radashi'
import type { NodeFieldMetadataByTag } from '../inferFieldMetadata'

const dataDir = new URL('.', import.meta.url).pathname
const data = mapValues(
JSON.parse(
fs.readFileSync(path.join(dataDir, 'fieldMetadata.json'), 'utf8'),
) as NodeFieldMetadataByTag,
fieldMap =>
mapValues(fieldMap!, ([nullable, tags, listTags]) => ({
nullable,
tags,
listTags,
})),
)

/**
* Field metadata is inferred from test cases sourced from the libpg_query
* repository. Check out the {@link ../inferFieldMetadata.ts inferFieldMetadata}
* module for more details.
*/
export const fieldMetadataMap = data as {
[typeName: string]: {
[fieldName: string]: typeof data[string][string] | undefined
} | undefined
}
22 changes: 9 additions & 13 deletions scripts/generateTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@ import path from 'node:path'
import { bitMasks } from './data/bitMasks'
import { expressionFields } from './data/expressionFields'
import { expressionTypes } from './data/expressionTypes'
import { fieldMetadataMap } from './data/fieldMetadata'
import { nullableFields } from './data/nullableFields'
import { skippedEntities } from './data/skippedEntities'
import { typeMappings } from './data/typeMappings'
import type { NodeFieldMetadataByTag } from './inferFieldMetadata'

/** A record of node tag -> field name -> nullability */
const nodeFieldsByTag: NodeFieldMetadataByTag = JSON.parse(
fs.readFileSync('nodeFields.json', 'utf8'),
)

type StructsByModule = Record<string, Record<string, StructDef>>
type EnumsByModule = Record<string, Record<string, EnumDef>>
Expand Down Expand Up @@ -63,8 +58,9 @@ function formatComment(
}

function readSrcData<T>(fileName: string): T {
const filePath = path.join(__dirname, '../libpg_query/srcdata', fileName)
const content = fs.readFileSync(filePath, 'utf-8')
const scriptDir = new URL('.', import.meta.url).pathname
const dataPath = path.join(scriptDir, '../libpg_query/srcdata', fileName)
const content = fs.readFileSync(dataPath, 'utf-8')
return JSON.parse(content) as T
}

Expand Down Expand Up @@ -246,7 +242,7 @@ async function main() {
constTypes.push(`{ ${namedFields[0].name}: ${typeName} }`)
}

const fieldMetadata = nodeFieldsByTag[typeName]
const fieldMetadata = fieldMetadataMap[typeName]
if (!fieldMetadata) {
// If field metadata could not be inferred from libpg_query's test
// suite, then it's likely not a node type.
Expand Down Expand Up @@ -295,15 +291,15 @@ async function main() {
const debugTags = false

if (fieldType === 'any' || fieldType === 'Node') {
const inferredTags = fieldMetadata?.[fieldName]?.[1]
const inferredTags = fieldMetadata?.[fieldName]?.tags
if (inferredTags) {
if (debugTags) {
console.log('Inferred tags for %s:', fieldPath, inferredTags)
}
fieldType = renderTagTypes(inferredTags, fieldPath)
}
} else if (fieldType === 'any[]') {
const inferredListTags = fieldMetadata?.[fieldName]?.[2]
const inferredListTags = fieldMetadata?.[fieldName]?.listTags
if (inferredListTags) {
if (debugTags) {
console.log(
Expand All @@ -321,7 +317,7 @@ async function main() {
}
}
if (fieldType === 'any[]' && field.c_type === 'List*') {
const inferredTags = fieldMetadata?.[fieldName]?.[1]
const inferredTags = fieldMetadata?.[fieldName]?.tags
if (inferredTags) {
if (debugTags) {
console.log('Inferred tags for %s:', fieldPath, inferredTags)
Expand All @@ -343,7 +339,7 @@ async function main() {

const nullable =
(fieldMetadata
? fieldMetadata[fieldName]?.[0] ?? true
? fieldMetadata[fieldName]?.nullable ?? true
: /\b(NULL|NIL|if any)\b/.test(field.comment ?? '')) ||
nullableFields.has(fieldPath)

Expand Down
12 changes: 8 additions & 4 deletions scripts/inferFieldMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export type NodeFieldMetadata = [
]

export type NodeFieldMetadataByTag = {
[typeName: string]:
| { [fieldName: string]: NodeFieldMetadata | undefined }
| undefined
[typeName: string]: {
[fieldName: string]: NodeFieldMetadata
}
}

const fieldsByNodeTag: NodeFieldMetadataByTag = {}
Expand Down Expand Up @@ -93,4 +93,8 @@ for (const testFile of testFiles) {
} catch {}
}

fs.writeFileSync('nodeFields.json', JSON.stringify(fieldsByNodeTag, null, 2))
const scriptDir = new URL('.', import.meta.url).pathname
fs.writeFileSync(
path.join(scriptDir, 'data/fieldMetadata.json'),
JSON.stringify(fieldsByNodeTag, null, 2),
)

0 comments on commit b3e72bf

Please sign in to comment.