Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion packages/cddl/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,32 @@ export function isPropertyReference (t: any): t is PropertyReference {
}

export function isNativeTypeWithOperator (t: any): t is NativeTypeWithOperator {
return t && typeof t.Type === 'object' && 'Operator' in t
return Boolean(
t &&
typeof t === 'object' &&
'Type' in t &&
!('Value' in t) &&
t.Operator &&
typeof t.Operator === 'object'
)
}

export function getRegexpPattern (t: any): string | undefined {
if (!isNativeTypeWithOperator(t)) {
return
}

if (typeof t.Type !== 'string' || !['str', 'text', 'tstr'].includes(t.Type)) {
return
}

if (t.Operator?.Type !== 'regexp' || !isLiteralWithValue(t.Operator.Value)) {
return
}

return typeof t.Operator.Value.Value === 'string'
? t.Operator.Value.Value
: undefined
}

export function isRange (t: any): boolean {
Expand Down
51 changes: 51 additions & 0 deletions packages/cddl/tests/cli.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { afterEach, describe, expect, it, vi } from 'vitest'

import { CLI_EPILOGUE } from '../src/cli/constants.js'

describe('cli entrypoint', () => {
afterEach(() => {
vi.resetModules()
vi.restoreAllMocks()
})

it('wires yargs commands and returns argv', async () => {
const command = vi.fn()
const example = vi.fn()
const epilogue = vi.fn()
const demandCommand = vi.fn()
const help = vi.fn()
const argvValue = { _: ['repl'] }
const chain = {
command,
example,
epilogue,
demandCommand,
help,
argv: argvValue
}

command.mockReturnValue(chain)
example.mockReturnValue(chain)
epilogue.mockReturnValue(chain)
demandCommand.mockReturnValue(chain)
help.mockReturnValue(chain)

const yargsMock = vi.fn().mockReturnValue(chain)
const hideBinMock = vi.fn().mockReturnValue(['repl'])

vi.doMock('yargs/yargs', () => ({ default: yargsMock }))
vi.doMock('yargs/helpers', () => ({ hideBin: hideBinMock }))

const { default: runCli } = await import('../src/cli/index.js')
const result = await runCli()

expect(hideBinMock).toHaveBeenCalledWith(process.argv)
expect(yargsMock).toHaveBeenCalledWith(['repl'])
expect(command).toHaveBeenCalledTimes(2)
expect(example).toHaveBeenCalledWith('$0 repl', 'Start CDDL repl')
expect(epilogue).toHaveBeenCalledWith(CLI_EPILOGUE)
expect(demandCommand).toHaveBeenCalledTimes(1)
expect(help).toHaveBeenCalledTimes(1)
expect(result).toEqual(argvValue)
})
})
8 changes: 8 additions & 0 deletions packages/cddl/tests/lexer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,12 @@ describe('lexer', () => {
expect(locEnd.line).toBe(0)
expect(locEnd.position).toBe(0)
})

it('should render caret location info for the current line', () => {
const l = new Lexer('foo\nbar')
l.nextToken()

expect(l.getLine(1)).toBe('bar')
expect(l.getLocationInfo()).toBe('foo\n ^\n |\n')
})
})
25 changes: 25 additions & 0 deletions packages/cddl/tests/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,29 @@ describe('parser', () => {

vi.restoreAllMocks()
})

