Skip to content

Commit fd571de

Browse files
committed
documentation addition
1 parent 61c739d commit fd571de

File tree

2 files changed

+54
-52
lines changed

2 files changed

+54
-52
lines changed

.github/workflows/linter.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ jobs:
5959
# Note that this actually uses super-linter version 4.6.0 which is the desired version.
6060
# See https://github.com/github/super-linter/issues/1839.
6161
# It seems that version 4.6.1 has a bug with perlcritic, so we actually want 4.6.0.
62+
# updated to 4.8.1 If this works, I'll edit the above lines.
6263
uses: github/[email protected]
6364

6465
env:

src/store/models/index.ts

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
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

510
export interface Dictionary<T> {
611
[key: string]: T;
712
}
813

14+
// General Error coming from the API service
15+
916
export interface ResponseError {
1017
exception: string;
1118
message: string;
1219
}
1320

21+
// General Parsing error
22+
1423
export 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+
2435
export 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+
3076
export 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) {
3884
export const mailRE = /^[\w.]+@([a-zA-Z_.]+)+\.[a-zA-Z]{2,9}$/;
3985
export const usernameRE = /^[_a-zA-Z]([a-zA-Z._0-9])+$/;
4086

41-
export class UsernameParseException extends ParseError {
42-
constructor(message: string){
43-
super('UsernameParseExcpeption', message);
44-
}
45-
}
46-
4787
export 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-
6195
export 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-
75103
const booleanRE = /^([01])|(true)|(false)$/;
76104
const booleanTrue = /^(1)|(true)$/;
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-
115129
export type generic = string|number|boolean;
116130

117131
export 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

Comments
 (0)