Skip to content

Commit

Permalink
Fix so that enum member values are actually numbers (#909)
Browse files Browse the repository at this point in the history
* Fix so that enum member values are actually numbers

* Pretty
  • Loading branch information
Allon-Guralnek authored Jan 24, 2024
1 parent 7730c84 commit f36ce2f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions ts/.prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ src/model/endpoint.test.ts
src/pbModel/statement.ts
src/model/statement.test.ts
src/model/field.test.ts
src/model/enum.test.ts
2 changes: 1 addition & 1 deletion ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@anz-bank/sysl",
"version": "2.2.1",
"version": "2.2.2",
"description": "Sysl (pronounced \"sizzle\") is a open source system specification language.",
"author": "ANZ Bank",
"publisher": "anz-bank",
Expand Down
15 changes: 15 additions & 0 deletions ts/src/model/enum.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { realign } from "../common/format";
import { Enum } from "./enum";
import { Model } from "./model";
import "jest-extended";

test.concurrent("Enum value is number", async () => {
const model = await Model.fromText(realign(`
App:
!enum Enum:
A: 5
`));
const enumVal = (model.getApp("App").children[0] as Enum).children[0].value;
expect(enumVal).toBeNumber();
expect(enumVal).toBe(5);
});
9 changes: 6 additions & 3 deletions ts/src/pbModel/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ export class PbTypeDefUnion {

@jsonObject
export class PbTypeDefEnum {
@jsonMapMember(String, Number, serializerFor(String))
items!: Map<string, number>;
@jsonMapMember(String, String, serializerFor(String))
items!: Map<string, string>;
}

@jsonObject
Expand All @@ -141,6 +141,7 @@ export class PbTypeDef {
@jsonArrayMember(PbTypeConstraint) constraint?: PbTypeConstraint[];
@jsonMember opt!: boolean;
@jsonArrayMember(Location) sourceContexts!: Location[];

@jsonMember enum?: PbTypeDefEnum;
@jsonMember tuple?: PbTypeDefStruct;
// FIXME: Defaults to an empty map even if not defined in the deserialized source.
Expand Down Expand Up @@ -199,7 +200,9 @@ export class PbTypeDef {
if (type) return new Type(name, !!this.relation, PbTypeDef.defsToFields(type.attrDefs, parentRef), params);
if (this.enum) {
// Original order of enum items is not serialized, so assume value (number) order, since it's most common.
const values = [...this.enum.items].map(([k, v]) => new EnumValue(k, v)).sort((a, b) => a.value - b.value);
const values = [...this.enum.items]
.map(([k, v]) => new EnumValue(k, Number(v)))
.sort((a, b) => a.value - b.value);
return new Enum(name, values, params);
}
if (this.oneOf) {
Expand Down

0 comments on commit f36ce2f

Please sign in to comment.