Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Support for Schema References. #92

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions src/@types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ export interface Schema extends RawSchema {
isValid: (payload: object, opts: { errorHook: (path: any) => void }) => void // FIXME:
}

export interface SchemaReference {
name: string
subject: string
version: number | string
}

export type SchemaRef = Omit<SchemaReference, 'name'>

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace jest {
Expand Down
26 changes: 22 additions & 4 deletions src/SchemaRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable prettier/prettier */
import { Response } from 'mappersmith'
import { types } from 'avsc'

import { encode, MAGIC_BYTE } from './encoder'
import decode from './decoder'
Expand All @@ -14,7 +16,7 @@ import {
ConfluentSchemaRegistryArgumentError,
ConfluentSchemaRegistryCompatibilityError,
} from './errors'
import { Schema, RawSchema } from './@types'
import { RawSchema, Schema, SchemaReference } from './@types'

interface RegisteredSchema {
id: number
Expand Down Expand Up @@ -102,10 +104,21 @@ export default class SchemaRegistry {
}

const response = await this.getSchemaOriginRequest(registryId)
const foundSchema: { schema: string } = response.data()
const foundSchema: { schema: string; references?: Array<SchemaReference> } = response.data()
const rawSchema: RawSchema = JSON.parse(foundSchema.schema)

return this.cache.setSchema(registryId, rawSchema)
let logicalTypes: Record<string, new () => types.LogicalType> | undefined
if (foundSchema.references) {
logicalTypes = Object.fromEntries(
await Promise.all(
foundSchema.references.map(async ({ name, subject, version }) => {
const schemaType = await this.getSchema(await this.getRegistryId(subject, version))

return [ name, schemaType ];
}),
),
)
}
return this.cache.setSchema(registryId, rawSchema, logicalTypes)
}

public async encode(registryId: number, jsonPayload: any): Promise<Buffer> {
Expand Down Expand Up @@ -140,8 +153,13 @@ export default class SchemaRegistry {
}

public async getRegistryId(subject: string, version: number | string): Promise<number> {
const cached = this.cache.getRegistryIdBySchemaRef({subject, version});
if (cached) {
return cached;
}
const response = await this.api.Subject.version({ subject, version })
const { id }: { id: number } = response.data()
this.cache.setRegistryIdBySchemaRef({subject, version}, id);

return id
}
Expand Down
46 changes: 42 additions & 4 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import avro, { ForSchemaOptions } from 'avsc'
import avro, { ForSchemaOptions, types } from 'avsc'

import { Schema, RawSchema } from './@types'
import { RawSchema, Schema, SchemaRef } from './@types'

export default class Cache {
registryIdBySubject: { [key: string]: number }
schemasByRegistryId: { [key: string]: Schema }
registryIdBySchemaRef: { [key: string]: number }
forSchemaOptions?: Partial<ForSchemaOptions>

constructor(forSchemaOptions?: Partial<ForSchemaOptions>) {
this.registryIdBySubject = {}
this.schemasByRegistryId = {}
this.registryIdBySchemaRef = {}
this.forSchemaOptions = forSchemaOptions
}

private schemaKeyGen = ({ subject, version }: SchemaRef): string => `${subject}:${version}`

getRegistryIdBySchemaRef = (schema: SchemaRef): number =>
this.registryIdBySchemaRef[this.schemaKeyGen(schema)]

setRegistryIdBySchemaRef = (schema: SchemaRef, registryId: number) => {
this.registryIdBySchemaRef[this.schemaKeyGen(schema)] = registryId

return this.registryIdBySchemaRef[this.schemaKeyGen(schema)]
}

getLatestRegistryId = (subject: string): number | undefined => this.registryIdBySubject[subject]

setLatestRegistryId = (subject: string, id: number): number => {
Expand All @@ -23,9 +36,34 @@ export default class Cache {

getSchema = (registryId: number): Schema => this.schemasByRegistryId[registryId]

setSchema = (registryId: number, schema: RawSchema) => {
setSchema = (
registryId: number,
schema: RawSchema,
logicalTypesExtra: Record<string, new () => types.LogicalType> = {},
) => {
// @ts-ignore TODO: Fix typings for Schema...
this.schemasByRegistryId[registryId] = avro.Type.forSchema(schema, this.forSchemaOptions)
this.schemasByRegistryId[registryId] = avro.Type.forSchema(schema, {
...this.forSchemaOptions,
typeHook:
this.forSchemaOptions?.typeHook ||
function(attr, opts) {
if (typeof attr == 'string') {
if (attr in opts.logicalTypes) {
return (opts.logicalTypes[attr] as unknown) as avro.Type
}
// if we map this as 'namespace.type'.
const qualifiedName = `${opts.namespace}.${attr}`
if (qualifiedName in opts.logicalTypes) {
return (opts.logicalTypes[qualifiedName] as unknown) as avro.Type
}
}
return (undefined as unknown) as avro.Type
},
logicalTypes: {
...this.forSchemaOptions?.logicalTypes,
...logicalTypesExtra,
},
})

return this.schemasByRegistryId[registryId]
}
Expand Down