1- // general classes and parsing functions
1+ /* This find contains three types of functionality
2+ 1) some basic interfaces for non-specific models.
3+ 2) some parsing functions for most types
4+ 3) the general Model class for handling parsing of all basic object types (Course, ProblemSet, User)
25
3- import { intersection , isEqual , difference , pickBy } from 'lodash' ;
6+ */
7+
8+ import { intersection , isEqual , difference , pickBy , pick } from 'lodash' ;
49
510export interface Dictionary < T > {
611 [ key : string ] : T ;
712}
813
14+ // General Error coming from the API service
15+
916export interface ResponseError {
1017 exception : string ;
1118 message : string ;
1219}
1320
21+ // General Parsing error
22+
1423export class ParseError {
1524 type : string ;
1625 message : string ;
@@ -21,12 +30,49 @@ export class ParseError {
2130 }
2231}
2332
33+ // Some specific parsing errors/exceptions
34+
2435export class NonNegIntException extends ParseError {
2536 constructor ( message : string ) {
2637 super ( 'NonNegIntException' , message ) ;
2738 }
2839}
2940
41+ export class UsernameParseException extends ParseError {
42+ constructor ( message : string ) {
43+ super ( 'UsernameParseExcpeption' , message ) ;
44+ }
45+ }
46+
47+ export class EmailParseException extends ParseError {
48+ constructor ( message : string ) {
49+ super ( 'EmailParseException' , message ) ;
50+ }
51+ }
52+
53+ export class BooleanParseException extends ParseError {
54+ constructor ( message : string ) {
55+ super ( 'BooleanParseException' , message ) ;
56+ }
57+ }
58+
59+ export class RequiredFieldsException extends ParseError {
60+ constructor ( _field : string , message : string ) {
61+ super ( 'RequiredFieldsException' , message ) ;
62+ super . field = _field ;
63+ }
64+ }
65+
66+ export class InvalidFieldsException extends ParseError {
67+ constructor ( _field : string , message : string ) {
68+ super ( 'InvalidFieldsException' , message ) ;
69+ super . field = _field ;
70+ }
71+ }
72+
73+
74+ // Parsing functions
75+
3076export function parseNonNegInt ( val : string | number ) {
3177 if ( / ^ \s * ( \d + ) \s * $ / . test ( `${ val } ` ) ) {
3278 return parseInt ( `${ val } ` ) ;
@@ -38,12 +84,6 @@ export function parseNonNegInt(val: string | number) {
3884export const mailRE = / ^ [ \w . ] + @ ( [ a - z A - Z _ . ] + ) + \. [ a - z A - Z ] { 2 , 9 } $ / ;
3985export const usernameRE = / ^ [ _ a - z A - Z ] ( [ a - z A - Z . _ 0 - 9 ] ) + $ / ;
4086
41- export class UsernameParseException extends ParseError {
42- constructor ( message : string ) {
43- super ( 'UsernameParseExcpeption' , message ) ;
44- }
45- }
46-
4787export function parseUsername ( val : string | undefined ) {
4888 if ( typeof val === 'string' && ( mailRE . test ( `${ val } ` ) || usernameRE . test ( `${ val } ` ) ) ) {
4989 return val ;
@@ -52,12 +92,6 @@ export function parseUsername(val: string | undefined) {
5292 }
5393}
5494
55- export class EmailParseException extends ParseError {
56- constructor ( message : string ) {
57- super ( 'EmailParseException' , message ) ;
58- }
59- }
60-
6195export function parseEmail ( val : string | undefined ) : string | undefined {
6296 if ( typeof val === 'string' && mailRE . test ( `${ val } ` ) ) {
6397 return val ;
@@ -66,12 +100,6 @@ export function parseEmail(val: string|undefined): string|undefined {
66100 }
67101}
68102
69- export class BooleanParseException extends ParseError {
70- constructor ( message : string ) {
71- super ( 'BooleanParseException' , message ) ;
72- }
73- }
74-
75103const booleanRE = / ^ ( [ 0 1 ] ) | ( t r u e ) | ( f a l s e ) $ / ;
76104const booleanTrue = / ^ ( 1 ) | ( t r u e ) $ / ;
77105
@@ -98,20 +126,6 @@ export function parseUserRole(role: string) {
98126 return role ;
99127}
100128
101- export class RequiredFieldsException extends ParseError {
102- constructor ( _field : string , message : string ) {
103- super ( 'RequiredFieldsException' , message ) ;
104- super . field = _field ;
105- }
106- }
107-
108- export class InvalidFieldsException extends ParseError {
109- constructor ( _field : string , message : string ) {
110- super ( 'InvalidFieldsException' , message ) ;
111- super . field = _field ;
112- }
113- }
114-
115129export type generic = string | number | boolean ;
116130
117131export type ParseableModel = Dictionary < generic | Dictionary < generic > > ;
@@ -197,28 +211,15 @@ export const Model = <Bool extends string, Num extends string, Str extends strin
197211 @typescript -eslint/no-explicit-any */
198212
199213 set ( params : Dictionary < generic | Dictionary < generic > > ) {
214+ // parse the non-object fields;
200215 const fields = [ ...this . _boolean_field_names , ...this . _number_field_names , ...this . _string_field_names ] ;
216+ const parsed_params = parseParams ( pick ( params , fields ) , this . _fields ) ;
217+
201218 fields . forEach ( key => {
202- // if the field is undefined or null in the params, but there is a default value, set it
203- if ( ( params as any ) [ key ] == null && this . _fields [ key ] . default_value !== undefined ) {
204- ( params as any ) [ key ] = this . _fields [ key ] . default_value ;
205- }
206- // parse each field
207- if ( ( params as any ) [ key ] != null && this . _fields [ key ] . field_type === 'boolean' ) {
208- ( this as any ) [ key ] = parseBoolean ( ( params as any ) [ key ] ) ;
209- } else if ( ( params as any ) [ key ] != null && this . _fields [ key ] . field_type === 'string' ) {
210- ( this as any ) [ key ] = `${ ( params as any ) [ key ] as string } ` ;
211- } else if ( ( params as any ) [ key ] != null && this . _fields [ key ] . field_type === 'non_neg_int' ) {
212- ( this as any ) [ key ] = parseNonNegInt ( ( params as any ) [ key ] ) ;
213- } else if ( ( params as any ) [ key ] != null && this . _fields [ key ] . field_type === 'username' ) {
214- ( this as any ) [ key ] = parseUsername ( ( params as any ) [ key ] ) ;
215- } else if ( ( params as any ) [ key ] != null && this . _fields [ key ] . field_type === 'email' ) {
216- ( this as any ) [ key ] = parseEmail ( ( params as any ) [ key ] ) ;
217- } else if ( ( params as any ) [ key ] != null && this . _fields [ key ] . field_type === 'role' ) {
218- ( this as any ) [ key ] = parseUserRole ( ( params as any ) [ key ] ) ;
219+ if ( ( parsed_params as any ) [ key ] != null ) {
220+ ( this as any ) [ key ] = ( parsed_params as any ) [ key ] ;
219221 }
220222 } ) ;
221- // assign(this, pick(params, this._dictionary_field_names));
222223 }
223224 /* eslint-enable */
224225
0 commit comments