JS codegen: Add tests and make more things work (#1182)

Now the generated code is tested and most things work.



To generate a .noms.js file use the codegen go binary



Things that are not complete/needs improvements include:

- Recursive structs

- Structs with annonymous unions

- Enum -- constants for the values



Issue #1081
This commit is contained in:
Erik Arvidsson
2016-04-07 14:11:40 -07:00
parent b85ce637b5
commit b79d89963f
29 changed files with 638 additions and 191 deletions

View File

@@ -30,6 +30,11 @@ before_script:
- ./build.py
- npm test
- popd
- pushd nomdl/codegen/test
- npm prune
- npm install
- npm test
- popd
script:
- export GODEBUG=invalidptr=0
- export GO15VENDOREXPERIMENT=1
@@ -47,3 +52,4 @@ cache:
- clients/crunchbase/ui/node_modules
- clients/pitchmap/ui/node_modules
- clients/splore/node_modules
- nomdl/codegen/test/node_modules

View File

@@ -1,10 +1,10 @@
{{if .HasTypes}}
{
const pkg = new {{importJs "Package"}}([{{range $i, $t := .Types}}
{{toTypesTypeJS $t "" "_noms" 3}},{{end}}
], [{{range $deps := .Dependencies}}
{{importJs "Ref"}}.parse('{{$deps}}'),{{end}}
]);
{{importJs "registerPackage"}}(pkg);
}
const _pkg = new {{importJs "Package"}}([{{range $i, $t := .Types}}
{{toTypesTypeJS $t "" "_noms" 3}},{{end}}
], [{{range $deps := .Dependencies}}
{{importJs "Ref"}}.parse('{{$deps}}'),{{end}}
]);
{{importJs "registerPackage"}}(_pkg);{{range $i, $t := .Types}}
const {{userType $t}}$type = {{importJs "makeType"}}(_pkg.ref, {{$i}});
const {{userType $t}}$typeDef = _pkg.types[{{$i}}];{{end}}
{{end}}

View File

@@ -1,7 +1,14 @@
{{$name := .Name}}
export interface {{.Name}} extends {{importJsType "Struct"}} {{"{"}}{{range $field := .Fields}}
type {{.Name}}$Data = {{"{"}}{{range $field := .Fields}}
{{.Name}}{{if .Optional}}?{{end}}: {{userTypeJS .T}};{{end}}
};
interface {{.Name}}$Interface extends {{importJsType "Struct"}} {
constructor(data: {{.Name}}$Data): void;{{range $field := .Fields}}
{{.Name}}: {{if .Optional}}?{{end}}{{userTypeJS .T}}; // readonly
set{{title .Name}}(value: {{if .Optional}}?{{end}}{{userTypeJS .T}}): {{$name}};{{end}}{{range $field := .Choices}}
set{{title .Name}}(value: {{if .Optional}}?{{end}}{{userTypeJS .T}}): {{$name}}$Interface;{{end}}{{range $field := .Choices}}
{{.Name}}: ?{{userTypeJS .T}}; // readonly
set{{title .Name}}(value: {{userTypeJS .T}}): {{$name}};{{end}}
set{{title .Name}}(value: {{userTypeJS .T}}): {{$name}}$Interface;{{end}}
}
export const {{.Name}}: Class<{{.Name}}$Interface> = {{importJs "createStructClass"}}({{.Name}}$type, {{.Name}}$typeDef);

1
nomdl/codegen/test/.babelrc Symbolic link
View File

@@ -0,0 +1 @@
../../../js/.babelrc

View File

@@ -0,0 +1 @@
../../../js/.eslintrc

View File

@@ -0,0 +1 @@
../../../js/.flowconfig

1
nomdl/codegen/test/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
node_modules

View File

@@ -0,0 +1,17 @@
// @flow
import {assert} from 'chai';
import {suite, test} from 'mocha';
import {EnumStruct} from './gen/enum_struct.noms.js';
import type {Handedness} from './gen/enum_struct.noms.js';
suite('enum_struct.noms', () => {
test('constructor', async () => {
const es = new EnumStruct({hand: 0});
assert.equal(es.hand, 0);
const hand: Handedness = es.hand;
assert.equal(es.hand, hand);
const es2 = es.setHand(1);
assert.equal(es2.hand, 1);
});
});

View File

