How to use the avsc.types function in avsc

To help you get started, we’ve selected a few avsc examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mtth / avsc / js / index.js View on Github external
function(path, any, type) {
              if (
                typeof any == 'string' &&
                ( 
                  type instanceof avsc.types.BytesType ||
                  (
                    type instanceof avsc.types.FixedType &&
                    any.length === type.getSize()
                  )
                )
              ) {
                // This is a string-encoded buffer.
                return;
              }
              throw new Error('invalid ' + type + ' at ' + path.join('.'));
            }
          });
github eHealthAfrica / aether / aether-ui / aether / ui / assets / apps / utils / avro-utils.jsx View on Github external
avro.types.UnwrappedUnionType.prototype.random = function () {
  const types = this.types.filter(({ typeName }) => typeName !== NULL)
  if (types.length === 0) {
    return null
  }
  const index = Math.floor(Math.random() * types.length)
  return types[index].random()
}

const randomBytes = (len = 8) => Math.floor((1 + Math.random()) * 16 ** len).toString(16).slice(1)

// In-house workaround to the avsc library to avoid buffer serialization
avro.types.FixedType.prototype.random = function () {
  return randomBytes(this.size)
}
avro.types.BytesType.prototype.random = function () {
  return randomBytes()
}

export const parseSchema = (schema) => (
  avro.parse(schema, { noAnonymousTypes: true, wrapUnions: false })
)

export const randomInput = (schema) => {
  const type = parseSchema(schema)
  const input = type.random()

  // check if there is a string "id" field
  if (schema.fields.find(field => field.name === FIELD_ID && field.type === 'string')) {
    input[FIELD_ID] = generateGUID() // make it more UUID
  }
  return input
github eHealthAfrica / aether / aether-ui / aether / ui / assets / apps / utils / avro-utils.jsx View on Github external
// In-house workaround to the avsc library to avoid null values
// in case of union types
avro.types.UnwrappedUnionType.prototype.random = function () {
  const types = this.types.filter(({ typeName }) => typeName !== NULL)
  if (types.length === 0) {
    return null
  }
  const index = Math.floor(Math.random() * types.length)
  return types[index].random()
}

const randomBytes = (len = 8) => Math.floor((1 + Math.random()) * 16 ** len).toString(16).slice(1)

// In-house workaround to the avsc library to avoid buffer serialization
avro.types.FixedType.prototype.random = function () {
  return randomBytes(this.size)
}
avro.types.BytesType.prototype.random = function () {
  return randomBytes()
}

export const parseSchema = (schema) => (
  avro.parse(schema, { noAnonymousTypes: true, wrapUnions: false })
)

export const randomInput = (schema) => {
  const type = parseSchema(schema)
  const input = type.random()

  // check if there is a string "id" field
  if (schema.fields.find(field => field.name === FIELD_ID && field.type === 'string')) {
github mtth / avsc / js / index.js View on Github external
function(path, any, type) {
              if (
                typeof any == 'string' &&
                ( 
                  type instanceof avsc.types.BytesType ||
                  (
                    type instanceof avsc.types.FixedType &&
                    any.length === type.getSize()
                  )
                )
              ) {
                // This is a string-encoded buffer.
                return;
              }
              throw new Error('invalid ' + type + ' at ' + path.join('.'));
            }
          });
github mtth / avsc / js / meta.js View on Github external
var util = require('util'),
    avro = require('avsc');
function MetaType(attr, opts) {
  avro.types.LogicalType.call(this, attr, opts, [avro.types.RecordType]);
}

util.inherits(MetaType, avro.types.LogicalType);

MetaType.prototype._fromValue = function(val) {
  var obj = val.value;
  return obj[Object.keys(obj)[0]];
}

MetaType.prototype._toValue = function(any) {
  var obj;
  if(typeof any == 'string') {
    if (~primitiveSymbols.indexOf(any)) {
      // Handling primitive names separately from references lets us save a
      // significant amount of space (1 byte per type name instead of 5-8).
      obj = { PrimitiveType: any};
    } else {
      obj = {string: any};
    }
github eHealthAfrica / aether / aether-ui / aether / ui / assets / apps / utils / avro-utils.jsx View on Github external
'float',
  'double',
  'bytes',
  'string',

  // these ones are not primitives but work the same
  // {"type": {"type": "enum", "name": "e", "symbols": ["A", "B", "C", "D"]}}
  'enum',
  // {"type": {"type": "fixed", "size": 16, "name": "f"}}
  'fixed'
]
export const FIELD_ID = 'id'

// In-house workaround to the avsc library to avoid null values
// in case of union types
avro.types.UnwrappedUnionType.prototype.random = function () {
  const types = this.types.filter(({ typeName }) => typeName !== NULL)
  if (types.length === 0) {
    return null
  }
  const index = Math.floor(Math.random() * types.length)
  return types[index].random()
}

const randomBytes = (len = 8) => Math.floor((1 + Math.random()) * 16 ** len).toString(16).slice(1)

// In-house workaround to the avsc library to avoid buffer serialization
avro.types.FixedType.prototype.random = function () {
  return randomBytes(this.size)
}
avro.types.BytesType.prototype.random = function () {
  return randomBytes()
github mtth / avsc / js / meta.js View on Github external
function MetaType(attr, opts) {
  avro.types.LogicalType.call(this, attr, opts, [avro.types.RecordType]);
}

avsc

Avro for JavaScript

MIT
Latest version published 1 year ago

Package Health Score

69 / 100
Full package analysis