it('parses RFC 9165 regexp operators on text strings', () => {
vi.spyOn(fs, 'readFileSync').mockReturnValue('channel = tstr .regexp "custom:.+"\n')
const p = new Parser('foo.cddl')

expect(p.parse()).toEqual([{
Type: 'variable',
Name: 'channel',
IsChoiceAddition: false,
PropertyType: [{
Type: 'tstr',
Operator: {
Type: 'regexp',
Value: {
Type: 'literal',
Value: 'custom:.+',
Unwrapped: false
}
}
}],
Comments: []
}])

vi.restoreAllMocks()
})
})
31 changes: 31 additions & 0 deletions packages/cddl/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import type {
PropertyReference,
Variable
} from '../src/ast.js'
import { Type } from '../src/ast.js'
import { Tokens, type Token } from '../src/tokens.js'
import {
getRegexpPattern,
hasSpecialNumberCharacter,
isAlphabeticCharacter,
isCDDLArray,
Expand Down Expand Up @@ -183,6 +185,17 @@ describe('utils', () => {
Value: 'tstr'
}
}
const nativeStringTypeWithRegexp: NativeTypeWithOperator = {
Type: Type.TSTR,
Operator: {
Type: 'regexp',
Value: {
Type: 'literal',
Value: 'custom:.+',
Unwrapped: false
}
}
}
const rangeReference: PropertyReference = {
Type: 'range',
Value: {
Expand All @@ -194,7 +207,25 @@ describe('utils', () => {
}

expect(isNativeTypeWithOperator(nativeTypeWithOperator)).toBe(true)
expect(isNativeTypeWithOperator(nativeStringTypeWithRegexp)).toBe(true)
expect(isNativeTypeWithOperator({ Type: 'tstr' })).toBe(false)
expect(isNativeTypeWithOperator({
Type: Type.TSTR,
Operator: 'regexp'
})).toBe(false)
expect(getRegexpPattern(nativeStringTypeWithRegexp)).toBe('custom:.+')
expect(getRegexpPattern(nativeTypeWithOperator)).toBeUndefined()
expect(getRegexpPattern({
Type: Type.TSTR,
Operator: {
Type: 'regexp',
Value: {
Type: 'literal',
Value: 42,
Unwrapped: false
}
}
})).toBeUndefined()

expect(isRange({ Type: rangeReference })).toBe(true)
expect(isRange({ Type: 'range' })).toBe(false)
Expand Down
77 changes: 76 additions & 1 deletion packages/cddl2py/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {
getRegexpPattern,
isCDDLArray, isGroup, isNamedGroupReference, isLiteralWithValue,
isNativeTypeWithOperator, isUnNamedProperty, isPropertyReference,
isRange, isVariable, pascalCase,
type Assignment, type PropertyType, type PropertyReference,
type Assignment, type NativeTypeWithOperator, type PropertyType, type PropertyReference,
type Property, type Array as CDDLArray, type Operator, type Group,
type Variable, type Comment, type Tag
} from 'cddl'
Expand Down Expand Up @@ -516,6 +517,72 @@ function getExtraItemsType (props: Property[], ctx: Context): string | undefined
return `Union[${uniqueTypes.join(', ')}]`
}

function stringifyPythonLiteral (value: string) {
return JSON.stringify(value)
}

function getTemplateAnnotatedPattern (regexpPattern: string): string | undefined {
const wildcard = '.+'
if (!regexpPattern.includes(wildcard) || /[\\()[\]{}|?*^$]/.test(regexpPattern.replaceAll(wildcard, ''))) {
return
}

const segments = regexpPattern.split(wildcard)
const parts: string[] = []

for (let i = 0; i < segments.length; i++) {
const segment = segments[i]
if (segment.length > 0) {
parts.push(stringifyPythonLiteral(segment))
}

if (i < segments.length - 1) {
parts.push('str')
}
}

if (parts.length === 0 || !parts.includes('str')) {
return
}

return `Annotated[str, ${parts.join(' + ')}]`
}

function resolveNativeTypeWithOperator (t: NativeTypeWithOperator, ctx: Context): string | undefined {
if (typeof t.Type !== 'string') {
return
}

const mapped = NATIVE_TYPE_MAP[t.Type]
if (!mapped) {
return
}

const regexpPattern = getRegexpPattern(t)
if (!regexpPattern) {
if (mapped === 'Any') {
ctx.typingImports.add('Any')
}
return mapped
}

const templateAnnotatedPattern = getTemplateAnnotatedPattern(regexpPattern)
if (!templateAnnotatedPattern) {
if (mapped === 'Any') {
ctx.typingImports.add('Any')
}
return mapped
}

ctx.typingImports.add('Annotated')
if (ctx.pydantic) {
ctx.pydanticImports.add('StringConstraints')
return `Annotated[${mapped}, StringConstraints(pattern=${JSON.stringify(regexpPattern)})]`
}

return templateAnnotatedPattern
}

// ---------------------------------------------------------------------------
// Type resolution
// ---------------------------------------------------------------------------
Expand All @@ -532,6 +599,14 @@ function resolveType (t: PropertyType, ctx: Context, options: ResolveTypeOptions
throw new Error(`Unknown native type: "${t}"`)
}

if (isNativeTypeWithOperator(t) && typeof t.Type === 'string') {
const resolved = resolveNativeTypeWithOperator(t, ctx)
if (resolved) {
return resolved
}
throw new Error(`Unknown native type with operator: ${JSON.stringify(t)}`)
}

if ((t as any).Type && typeof (t as any).Type === 'string' && NATIVE_TYPE_MAP[(t as any).Type]) {
const mapped = NATIVE_TYPE_MAP[(t as any).Type]
if (mapped === 'Any') {
Expand Down
68 changes: 68 additions & 0 deletions packages/cddl2py/tests/transform_edge_cases.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,74 @@ describe('transform edge cases', () => {
expect(output).toContain('Combined = Union[_CombinedVariant0, _CombinedVariant1]')
})

it('should preserve template-like regexp strings in python-friendly types', () => {
const typedDictOutput = transform([
variable('channel', {
Type: 'tstr',
Operator: {
Type: 'regexp',
Value: literal('custom:.+')
}
} as any),
variable('prefixed-name', {
Type: 'tstr',
Operator: {
Type: 'regexp',
Value: literal('foo_.+')
}
} as any),
variable('sandwiched-name', {
Type: 'tstr',
Operator: {
Type: 'regexp',
Value: literal('some_.+_name')
}
} as any),
variable('multi-slot-name', {
Type: 'tstr',
Operator: {
Type: 'regexp',
Value: literal('pre_.+_mid_.+_post')
}
} as any),
group('event-envelope', [
property('channel', {
Type: 'tstr',
Operator: {
Type: 'regexp',
Value: literal('custom:.+')
}
} as any)
]),
variable('email-address', {
Type: 'tstr',
Operator: {
Type: 'regexp',
Value: literal('[^@]+@[^@]+')
}
} as any)
])
const pydanticOutput = transform([
variable('channel', {
Type: 'tstr',
Operator: {
Type: 'regexp',
Value: literal('custom:.+')
}
} as any)
], { pydantic: true })

expect(typedDictOutput).toContain('from typing import Annotated')
expect(typedDictOutput).toContain('Channel = Annotated[str, "custom:" + str]')
expect(typedDictOutput).toContain('PrefixedName = Annotated[str, "foo_" + str]')
expect(typedDictOutput).toContain('SandwichedName = Annotated[str, "some_" + str + "_name"]')
expect(typedDictOutput).toContain('MultiSlotName = Annotated[str, "pre_" + str + "_mid_" + str + "_post"]')
expect(typedDictOutput).toContain('channel: Annotated[str, "custom:" + str]')
expect(typedDictOutput).toContain('EmailAddress = str')
expect(pydanticOutput).toContain('from pydantic import StringConstraints')
expect(pydanticOutput).toContain('Channel = Annotated[str, StringConstraints(pattern="custom:.+")]')
})

it('should collapse multiple union mixin groups into a single alias', () => {
const output = transform([
group('combined', [
Expand Down
Loading