Skip to content

Commit

Permalink
fix serialization of enums
Browse files Browse the repository at this point in the history
  + fix partial Refactoring of (name of) integerDatatype constant
  • Loading branch information
dslmeinte committed Aug 31, 2023
1 parent b79a03e commit 7dd62da
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src-test/m3/key-generation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Deno.test("key generation", async (tctx) => {

assertEquals(form.key, "FormLanguage-Form")

const size = factory.property(form, "size").ofType(builtinPrimitives.intDatatype)
const size = factory.property(form, "size").ofType(builtinPrimitives.integerDatatype)
form.havingFeatures(size)

assertEquals(size.key, "FormLanguage-Form-size")
Expand Down
4 changes: 2 additions & 2 deletions src-test/m3/library-language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {builtinPrimitives} from "../../src/m3/builtins.ts"
const factory = new LanguageFactory("library", "1", hashingIdGen())
export const libraryLanguage = factory.language

const {intDatatype, stringDatatype} = builtinPrimitives
const {integerDatatype, stringDatatype} = builtinPrimitives

const library = factory.concept("Library", false)
const book = factory.concept("Book", false)
Expand All @@ -19,7 +19,7 @@ const library_books = factory.containment(library, "books").ofType(book).isMulti
library.havingFeatures(library_name, library_books)

const book_title = factory.property(book, "title").ofType(stringDatatype)
const book_pages = factory.property(book, "pages").ofType(intDatatype)
const book_pages = factory.property(book, "pages").ofType(integerDatatype)
const book_author = factory.reference(book, "author").ofType(writer)
book.havingFeatures(book_title, book_pages, book_author)

Expand Down
4 changes: 2 additions & 2 deletions src-utils/m3/ecore/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const deref = (typeDescriptor: string): string =>
: typeDescriptor


const {booleanDatatype, intDatatype, stringDatatype} = builtinPrimitives
const {booleanDatatype, integerDatatype, stringDatatype} = builtinPrimitives

/**
* Converts a parsed Ecore XML metamodel (file) to a {@link Language LIonCore/M3 instance}.
Expand Down Expand Up @@ -71,7 +71,7 @@ export const asLIonCoreLanguage = (ecoreXml: EcoreXml, version: string): Languag
case "ecore:EDataType http://www.eclipse.org/emf/2003/XMLType#//Boolean":
return booleanDatatype
case "ecore:EDataType http://www.eclipse.org/emf/2003/XMLType#//Int":
return intDatatype
return integerDatatype
default:
throw new Error(`don't know what to convert this EDataType ref. descriptor to: ${eDataType}`)
}
Expand Down
4 changes: 2 additions & 2 deletions src/m3/builtins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ lioncoreBuiltins.havingEntities(
)


type BuiltinPrimitive = string | boolean | number | Record<string, unknown>
type BuiltinPrimitive = string | boolean | number | Record<string, unknown> | Array<unknown>

const builtinPrimitives = {
stringDatatype,
booleanDatatype,
intDatatype: integerDatatype,
integerDatatype,
jsonDatatype
}

Expand Down
24 changes: 22 additions & 2 deletions src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ import {ConceptDeducer as _ConceptDeducer, ModelAPI} from "./api.ts"
import {MetaPointer, SerializationChunk, SerializedNode} from "./serialization.ts"
import {asIds} from "./functions.ts"
import {Node} from "./types.ts"
import {Containment, isINamed, Language, Property, Reference} from "./m3/types.ts"
import {
Containment,
Enumeration,
EnumerationLiteral,
isINamed,
Language,
PrimitiveType,
Property,
Reference
} from "./m3/types.ts"
import {allFeaturesOf} from "./m3/functions.ts"
import {asArray} from "./utils/array-helpers.ts"
import {BuiltinPrimitive, lioncoreBuiltins, serializeBuiltin} from "./m3/builtins.ts"
Expand Down Expand Up @@ -56,9 +65,20 @@ export const serializeNodes = <NT extends Node>(nodes: NT[], api: ModelAPI<NT>):
key: feature.key
}
if (feature instanceof Property && value !== undefined) {
const encodedValue = (() => {
// (could also just inspect type of value:)
if (feature.type instanceof PrimitiveType) {
return serializeBuiltin(value as BuiltinPrimitive)
}
if (feature.type instanceof Enumeration) {
return (value as EnumerationLiteral).key
}
return null
})()
if (encodedValue !== null)
serializedNode.properties.push({
property: featureMetaPointer,
value: serializeBuiltin(value as BuiltinPrimitive)
value: encodedValue
})
return
}
Expand Down

0 comments on commit 7dd62da

Please sign in to comment.