Skip to content

Commit

Permalink
Add initial class-based LitTypes module and tests.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 458312425
  • Loading branch information
cjqian authored and LIT team committed Jun 30, 2022
1 parent db1ef3d commit c85e556
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
58 changes: 58 additions & 0 deletions lit_nlp/client/lib/lit_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

type LitClass = 'LitType';

/**
* Data classes used in configuring front-end components to describe
* input data and model outputs.
*/
export class LitType {
// tslint:disable:enforce-name-casing
__class__: LitClass|'type' = 'LitType';
// TODO(b/162269499): Replace this with LitName, when created.
// tslint:disable-next-line:no-any
__name__: any|undefined;
// TODO(b/162269499): __mro__ is included here to temporarily ensure
// type equivalence betwen the old `LitType` and new `LitType`.
__mro__: string[] = [];
readonly required: boolean = true;
// TODO(b/162269499): Replace this with `unknown` after migration.
// tslint:disable-next-line:no-any
readonly default: any|undefined = null;
// TODO(b/162269499): Update to camel case once we've replaced old LitType.
show_in_data_table: boolean = false;

// TODO(b/162269499): Add isCompatible functionality.
}

/**
* A string LitType.
*/
export class String extends LitType {
override default: string = '';
}

/**
* A scalar value, either a single float or int.
*/
export class Scalar extends LitType {
override default: number = 0;
min_val: number = 0;
max_val: number = 1;
step: number = .01;
}
17 changes: 17 additions & 0 deletions lit_nlp/client/lib/lit_types_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'jasmine';
import * as litTypes from './lit_types';

describe('lit types test', () => {

it('creates a string', () => {
const testString = new litTypes.String();
testString.default = "string value";

expect(testString.default).toBe("string value");
expect(testString.required).toBe(true);

expect(testString instanceof litTypes.String).toBe(true);
expect(testString instanceof litTypes.LitType).toBe(true);
expect(testString instanceof litTypes.Scalar).toBe(false);
});
});
2 changes: 2 additions & 0 deletions lit_nlp/client/lib/utils_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ describe('isLitSubtype test', () => {
'score': {
__class__: 'LitType',
__name__: 'RegressionScore',
// TODO(b/162269499): Update Specs to have updated relevant properties,
// now that the __mro__ field is no longer needed.
__mro__: ['RegressionScore', 'Scalar', 'LitType', 'object']
},
};
Expand Down

0 comments on commit c85e556

Please sign in to comment.