Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
loicknuchel committed Oct 30, 2024
1 parent 53b06c9 commit 8b0bd91
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 49 deletions.
2 changes: 1 addition & 1 deletion libs/aml/src/amlParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ function tokenPosition(token: IToken): TokenPosition {
}

function pos(value: number | undefined): number {
return value !== undefined && !isNaN(value) ? value : defaultPos
return value !== undefined && !Number.isNaN(value) ? value : defaultPos
}

// utils functions
Expand Down
4 changes: 2 additions & 2 deletions libs/models/src/parserResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ export const positionEndAdd = <T extends TokenPosition>(pos: T, value: number):
})

const posStart = (values: number[]): number => {
const valid = values.filter(n => n >= 0 && !isNaN(n) && isFinite(n))
const valid = values.filter(n => n >= 0 && !Number.isNaN(n) && isFinite(n))
return valid.length > 0 ? Math.min(...valid) : 0
}

const posEnd = (values: number[]): number => {
const valid = values.filter(n => n >= 0 && !isNaN(n) && isFinite(n))
const valid = values.filter(n => n >= 0 && !Number.isNaN(n) && isFinite(n))
return valid.length > 0 ? Math.max(...valid) : 0
}
2 changes: 1 addition & 1 deletion libs/parser-sql/src/postgresAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export type TableCheckAst = { kind: 'Check', predicate: ExpressionAst } & Constr
export type TableFkAst = { kind: 'ForeignKey', columns: IdentifierAst[], ref: { token: TokenInfo, schema?: IdentifierAst, table: IdentifierAst, columns?: IdentifierAst[] }, onUpdate?: ForeignKeyActionAst & {token: TokenInfo}, onDelete?: ForeignKeyActionAst & {token: TokenInfo} } & ConstraintCommonAst
export type ConstraintCommonAst = { token: TokenInfo, constraint?: ConstraintNameAst }
export type ConstraintNameAst = { token: TokenInfo, name: IdentifierAst }
export type ColumnTypeAst = { token: TokenInfo, schema?: IdentifierAst, name: { token: TokenInfo, value: string }, args?: IntegerAst[], array?: TokenInfo }
export type ColumnTypeAst = { token: TokenInfo, schema?: IdentifierAst, name: IdentifierAst, args?: IntegerAst[], array?: TokenInfo }
export type ForeignKeyActionAst = { action: { kind: ForeignKeyAction, token: TokenInfo }, columns?: IdentifierAst[] }
export type SetValueAst = IdentifierAst | LiteralAst | (IdentifierAst | LiteralAst)[] | { kind: 'Default', token: TokenInfo }
export type OnConflictClauseAst = { token: TokenInfo, target?: OnConflictColumnsAst | OnConflictConstraintAst, action: OnConflictNothingAst | OnConflictUpdateAst }
Expand Down
2 changes: 1 addition & 1 deletion libs/parser-sql/src/postgresBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ function buildTableAttr(index: number, c: TableColumnAst, notNull?: boolean): At
return removeUndefined({
name: c.name.value,
type: c.type.name.value,
null: c.constraints?.find(c => c.kind === 'Nullable' ? !c.value : false) || notNull ? undefined : true,
null: (c.constraints?.find(c => c.kind === 'Nullable' ? !c.value : false) || notNull) ? undefined : true,
// gen: z.boolean().optional(), // not handled for now
default: (c.constraints || []).flatMap(c => c.kind === 'Default' ? [expressionToValue(c.expression)] : [])[0],
// attrs: z.lazy(() => Attribute.array().optional()), // no nested attrs from SQL
Expand Down
78 changes: 39 additions & 39 deletions libs/parser-sql/src/postgresParser.test.ts

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions libs/parser-sql/src/postgresParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ import {
} from "./postgresAst";

const LineComment = createToken({name: 'LineComment', pattern: /--.*/, group: 'comments'})
const BlockComment = createToken({name: 'BlockComment', pattern: /\/\*[^]*?\*\//, line_breaks: true, group: 'comments'})
const BlockComment = createToken({name: 'BlockComment', pattern: /\/\*[\s\S]*?\*\//, line_breaks: true, group: 'comments'})
const WhiteSpace = createToken({name: 'WhiteSpace', pattern: /\s+/, group: Lexer.SKIPPED})

const Identifier = createToken({name: 'Identifier', pattern: /\b[a-zA-Z_]\w*\b|"([^\\"]|\\\\|\\")+"/})
Expand Down Expand Up @@ -1614,6 +1614,7 @@ class PostgresParser extends EmbeddedActionsParser {
]))})
const array = $.OPTION3(() => tokenInfo2($.CONSUME(BracketLeft), $.CONSUME(BracketRight)))
const name = {
kind: 'Identifier' as const,
token: mergePositions(parts.flatMap(p => [p.name?.token, p.last]).concat([array])),
value: parts.filter(isNotUndefined).map(p => p.name?.value + (p.args ? `(${p.args.map(v => v.value).join(', ')})` : '')).join(' ') + (array ? '[]' : '')
}
Expand Down Expand Up @@ -1793,5 +1794,5 @@ function tokenPosition(token: IToken): TokenPosition {
}

function pos(value: number | undefined): number {
return value !== undefined && !isNaN(value) ? value : defaultPos
return value !== undefined && !Number.isNaN(value) ? value : defaultPos
}
18 changes: 15 additions & 3 deletions libs/utils/src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,22 @@ export const collectOne = <T, U>(arr: T[], f: (t: T) => U | undefined): U | unde
return undefined
}

export const distinct = <T>(arr: T[]): T[] => arr.filter((t, i) => arr.indexOf(t) === i)
export const distinct = <T>(arr: T[]): T[] => {
const seen = new Set<T>()
return arr.filter(t => {
if (seen.has(t)) return false
seen.add(t)
return true
})
}
export const distinctBy = <T>(arr: T[], by: (t: T) => string | number): T[] => {
const arrStr = arr.map(t => by(t))
return arr.filter((t, i) => arrStr.indexOf(by(t)) === i)
const seen = new Set<string | number>()
return arr.filter(t => {
const key = by(t)
if (seen.has(key)) return false
seen.add(key)
return true
})
}

export type Diff<T> = {left: T[], right: T[], both: {left: T, right: T}[]}
Expand Down

0 comments on commit 8b0bd91

Please sign in to comment.