Skip to content

Commit

Permalink
feat: Adjust response formatting to conditionally display 'Reason' fi…
Browse files Browse the repository at this point in the history
…eld when not empty (#121)
  • Loading branch information
HashCookie authored Jun 15, 2024
1 parent bbd20ff commit 33cadd6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
6 changes: 3 additions & 3 deletions app/components/editor/hooks/useRunTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function parseABACRequest(line: string): any[] {

async function enforcer(props: RunTestProps) {
const startTime = performance.now();
const result = [];
const result: { request: string; okEx: boolean; reason: string[]; }[] = [];
try {
const e = await newEnforcer(newModel(props.model), props.policy ? new StringAdapter(props.policy) : undefined);

Expand Down Expand Up @@ -172,8 +172,8 @@ async function enforcer(props: RunTestProps) {
const rvals = parseABACRequest(n);
const ctx = newEnforceContext(props.enforceContextData);

// @ts-ignore
result.push(await e.enforce(ctx, ...rvals));
const [okEx, reason] = await e.enforceEx(ctx, ...rvals);
result.push({ request: n, okEx, reason });
}

const stopTime = performance.now();
Expand Down
22 changes: 20 additions & 2 deletions app/components/editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,16 @@ export const EditorScreen = () => {
if (isValidElement(v)) {
setEcho(v);
} else if (Array.isArray(v)) {
setRequestResult(v.join('\n'));
const formattedResults = v.map((res) => {
if (typeof res === 'object') {
const reasonString = Array.isArray(res.reason) && res.reason.length > 0
? ` Reason: ${JSON.stringify(res.reason)}`
: '';
return `${res.okEx}${reasonString}`;
}
return res;
});
setRequestResult(formattedResults.join('\n'));
}
},
});
Expand Down Expand Up @@ -350,7 +359,16 @@ export const EditorScreen = () => {
if (isValidElement(v)) {
setEcho(v);
} else if (Array.isArray(v)) {
setRequestResult(v.join('\n'));
const formattedResults = v.map((res) => {
if (typeof res === 'object') {
const reasonString = Array.isArray(res.reason) && res.reason.length > 0
? ` Reason: ${JSON.stringify(res.reason)}`
: '';
return `${res.okEx}${reasonString}`;
}
return res;
});
setRequestResult(formattedResults.join('\n'));
}
},
});
Expand Down

0 comments on commit 33cadd6

Please sign in to comment.