Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adjust response formatting to conditionally display 'Reason' field when not empty #121

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading