MCPcopy Create free account
hub / github.com/hydro-dev/Hydro / schemaToSettings

Function schemaToSettings

packages/hydrooj/src/model/setting.ts:85–135  ·  view source on GitHub ↗
(schema: Schema<any>)

Source from the content-addressed store, hash-verified

83}
84
85function schemaToSettings(schema: Schema<any>) {
86 const result: _Setting[] = [];
87 const processNode = (key: string, s: Schema<number> | Schema<string> | Schema<boolean>, defaultFamily = 'setting_basic') => {
88 if (s.dict) throw new Error('Dict is not supported here');
89 let flag = (s.meta?.hidden ? FLAG_HIDDEN : 0)
90 | (s.meta?.disabled ? FLAG_DISABLED : 0);
91 const actualType = s.type === 'transform' ? s.inner.type : s.type;
92 const actualList = s.type === 'transform' ? s.inner.list : s.list;
93 const type = actualType === 'any' ? 'json'
94 : actualType === 'number' ? 'number'
95 : actualType === 'boolean' ? 'boolean'
96 : s.meta?.role === 'markdown' ? 'markdown'
97 : s.meta?.role === 'textarea' ? 'textarea' : 'text';
98 if (s.meta?.role === 'password') flag |= FLAG_SECRET;
99 if (s.meta?.flag) flag |= s.meta?.flag;
100 const options = {};
101 for (const item of actualList || []) {
102 if (item.type !== 'const') throw new Error(`List item must be a constant, got ${item.type}`);
103 options[item.value] = item.meta?.description || item.value;
104 }
105 return {
106 family: s.meta?.family || defaultFamily,
107 key,
108 value: s.meta?.default,
109 name: key,
110 desc: s.meta?.description,
111 flag,
112 subType: '',
113 type: actualList ? 'select' : type,
114 range: actualList ? options : null,
115 validation: (v) => {
116 try {
117 (s as any)(v);
118 return true;
119 } catch (e) {
120 return false;
121 }
122 },
123 } as _Setting;
124 };
125 if (!schema.dict) return [];
126 for (const key in schema.dict) {
127 const value = schema.dict[key];
128 if (value.dict) {
129 for (const subkey in value.dict) {
130 result.push(processNode(`${key}.${subkey}`, value.dict[subkey], value.meta?.family));
131 }
132 } else result.push(processNode(key, value));
133 }
134 return result;
135}
136
137export const PreferenceSetting = (...settings: _Setting[] | Schema<any>[]) => {
138 settings = settings.flatMap((s) => (s instanceof Schema ? schemaToSettings(s) : s) as _Setting[]);

Callers 5

PreferenceSettingFunction · 0.85
AccountSettingFunction · 0.85
DomainUserSettingFunction · 0.85
DomainSettingFunction · 0.85
SystemSettingFunction · 0.85

Calls 2

processNodeFunction · 0.85
pushMethod · 0.45

Tested by

no test coverage detected