Skip to content

rjotanm/ext-unionize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Externally Unionize

This package provide extension for original unionize package, for create Externally tagged representation.

Externally tagged enum representation provide by default via Rust Serde and for Python Pydantic via typenum library.

Dependency from unionize unnecessary, but it small and not affect to compiled code (when not used), therefore insert into dependency intentionally.

Compatability with Python\Rust provided representation described in typenum package.

Example

Internally (original unionize)

import { unionize, ofType } from 'unionize';

const MyEnym = unionize({
  First: ofType<{ id: number; text: string }>(),
  Second: ofType<{ id: number; }>(),
});

// {"tag":"First","id":1,"text":"test"}
console.log(JSON.stringify(MyEnum.First({ id: 1, text: "test" })))

Adjacently (original unionize)

import { unionize, ofType } from 'unionize';

const MyEnym = unionize({
  First: ofType<{ id: number; text: string }>(),
  Second: ofType<{ id: number; }>(),
}, {tag: "tag", value: "value"});

// {"tag":"First","value":{"id":1,"text":"test"}}
console.log(JSON.stringify(MyEnum.First({ id: 1, text: "test" })))

Externally

import { ofType } from 'unionize';
import { extUnionize } from 'ext-unionize';

const MyEnym = extUnionize({
  First: ofType<{ id: number; text: string }>(),
  Second: ofType<{ id: number; }>(),
});

// {"First":{"id":1,"text":"test"}}
console.log(JSON.stringify(MyEnum.First({ id: 1, text: "test" })))

About

Extension that provide externally tagged representation for unionize.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published