Safe Haskell | None |
---|---|
Language | Haskell2010 |
Avro Schema
s, represented here as values of type Schema
,
describe the serialization and de-serialization of values.
In Avro schemas are compose-able such that encoding data under a schema and
decoding with a variant, such as newer or older version of the original
schema, can be accomplished by using the Deconflict
module.
Synopsis
- type Schema = Type
- data Type
- data Field = Field {}
- data Order
- newtype TypeName = TN {}
- mkEnum :: TypeName -> [TypeName] -> Maybe Text -> Maybe Text -> [Text] -> Type
- mkUnion :: NonEmpty Type -> Type
- validateSchema :: Schema -> Parser ()
- typeName :: Type -> Text
- buildTypeEnvironment :: Applicative m => (TypeName -> m Type) -> Type -> TypeName -> m Type
- data Result a
- resultToEither :: Result b -> Either String b
- matches :: Type -> Type -> Bool
- parseBytes :: Text -> Result ByteString
- serializeBytes :: ByteString -> Text
- parseAvroJSON :: (Type -> Value -> Result (Value Type)) -> (Text -> Maybe Type) -> Type -> Value -> Result (Value Type)
Schema description types
An Avro schema is either * A "JSON object in the form `{"type":"typeName" ...` * A "JSON string, naming a defined type" (basic type wo free variablesnames) * A "JSON array, representing a union"
N.B. It is possible to create a Haskell value (of Schema type) that is
not a valid Avro schema by violating one of the above or one of the
conditions called out in validateSchema
.
Avro types are considered either primitive (string, int, etc) or complex/declared (structures, unions etc).
Null | |
Boolean | |
Int | |
Long | |
Float | |
Double | |
Bytes | |
String | |
Array | |
Map | |
NamedType TypeName | |
Record | |
Enum | |
Union | |
Fixed | |
Instances
Eq TypeName Source # | |
Ord TypeName Source # | |
Defined in Data.Avro.Schema | |
Show TypeName Source # | |
IsString TypeName Source # | |
Defined in Data.Avro.Schema fromString :: String -> TypeName # | |
Semigroup TypeName Source # | |
Monoid TypeName Source # | |
Hashable TypeName Source # | |
Defined in Data.Avro.Schema | |
ToJSON TypeName Source # | |
Defined in Data.Avro.Schema | |
FromJSON TypeName Source # | |
mkEnum :: TypeName -> [TypeName] -> Maybe Text -> Maybe Text -> [Text] -> Type Source #
mkEnum name aliases namespace docs syms
Constructs an Enum
schema using
the enumeration type's name, aliases (if any), namespace, documentation, and list of
symbols that inhabit the enumeration.
mkUnion :: NonEmpty Type -> Type Source #
mkUnion subTypes
Defines a union of the provided subTypes. N.B. it is
invalid Avro to include another union or to have more than one of the same
type as a direct member of the union. No check is done for this condition!
validateSchema :: Schema -> Parser () Source #
Placeholder NO-OP function!
Validates a schema to ensure:
- All types are defined
- Unions do not directly contain other unions
- Unions are not ambiguous (may not contain more than one schema with the same type except for named types of record, fixed and enum)
- Default values for unions can be cast as the type indicated by the first structure.
- Default values can be cast/de-serialize correctly.
- Named types are resolvable
Lower level utilities
typeName :: Type -> Text Source #
Get the name of the type. In the case of unions, get the name of the first value in the union schema.
buildTypeEnvironment :: Applicative m => (TypeName -> m Type) -> Type -> TypeName -> m Type Source #
buildTypeEnvironment schema
builds a function mapping type names to
the types declared in the traversed schema. Notice this function does not
currently handle namespaces in a correct manner, possibly allowing
for bad environment lookups when used on complex schemas.
Instances
matches :: Type -> Type -> Bool Source #
Checks that two schemas match. This is like equality of schemas,
except NamedTypes
match against other types with the same name.
This extends recursively: two records match if they have the same name, the same number of fields and the fields all match.
parseBytes :: Text -> Result ByteString Source #
Parses a string literal into a bytestring in the format expected for bytes and fixed values. Will fail if every character does not have a codepoint between 0 and 255.
serializeBytes :: ByteString -> Text Source #
Turn a ByteString
into a Text
that matches the format Avro
expects from bytes and fixed literals in JSON. Each byte is mapped
to a single Unicode codepoint between 0 and 255.
:: (Type -> Value -> Result (Value Type)) | How to handle unions. The way unions are formatted in JSON depends on whether we're parsing a normal Avro object or we're parsing a default declaration in a schema. This function will only ever be passed |
-> (Text -> Maybe Type) | |
-> Type | |
-> Value | |
-> Result (Value Type) |
Parse JSON-encoded avro data.