Unify identical abstracts over different typedefs

Adobe spits out 2 possible JSON formats for a file I’m using, one uses full names but the other uses abbreviations. but I can use some fuckery to rename the fields to something more readable.

private typedef RawAnimFileDataShort =
{
	AN:RawAnimationDataShort,
	MD:RawMetaDataShort,
	SD:Null<RawSymbolDictionaryDataShort>
}

abstract AnimFileDataShort(RawAnimFileDataShort) from RawAnimFileDataShort
{
	public var animation(get, never):AnimationDataShort;
	public var symbols(get, never):Null<SymbolDictionaryDataShort>;
	public var metadata(get, never):MetadataShort;
	inline function get_animation() return this.AN;
	inline function get_symbols() return this.SD;
	inline function get_metadata() return this.MD;
}

private typedef RawAnimFileDataLong =
{
	ANIMATION:RawAnimationDataLong,
	metadata:RawMetaDataLong,
	SYMBOL_DICTIONARY:Null<RawSymbolDictionaryDataLong>
}

abstract AnimFileDataLong(RawAnimFileDataLong) from RawAnimFileDataLong
{
	public var animation(get, never):AnimationDataLong;
	public var symbols(get, never):Null<SymbolDictionaryDataLong>;
	public var metadata(get, never):MetadataLong;
	inline function get_animation() return this.ANIMATION;
	inline function get_symbols() return this.SYMBOL_DICTIONARY;
	inline function get_metadata() return this.metadata;
}

Is there some way to unify these types so I can traverse either format without needing to know which format i’m actually dealing with, assuming the abstracts all have the same fields?

1 Like

You can use @:resolve or @:op(a.b) metadata in your abstract. Take a look at this Haxe test class that implements runtime and compile time versions.

You can use a @:from directive to convert one type into another

3 Likes

Kevin’s answer is the correct one, I misunderstood your goals. Check out this https://try.haxe.org/#74dd8b05 implementing Kevin’s answer allowing two different typedefs to be assigned to a single unifying abstract type.

3 Likes