@@ -6,6 +6,7 @@ import {
Field as _Field,
Package as _Package,
Ref as _Ref,
createStructClass as _createStructClass,
makeEnumType as _makeEnumType,
makeStructType as _makeStructType,
makeType as _makeType,
@@ -15,10 +16,9 @@ import type {
Struct as _Struct,
} from '@attic/noms';
{
const pkg = new _Package([
_makeEnumType('Handedness', 'right', 'left', 'switch'),
_makeStructType('EnumStruct',
const _pkg = new _Package([
_makeEnumType('Handedness', 'right', 'left', 'switch'),
_makeStructType('EnumStruct',
[
new _Field('hand', _makeType(new _Ref(), 0), false),
],
@@ -26,10 +26,13 @@ import type {
]
),
], [
]);
_registerPackage(pkg);
}
], [
]);
_registerPackage(_pkg);
const Handedness$type = _makeType(_pkg.ref, 0);
const Handedness$typeDef = _pkg.types[0];
const EnumStruct$type = _makeType(_pkg.ref, 1);
const EnumStruct$typeDef = _pkg.types[1];
export type Handedness =
@@ -37,7 +40,14 @@ export type Handedness =
1 | // left
2; // switch
export interface EnumStruct extends _Struct {
type EnumStruct$Data = {
hand: Handedness;
};
interface EnumStruct$Interface extends _Struct {
constructor(data: EnumStruct$Data): void;
hand: Handedness; // readonly
setHand(value: Handedness): EnumStruct;
setHand(value: Handedness): EnumStruct$Interface;
}
export const EnumStruct: Class<EnumStruct$Interface> = _createStructClass(EnumStruct$type, EnumStruct$typeDef);

View File

@@ -6,9 +6,11 @@ import {
Field as _Field,
Kind as _Kind,
Package as _Package,
createStructClass as _createStructClass,
float32Type as _float32Type,
makeCompoundType as _makeCompoundType,
makeStructType as _makeStructType,
makeType as _makeType,
registerPackage as _registerPackage,
} from '@attic/noms';
import type {
@@ -18,9 +20,8 @@ import type {
float32 as _float32,
} from '@attic/noms';
{
const pkg = new _Package([
_makeStructType('StructWithRef',
const _pkg = new _Package([
_makeStructType('StructWithRef',
[
new _Field('r', _makeCompoundType(_Kind.Ref, _makeCompoundType(_Kind.Set, _float32Type)), false),
],
@@ -28,13 +29,21 @@ import type {
]
),
], [
]);
_registerPackage(pkg);
}
], [
]);
_registerPackage(_pkg);
const StructWithRef$type = _makeType(_pkg.ref, 0);
const StructWithRef$typeDef = _pkg.types[0];
export interface StructWithRef extends _Struct {
type StructWithRef$Data = {
r: _RefValue<_NomsSet<_float32>>;
};
interface StructWithRef$Interface extends _Struct {
constructor(data: StructWithRef$Data): void;
r: _RefValue<_NomsSet<_float32>>; // readonly
setR(value: _RefValue<_NomsSet<_float32>>): StructWithRef;
setR(value: _RefValue<_NomsSet<_float32>>): StructWithRef$Interface;
}
export const StructWithRef: Class<StructWithRef$Interface> = _createStructClass(StructWithRef$type, StructWithRef$typeDef);

View File

@@ -6,8 +6,10 @@ import {
Field as _Field,
Package as _Package,
boolType as _boolType,
createStructClass as _createStructClass,
makeEnumType as _makeEnumType,
makeStructType as _makeStructType,
makeType as _makeType,
registerPackage as _registerPackage,
stringType as _stringType,
} from '@attic/noms';
@@ -15,9 +17,8 @@ import type {
Struct as _Struct,
} from '@attic/noms';
{
const pkg = new _Package([
_makeStructType('S',
const _pkg = new _Package([
_makeStructType('S',
[
new _Field('s', _stringType, false),
new _Field('b', _boolType, false),
@@ -26,20 +27,31 @@ import type {
]
),
_makeEnumType('E', 'e1', 'e2', 'e3'),
], [
]);
_registerPackage(pkg);
}
_makeEnumType('E', 'e1', 'e2', 'e3'),
], [
]);
_registerPackage(_pkg);
const S$type = _makeType(_pkg.ref, 0);
const S$typeDef = _pkg.types[0];
const E$type = _makeType(_pkg.ref, 1);
const E$typeDef = _pkg.types[1];
export interface S extends _Struct {
type S$Data = {
s: string;
b: boolean;
};
interface S$Interface extends _Struct {
constructor(data: S$Data): void;
s: string; // readonly
setS(value: string): S;
setS(value: string): S$Interface;
b: boolean; // readonly
setB(value: boolean): S;
setB(value: boolean): S$Interface;
}
export const S: Class<S$Interface> = _createStructClass(S$type, S$typeDef);
export type E =
0 | // e1
1 | // e2

View File

@@ -7,8 +7,10 @@ import {
Kind as _Kind,
Package as _Package,
blobType as _blobType,
createStructClass as _createStructClass,
makeCompoundType as _makeCompoundType,
makeStructType as _makeStructType,
makeType as _makeType,
registerPackage as _registerPackage,
} from '@attic/noms';
import type {
@@ -17,9 +19,8 @@ import type {
Struct as _Struct,
} from '@attic/noms';
{
const pkg = new _Package([
_makeStructType('A',
const _pkg = new _Package([
_makeStructType('A',
[
new _Field('A', _makeCompoundType(_Kind.List, _makeCompoundType(_Kind.List, _blobType)), false),
],
@@ -27,13 +28,21 @@ import type {
]
),
], [
]);
_registerPackage(pkg);
}
], [
]);
_registerPackage(_pkg);
const A$type = _makeType(_pkg.ref, 0);
const A$typeDef = _pkg.types[0];
export interface A extends _Struct {
type A$Data = {
A: _NomsList<_NomsList<_Blob>>;
};
interface A$Interface extends _Struct {
constructor(data: A$Data): void;
A: _NomsList<_NomsList<_Blob>>; // readonly
setA(value: _NomsList<_NomsList<_Blob>>): A;
setA(value: _NomsList<_NomsList<_Blob>>): A$Interface;
}
export const A: Class<A$Interface> = _createStructClass(A$type, A$typeDef);

View File

@@ -6,6 +6,7 @@ import {
Field as _Field,
Package as _Package,
Ref as _Ref,
createStructClass as _createStructClass,
makeStructType as _makeStructType,
makeType as _makeType,
registerPackage as _registerPackage,
@@ -16,9 +17,8 @@ import type {
import * as _sha1_1c216c6 from './sha1_1c216c6.js';
{
const pkg = new _Package([
_makeStructType('D',
const _pkg = new _Package([
_makeStructType('D',
[
new _Field('structField', _makeType(_Ref.parse('sha1-1c216c6f1d6989e4ede5f78b7689214948dabeef'), 0), false),
new _Field('enumField', _makeType(_Ref.parse('sha1-1c216c6f1d6989e4ede5f78b7689214948dabeef'), 1), false),
@@ -27,7 +27,7 @@ import * as _sha1_1c216c6 from './sha1_1c216c6.js';
]
),
_makeStructType('DUser',
_makeStructType('DUser',
[
new _Field('Dfield', _makeType(new _Ref(), 0), false),
],
@@ -35,21 +35,39 @@ import * as _sha1_1c216c6 from './sha1_1c216c6.js';
]
),
], [
_Ref.parse('sha1-1c216c6f1d6989e4ede5f78b7689214948dabeef'),
]);
_registerPackage(pkg);
}
], [
_Ref.parse('sha1-1c216c6f1d6989e4ede5f78b7689214948dabeef'),
]);
_registerPackage(_pkg);
const D$type = _makeType(_pkg.ref, 0);
const D$typeDef = _pkg.types[0];
const DUser$type = _makeType(_pkg.ref, 1);
const DUser$typeDef = _pkg.types[1];
export interface D extends _Struct {
type D$Data = {
structField: _sha1_1c216c6.S;
enumField: _sha1_1c216c6.E;
};
interface D$Interface extends _Struct {
constructor(data: D$Data): void;
structField: _sha1_1c216c6.S; // readonly
setStructField(value: _sha1_1c216c6.S): D;
setStructField(value: _sha1_1c216c6.S): D$Interface;
enumField: _sha1_1c216c6.E; // readonly
setEnumField(value: _sha1_1c216c6.E): D;
setEnumField(value: _sha1_1c216c6.E): D$Interface;
}
export interface DUser extends _Struct {
export const D: Class<D$Interface> = _createStructClass(D$type, D$typeDef);
type DUser$Data = {
Dfield: D;
};
interface DUser$Interface extends _Struct {
constructor(data: DUser$Data): void;
Dfield: D; // readonly
setDfield(value: D): DUser;
setDfield(value: D): DUser$Interface;
}
export const DUser: Class<DUser$Interface> = _createStructClass(DUser$type, DUser$typeDef);

View File

@@ -6,7 +6,9 @@ import {
Field as _Field,
Package as _Package,
boolType as _boolType,
createStructClass as _createStructClass,
makeStructType as _makeStructType,
makeType as _makeType,
registerPackage as _registerPackage,
stringType as _stringType,
} from '@attic/noms';
@@ -14,9 +16,8 @@ import type {
Struct as _Struct,
} from '@attic/noms';
{
const pkg = new _Package([
_makeStructType('Struct',
const _pkg = new _Package([
_makeStructType('Struct',
[
new _Field('s', _stringType, false),
new _Field('b', _boolType, false),
@@ -25,15 +26,24 @@ import type {
]
),
], [
]);
_registerPackage(pkg);
}
], [
]);
_registerPackage(_pkg);
const Struct$type = _makeType(_pkg.ref, 0);
const Struct$typeDef = _pkg.types[0];
export interface Struct extends _Struct {
type Struct$Data = {
s: string;
b: boolean;
};
interface Struct$Interface extends _Struct {
constructor(data: Struct$Data): void;
s: string; // readonly
setS(value: string): Struct;
setS(value: string): Struct$Interface;
b: boolean; // readonly
setB(value: boolean): Struct;
setB(value: boolean): Struct$Interface;
}
export const Struct: Class<Struct$Interface> = _createStructClass(Struct$type, Struct$typeDef);

View File

@@ -6,7 +6,9 @@ import {
Field as _Field,
Package as _Package,
boolType as _boolType,
createStructClass as _createStructClass,
makeStructType as _makeStructType,
makeType as _makeType,
registerPackage as _registerPackage,
stringType as _stringType,
} from '@attic/noms';
@@ -14,9 +16,8 @@ import type {
Struct as _Struct,
} from '@attic/noms';
{
const pkg = new _Package([
_makeStructType('OptionalStruct',
const _pkg = new _Package([
_makeStructType('OptionalStruct',
[
new _Field('s', _stringType, true),
new _Field('b', _boolType, true),
@@ -25,15 +26,24 @@ import type {
]
),
], [
]);
_registerPackage(pkg);
}
], [
]);
_registerPackage(_pkg);
const OptionalStruct$type = _makeType(_pkg.ref, 0);
const OptionalStruct$typeDef = _pkg.types[0];
export interface OptionalStruct extends _Struct {
type OptionalStruct$Data = {
s?: string;
b?: boolean;
};
interface OptionalStruct$Interface extends _Struct {
constructor(data: OptionalStruct$Data): void;
s: ?string; // readonly
setS(value: ?string): OptionalStruct;
setS(value: ?string): OptionalStruct$Interface;
b: ?boolean; // readonly
setB(value: ?boolean): OptionalStruct;
setB(value: ?boolean): OptionalStruct$Interface;
}
export const OptionalStruct: Class<OptionalStruct$Interface> = _createStructClass(OptionalStruct$type, OptionalStruct$typeDef);

View File

@@ -7,6 +7,7 @@ import {
Package as _Package,
blobType as _blobType,
boolType as _boolType,
createStructClass as _createStructClass,
float32Type as _float32Type,
float64Type as _float64Type,
int16Type as _int16Type,
@@ -14,6 +15,7 @@ import {
int64Type as _int64Type,
int8Type as _int8Type,
makeStructType as _makeStructType,
makeType as _makeType,
registerPackage as _registerPackage,
stringType as _stringType,
uint16Type as _uint16Type,
@@ -38,9 +40,8 @@ import type {
uint8 as _uint8,
} from '@attic/noms';
{
const pkg = new _Package([
_makeStructType('StructPrimitives',
const _pkg = new _Package([
_makeStructType('StructPrimitives',
[
new _Field('uint64', _uint64Type, false),
new _Field('uint32', _uint32Type, false),
@@ -61,39 +62,60 @@ import type {
]
),
], [
]);
_registerPackage(pkg);
}
], [
]);
_registerPackage(_pkg);
const StructPrimitives$type = _makeType(_pkg.ref, 0);
const StructPrimitives$typeDef = _pkg.types[0];
export interface StructPrimitives extends _Struct {
type StructPrimitives$Data = {
uint64: _uint64;
uint32: _uint32;
uint16: _uint16;
uint8: _uint8;
int64: _int64;
int32: _int32;
int16: _int16;
int8: _int8;
float64: _float64;
float32: _float32;
bool: boolean;
string: string;
blob: _Blob;
value: _Value;
};
interface StructPrimitives$Interface extends _Struct {
constructor(data: StructPrimitives$Data): void;
uint64: _uint64; // readonly
setUint64(value: _uint64): StructPrimitives;
setUint64(value: _uint64): StructPrimitives$Interface;
uint32: _uint32; // readonly
setUint32(value: _uint32): StructPrimitives;
setUint32(value: _uint32): StructPrimitives$Interface;
uint16: _uint16; // readonly
setUint16(value: _uint16): StructPrimitives;
setUint16(value: _uint16): StructPrimitives$Interface;
uint8: _uint8; // readonly
setUint8(value: _uint8): StructPrimitives;
setUint8(value: _uint8): StructPrimitives$Interface;
int64: _int64; // readonly
setInt64(value: _int64): StructPrimitives;
setInt64(value: _int64): StructPrimitives$Interface;
int32: _int32; // readonly
setInt32(value: _int32): StructPrimitives;
setInt32(value: _int32): StructPrimitives$Interface;
int16: _int16; // readonly
setInt16(value: _int16): StructPrimitives;
setInt16(value: _int16): StructPrimitives$Interface;
int8: _int8; // readonly
setInt8(value: _int8): StructPrimitives;
setInt8(value: _int8): StructPrimitives$Interface;
float64: _float64; // readonly
setFloat64(value: _float64): StructPrimitives;
setFloat64(value: _float64): StructPrimitives$Interface;
float32: _float32; // readonly
setFloat32(value: _float32): StructPrimitives;
setFloat32(value: _float32): StructPrimitives$Interface;
bool: boolean; // readonly
setBool(value: boolean): StructPrimitives;
setBool(value: boolean): StructPrimitives$Interface;
string: string; // readonly
setString(value: string): StructPrimitives;
setString(value: string): StructPrimitives$Interface;
blob: _Blob; // readonly
setBlob(value: _Blob): StructPrimitives;
setBlob(value: _Blob): StructPrimitives$Interface;
value: _Value; // readonly
setValue(value: _Value): StructPrimitives;
setValue(value: _Value): StructPrimitives$Interface;
}
export const StructPrimitives: Class<StructPrimitives$Interface> = _createStructClass(StructPrimitives$type, StructPrimitives$typeDef);

View File

@@ -7,6 +7,7 @@ import {
Kind as _Kind,
Package as _Package,
Ref as _Ref,
createStructClass as _createStructClass,
makeCompoundType as _makeCompoundType,
makeStructType as _makeStructType,
makeType as _makeType,
@@ -17,9 +18,8 @@ import type {
Struct as _Struct,
} from '@attic/noms';
{
const pkg = new _Package([
_makeStructType('Tree',
const _pkg = new _Package([
_makeStructType('Tree',
[
new _Field('children', _makeCompoundType(_Kind.List, _makeType(new _Ref(), 0)), false),
],
@@ -27,13 +27,21 @@ import type {
]
),
], [
]);
_registerPackage(pkg);
}
], [
]);
_registerPackage(_pkg);
const Tree$type = _makeType(_pkg.ref, 0);
const Tree$typeDef = _pkg.types[0];
export interface Tree extends _Struct {
type Tree$Data = {
children: _NomsList<Tree>;
};
interface Tree$Interface extends _Struct {
constructor(data: Tree$Data): void;
children: _NomsList<Tree>; // readonly
setChildren(value: _NomsList<Tree>): Tree;
setChildren(value: _NomsList<Tree>): Tree$Interface;
}
export const Tree: Class<Tree$Interface> = _createStructClass(Tree$type, Tree$typeDef);

View File

@@ -6,8 +6,10 @@ import {
Field as _Field,
Kind as _Kind,
Package as _Package,
createStructClass as _createStructClass,
makeCompoundType as _makeCompoundType,
makeStructType as _makeStructType,
makeType as _makeType,
registerPackage as _registerPackage,
uint8Type as _uint8Type,
} from '@attic/noms';
@@ -17,9 +19,8 @@ import type {
uint8 as _uint8,
} from '@attic/noms';
{
const pkg = new _Package([
_makeStructType('StructWithDupList',
const _pkg = new _Package([
_makeStructType('StructWithDupList',
[
new _Field('l', _makeCompoundType(_Kind.List, _uint8Type), false),
],
@@ -27,13 +28,21 @@ import type {
]
),
], [
]);
_registerPackage(pkg);
}
], [
]);
_registerPackage(_pkg);
const StructWithDupList$type = _makeType(_pkg.ref, 0);
const StructWithDupList$typeDef = _pkg.types[0];
export interface StructWithDupList extends _Struct {
type StructWithDupList$Data = {
l: _NomsList<_uint8>;
};
interface StructWithDupList$Interface extends _Struct {
constructor(data: StructWithDupList$Data): void;
l: _NomsList<_uint8>; // readonly
setL(value: _NomsList<_uint8>): StructWithDupList;
setL(value: _NomsList<_uint8>): StructWithDupList$Interface;
}
export const StructWithDupList: Class<StructWithDupList$Interface> = _createStructClass(StructWithDupList$type, StructWithDupList$typeDef);

View File

@@ -6,6 +6,7 @@ import {
Field as _Field,
Package as _Package,
Ref as _Ref,
createStructClass as _createStructClass,
makeEnumType as _makeEnumType,
makeStructType as _makeStructType,
makeType as _makeType,
@@ -17,10 +18,9 @@ import type {
import * as dep from './sha1_eda4273.js';
{
const pkg = new _Package([
_makeEnumType('LocalE', 'LocalE1', 'Ignored'),
_makeStructType('ImportUser',
const _pkg = new _Package([
_makeEnumType('LocalE', 'LocalE1', 'Ignored'),
_makeStructType('ImportUser',
[
new _Field('importedStruct', _makeType(_Ref.parse('sha1-eda4273cba9d5d4a1bccf41bcaec64743863cde0'), 0), false),
new _Field('enum', _makeType(new _Ref(), 0), false),
@@ -29,20 +29,31 @@ import * as dep from './sha1_eda4273.js';
]
),
], [
_Ref.parse('sha1-eda4273cba9d5d4a1bccf41bcaec64743863cde0'),
]);
_registerPackage(pkg);
}
], [
_Ref.parse('sha1-eda4273cba9d5d4a1bccf41bcaec64743863cde0'),
]);
_registerPackage(_pkg);
const LocalE$type = _makeType(_pkg.ref, 0);
const LocalE$typeDef = _pkg.types[0];
const ImportUser$type = _makeType(_pkg.ref, 1);
const ImportUser$typeDef = _pkg.types[1];
export type LocalE =
0 | // LocalE1
1; // Ignored
export interface ImportUser extends _Struct {
type ImportUser$Data = {
importedStruct: dep.D;
enum: LocalE;
};
interface ImportUser$Interface extends _Struct {
constructor(data: ImportUser$Data): void;
importedStruct: dep.D; // readonly
setImportedStruct(value: dep.D): ImportUser;
setImportedStruct(value: dep.D): ImportUser$Interface;
enum: LocalE; // readonly
setEnum(value: LocalE): ImportUser;
setEnum(value: LocalE): ImportUser$Interface;
}
export const ImportUser: Class<ImportUser$Interface> = _createStructClass(ImportUser$type, ImportUser$typeDef);

View File

@@ -7,9 +7,11 @@ import {
Kind as _Kind,
Package as _Package,
boolType as _boolType,
createStructClass as _createStructClass,
int64Type as _int64Type,
makeCompoundType as _makeCompoundType,
makeStructType as _makeStructType,
makeType as _makeType,
registerPackage as _registerPackage,
stringType as _stringType,
uint8Type as _uint8Type,
@@ -21,9 +23,8 @@ import type {
uint8 as _uint8,
} from '@attic/noms';
{
const pkg = new _Package([
_makeStructType('StructWithList',
const _pkg = new _Package([
_makeStructType('StructWithList',
[
new _Field('l', _makeCompoundType(_Kind.List, _uint8Type), false),
new _Field('b', _boolType, false),
@@ -34,19 +35,30 @@ import type {
]
),
], [
]);
_registerPackage(pkg);
}
], [
]);
_registerPackage(_pkg);
const StructWithList$type = _makeType(_pkg.ref, 0);
const StructWithList$typeDef = _pkg.types[0];
export interface StructWithList extends _Struct {
type StructWithList$Data = {
l: _NomsList<_uint8>;
b: boolean;
s: string;
i: _int64;
};
interface StructWithList$Interface extends _Struct {
constructor(data: StructWithList$Data): void;
l: _NomsList<_uint8>; // readonly
setL(value: _NomsList<_uint8>): StructWithList;
setL(value: _NomsList<_uint8>): StructWithList$Interface;
b: boolean; // readonly
setB(value: boolean): StructWithList;
setB(value: boolean): StructWithList$Interface;
s: string; // readonly
setS(value: string): StructWithList;
setS(value: string): StructWithList$Interface;
i: _int64; // readonly
setI(value: _int64): StructWithList;
setI(value: _int64): StructWithList$Interface;
}
export const StructWithList: Class<StructWithList$Interface> = _createStructClass(StructWithList$type, StructWithList$typeDef);

View File

@@ -7,10 +7,12 @@ import {
Kind as _Kind,
Package as _Package,
blobType as _blobType,
createStructClass as _createStructClass,
float32Type as _float32Type,
float64Type as _float64Type,
makeCompoundType as _makeCompoundType,
makeStructType as _makeStructType,
makeType as _makeType,
registerPackage as _registerPackage,
stringType as _stringType,
uint8Type as _uint8Type,
@@ -26,9 +28,8 @@ import type {
uint8 as _uint8,
} from '@attic/noms';
{
const pkg = new _Package([
_makeStructType('StructWithUnionField',
const _pkg = new _Package([
_makeStructType('StructWithUnionField',
[
new _Field('a', _float32Type, false),
],
@@ -40,23 +41,31 @@ import type {
new _Field('f', _makeCompoundType(_Kind.Set, _uint8Type), false),
]
),
], [
]);
_registerPackage(pkg);
}
], [
]);
_registerPackage(_pkg);
const StructWithUnionField$type = _makeType(_pkg.ref, 0);
const StructWithUnionField$typeDef = _pkg.types[0];
export interface StructWithUnionField extends _Struct {
type StructWithUnionField$Data = {
a: _float32;
};
interface StructWithUnionField$Interface extends _Struct {
constructor(data: StructWithUnionField$Data): void;
a: _float32; // readonly
setA(value: _float32): StructWithUnionField;
setA(value: _float32): StructWithUnionField$Interface;
b: ?_float64; // readonly
setB(value: _float64): StructWithUnionField;
setB(value: _float64): StructWithUnionField$Interface;
c: ?string; // readonly
setC(value: string): StructWithUnionField;
setC(value: string): StructWithUnionField$Interface;
d: ?_Blob; // readonly
setD(value: _Blob): StructWithUnionField;
setD(value: _Blob): StructWithUnionField$Interface;
e: ?_Value; // readonly
setE(value: _Value): StructWithUnionField;
setE(value: _Value): StructWithUnionField$Interface;
f: ?_NomsSet<_uint8>; // readonly
setF(value: _NomsSet<_uint8>): StructWithUnionField;
setF(value: _NomsSet<_uint8>): StructWithUnionField$Interface;
}
export const StructWithUnionField: Class<StructWithUnionField$Interface> = _createStructClass(StructWithUnionField$type, StructWithUnionField$typeDef);

View File

@@ -6,6 +6,7 @@ import {
Field as _Field,
Package as _Package,
Ref as _Ref,
createStructClass as _createStructClass,
float64Type as _float64Type,
makeStructType as _makeStructType,
makeType as _makeType,
@@ -17,9 +18,8 @@ import type {
float64 as _float64,
} from '@attic/noms';
{
const pkg = new _Package([
_makeStructType('StructWithUnions',
const _pkg = new _Package([
_makeStructType('StructWithUnions',
[
new _Field('a', _makeType(new _Ref(), 1), false),
new _Field('d', _makeType(new _Ref(), 2), false),
@@ -28,7 +28,7 @@ import type {
]
),
_makeStructType('',
_makeStructType('',
[
],
@@ -37,7 +37,7 @@ import type {
new _Field('c', _stringType, false),
]
),
_makeStructType('',
_makeStructType('',
[
],
@@ -46,29 +46,54 @@ import type {
new _Field('f', _stringType, false),
]
),
], [
]);
_registerPackage(pkg);
}
], [
]);
_registerPackage(_pkg);
const StructWithUnions$type = _makeType(_pkg.ref, 0);
const StructWithUnions$typeDef = _pkg.types[0];
const __unionOfBOfFloat64AndCOfString$type = _makeType(_pkg.ref, 1);
const __unionOfBOfFloat64AndCOfString$typeDef = _pkg.types[1];
const __unionOfEOfFloat64AndFOfString$type = _makeType(_pkg.ref, 2);
const __unionOfEOfFloat64AndFOfString$typeDef = _pkg.types[2];
export interface StructWithUnions extends _Struct {
type StructWithUnions$Data = {
a: __unionOfBOfFloat64AndCOfString;
d: __unionOfEOfFloat64AndFOfString;
};
interface StructWithUnions$Interface extends _Struct {
constructor(data: StructWithUnions$Data): void;
a: __unionOfBOfFloat64AndCOfString; // readonly
setA(value: __unionOfBOfFloat64AndCOfString): StructWithUnions;
setA(value: __unionOfBOfFloat64AndCOfString): StructWithUnions$Interface;
d: __unionOfEOfFloat64AndFOfString; // readonly
setD(value: __unionOfEOfFloat64AndFOfString): StructWithUnions;
setD(value: __unionOfEOfFloat64AndFOfString): StructWithUnions$Interface;
}
export interface __unionOfBOfFloat64AndCOfString extends _Struct {
export const StructWithUnions: Class<StructWithUnions$Interface> = _createStructClass(StructWithUnions$type, StructWithUnions$typeDef);
type __unionOfBOfFloat64AndCOfString$Data = {
};
interface __unionOfBOfFloat64AndCOfString$Interface extends _Struct {
constructor(data: __unionOfBOfFloat64AndCOfString$Data): void;
b: ?_float64; // readonly
setB(value: _float64): __unionOfBOfFloat64AndCOfString;
setB(value: _float64): __unionOfBOfFloat64AndCOfString$Interface;
c: ?string; // readonly
setC(value: string): __unionOfBOfFloat64AndCOfString;
setC(value: string): __unionOfBOfFloat64AndCOfString$Interface;
}
export interface __unionOfEOfFloat64AndFOfString extends _Struct {
export const __unionOfBOfFloat64AndCOfString: Class<__unionOfBOfFloat64AndCOfString$Interface> = _createStructClass(__unionOfBOfFloat64AndCOfString$type, __unionOfBOfFloat64AndCOfString$typeDef);
type __unionOfEOfFloat64AndFOfString$Data = {
};
interface __unionOfEOfFloat64AndFOfString$Interface extends _Struct {
constructor(data: __unionOfEOfFloat64AndFOfString$Data): void;
e: ?_float64; // readonly
setE(value: _float64): __unionOfEOfFloat64AndFOfString;
setE(value: _float64): __unionOfEOfFloat64AndFOfString$Interface;
f: ?string; // readonly
setF(value: string): __unionOfEOfFloat64AndFOfString;
setF(value: string): __unionOfEOfFloat64AndFOfString$Interface;
}
export const __unionOfEOfFloat64AndFOfString: Class<__unionOfEOfFloat64AndFOfString$Interface> = _createStructClass(__unionOfEOfFloat64AndFOfString$type, __unionOfEOfFloat64AndFOfString$typeDef);

View File

@@ -0,0 +1,41 @@
{
"name": "nomdl-codegen-test",
"version": "0.0.1",
"description": "Tests for generated js code",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@attic/noms": "^7.6.0",
"babel-cli": "6.6.5",
"babel-core": "6.7.2",
"babel-eslint": "5.0.0",
"babel-generator": "6.7.2",
"babel-plugin-syntax-async-functions": "6.5.0",
"babel-plugin-syntax-flow": "6.5.0",
"babel-plugin-transform-async-to-generator": "6.7.0",
"babel-plugin-transform-class-properties": "6.6.0",
"babel-plugin-transform-es2015-destructuring": "6.6.5",
"babel-plugin-transform-es2015-modules-commonjs": "6.7.0",
"babel-plugin-transform-es2015-parameters": "6.7.0",
"babel-plugin-transform-runtime": "^6.6.0",
"babel-preset-es2015": "6.6.0",
"babel-preset-react": "6.5.0",
"chai": "3.5.0",
"chokidar": "1.4.3",
"commander": "2.9.0",
"eslint-plugin-flow-vars": "^0.2.1",
"eslint-plugin-react": "4.2.3",
"eslint": "^1.10.3",
"flow-bin": "0.22.1",
"fs-extra": "0.26.7",
"mocha": "2.4.5"
},
"scripts": {
"pretest": "eslint . && flow .",
"test": "mocha --ui tdd --reporter dot --compilers js:babel-core/register ./*-test.js"
}
}

View File

@@ -0,0 +1,22 @@
// @flow
import {assert} from 'chai';
import {suite, test} from 'mocha';
import {newSet, RefValue, makeSetType, float32Type, DataStore, MemoryStore} from '@attic/noms';
import type {NomsSet, float32} from '@attic/noms';
import {StructWithRef} from './gen/ref.noms.js';
suite('ref.noms', () => {
test('constructor', async () => {
const ds = new DataStore(new MemoryStore());
const set: NomsSet<float32> = await newSet([0, 1, 2, 3], makeSetType(float32Type));
const ref = ds.writeValue(set);
const r = new RefValue(ref);
const struct = new StructWithRef({r});
assert.isTrue(struct.r.equals(r));
const set2 = await ds.readValue(r.targetRef);
assert.isTrue(set.equals(set2));
});
});

View File

@@ -0,0 +1,85 @@
// @flow
import {assert} from 'chai'; //eslint-disable-line
import {suite, test} from 'mocha';
import {newBlob} from '@attic/noms';
import {StructPrimitives} from './gen/struct_primitives.noms.js';
suite('struct-primitives.noms', () => {
test('constructor', async () => {
const s: StructPrimitives = new StructPrimitives({ //eslint-disable-line
uint64: 1,
uint32: 2,
uint16: 3,
uint8: 4,
int64: 5,
int32: 6,
int16: 7,
int8: 8,
float64: 9,
float32: 10,
bool: true,
string: 'hi',
blob: await newBlob(new Uint8Array([0, 1, 2, 3])),
value: 123,
});
let s2;
assert.equal(s.uint64, 1);
s2 = s.setUint64(11);
assert.equal(s2.uint64, 11);
assert.equal(s.uint32, 2);
s2 = s.setUint32(22);
assert.equal(s2.uint32, 22);
assert.equal(s.uint16, 3);
s2 = s.setUint16(33);
assert.equal(s2.uint16, 33);
assert.equal(s.uint8, 4);
s2 = s.setUint8(44);
assert.equal(s2.uint8, 44);
assert.equal(s.int64, 5);
s2 = s.setInt64(55);
assert.equal(s2.int64, 55);
assert.equal(s.int32, 6);
s2 = s.setInt32(66);
assert.equal(s2.int32, 66);
assert.equal(s.int16, 7);
s2 = s.setInt16(77);
assert.equal(s2.int16, 77);
assert.equal(s.int8, 8);
s2 = s.setInt8(88);
assert.equal(s2.int8, 88);
assert.equal(s.float64, 9);
s2 = s.setFloat64(99);
assert.equal(s2.float64, 99);
assert.equal(s.float32, 10);
s2 = s.setFloat32(1010);
assert.equal(s2.float32, 1010);
assert.equal(s.bool, true);
s2 = s.setBool(false);
assert.equal(s2.bool, false);
assert.equal(s.string, 'hi');
s2 = s.setString('bye');
assert.equal(s2.string, 'bye');
assert.isTrue(s.blob.equals(await newBlob(new Uint8Array([0, 1, 2, 3]))));
s2 = s.setBlob(await newBlob(new Uint8Array([4, 5, 6, 7])));
assert.isTrue(s2.blob.equals(await newBlob(new Uint8Array([4, 5, 6, 7]))));
assert.equal(s.value, 123);
s2 = s.setValue('x');
assert.equal(s2.value, 'x');
});
});

View File

@@ -0,0 +1,20 @@
// @flow
import {assert} from 'chai';
import {suite, test} from 'mocha';
import {Kind} from '@attic/noms';
import {Struct} from './gen/struct.noms.js';
suite('struct.noms', () => {
test('constructor', () => {
const s: Struct = new Struct({s: 'hi', b: true});
assert.equal(s.s, 'hi');
assert.equal(s.b, true);
});
test('type', () => {
const s: Struct = new Struct({s: 'hi', b: true});
assert.equal(s.type.kind, Kind.Unresolved);
});
});

View File

@@ -0,0 +1,30 @@
// @flow
import {assert} from 'chai';
import {suite, test} from 'mocha';
import {StructWithUnionField} from './gen/struct_with_union_field.noms.js';
suite('struct_optional.noms', () => {
test('constructor', async () => {
const swuf = new StructWithUnionField({a: 1, b: 2});
assert.equal(swuf.a, 1);
assert.equal(swuf.b, 2);
assert.isUndefined(swuf.c);
assert.isUndefined(swuf.d);
assert.isUndefined(swuf.e);
assert.isUndefined(swuf.f);
const swuf2 = swuf.setC('hi');
assert.equal(swuf2.a, 1);
assert.isUndefined(swuf2.b);
assert.equal(swuf2.c, 'hi');
assert.isUndefined(swuf2.d);
assert.isUndefined(swuf2.e);
assert.isUndefined(swuf2.f);
assert.throws(() => {
swuf.setC(undefined);
});
});
});

View File

@@ -0,0 +1,29 @@
// @flow
import {assert} from 'chai';
import {suite, test} from 'mocha';
import {
StructWithUnions,
__unionOfBOfFloat64AndCOfString,
__unionOfEOfFloat64AndFOfString,
} from './gen/struct_with_unions.noms.js';
suite('struct_optional.noms', () => {
test('constructor', async () => {
// TODO: This needs to be cleaner.
const swu = new StructWithUnions({
a: new __unionOfBOfFloat64AndCOfString({b: 1}),
d: new __unionOfEOfFloat64AndFOfString({f:'hi'}),
});
assert.equal(swu.a.b, 1);
assert.equal(swu.d.f, 'hi');
const swu2 = swu.setA(swu.a.setC('bye'));
const swu3 = new StructWithUnions({
a: new __unionOfBOfFloat64AndCOfString({c: 'bye'}),
d: new __unionOfEOfFloat64AndFOfString({f:'hi'}),
});
assert.isTrue(swu2.equals(swu3));
});
});

View File

@@ -6,8 +6,10 @@ import {
Field as _Field,
Package as _Package,
boolType as _boolType,
createStructClass as _createStructClass,
makeEnumType as _makeEnumType,
makeStructType as _makeStructType,
makeType as _makeType,
registerPackage as _registerPackage,
stringType as _stringType,
} from '@attic/noms';
@@ -15,9 +17,8 @@ import type {
Struct as _Struct,
} from '@attic/noms';
{
const pkg = new _Package([
_makeStructType('S',
const _pkg = new _Package([
_makeStructType('S',
[
new _Field('s', _stringType, false),
new _Field('b', _boolType, false),
@@ -26,20 +27,31 @@ import type {
]
),
_makeEnumType('E', 'e1', 'e2', 'e3'),
], [
]);
_registerPackage(pkg);
}
_makeEnumType('E', 'e1', 'e2', 'e3'),
], [
]);
_registerPackage(_pkg);
const S$type = _makeType(_pkg.ref, 0);
const S$typeDef = _pkg.types[0];
const E$type = _makeType(_pkg.ref, 1);
const E$typeDef = _pkg.types[1];
export interface S extends _Struct {
type S$Data = {
s: string;
b: boolean;
};
interface S$Interface extends _Struct {
constructor(data: S$Data): void;
s: string; // readonly
setS(value: string): S;
setS(value: string): S$Interface;
b: boolean; // readonly
setB(value: boolean): S;
setB(value: boolean): S$Interface;
}
export const S: Class<S$Interface> = _createStructClass(S$type, S$typeDef);
export type E =
0 | // e1
1 | // e2