Skip to content

Commit

Permalink
docs(abac): fix Go SDK sample generator for WriteRequestViewer
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-whit committed Dec 8, 2023
1 parent 0f60c6f commit 675a3e8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 31 deletions.
4 changes: 2 additions & 2 deletions docs/content/modeling/abac-conditions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ For example, we can give `user:anne` viewer access to `document:1` for 10 minute
name: 'temporal_grant',
context: {
grant_time: '2023-01-01T00:00:00Z',
grant_duration: '10m'
grant_duration: '10m',
}
}
}
},
]}
skipSetup={true}
allowedLanguages={[
Expand Down
4 changes: 2 additions & 2 deletions src/components/Docs/SnippetViewer/CheckRequestViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const { allowed } = await fgaClient.check({
(tuple) => `${JSON.stringify(tuple)}`
)
.join(',')}
],`}${!context ? `\n }` : `,\n context: ${JSON.stringify(context)}\n }` }, {
],`}${!context ? `\n }` : `\n context: ${JSON.stringify(context)}\n }` }, {
authorization_model_id: '${modelId}',
});
Expand All @@ -82,7 +82,7 @@ const { allowed } = await fgaClient.check({
/* eslint-disable no-tabs */
return `
options := ClientCheckOptions{
\tAuthorizationModelId: openfga.PtrString("${modelId}"),
\tAuthorizationModelId: "${modelId}",
}
body := ClientCheckRequest{
\tUser: "${user}",
Expand Down
67 changes: 40 additions & 27 deletions src/components/Docs/SnippetViewer/WriteRequestViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@ ${
const writeTuples = opts.relationshipTuples
? opts.relationshipTuples
.map(
({ user, relation, object, _description }) => `
(tuple) => `
${
_description ? `// ${_description}\n ` : ''
}{ user: '${user}', relation: '${relation}', object: '${object}'}`,
tuple._description ? `// ${tuple._description}\n ` : ''
}${ JSON.stringify(tuple)}`,
)
.join(',')
: '';

const deleteTuples = opts.deleteRelationshipTuples
? opts.deleteRelationshipTuples
.map(
Expand All @@ -93,10 +94,8 @@ ${
)
.join(',')
: '';
const writes = `writes: [${writeTuples}]
}`;
const deletes = `deletes: [${deleteTuples}]
}`;
const writes = `writes: [${writeTuples.length > 0 ? `${writeTuples}\n ]` : ']'}`;
const deletes = `deletes: [${deleteTuples.length > 0 ? `${deleteTuples}\n ]` : ']'}`;
const separator = `${opts.deleteRelationshipTuples && opts.relationshipTuples ? ',\n ' : ''}`;
return `
await fgaClient.write({
Expand All @@ -111,42 +110,56 @@ await fgaClient.write({
const writeTuples = opts.relationshipTuples
? opts.relationshipTuples
.map(
({ user, relation, object, _description }) => `
\t\t\t{
\t\t\t\t${_description ? `// ${_description}\n\t\t\t\t` : ''}User: openfga.PtrString("${user}"),
\t\t\t\tRelation: openfga.PtrString("${relation}"),
\t\t\t\tObject: openfga.PtrString("${object}"),
\t\t\t}, `,
)
({ user, relation, object, condition, _description }) =>
` {${_description ? `
// ${_description}` : '' }
User: "${user}",
Relation: "${relation}",
Object: "${object}",${ condition ? `
Condition: &RelationshipCondition{
Name: "${condition.name}",
Context: &map[string]interface{}${JSON.stringify(condition.context)},
},` : '' }
}, `)
.join('')
: '';
const deleteTuples = opts.deleteRelationshipTuples

const deleteTuples = opts.deleteRelationshipTuples
? opts.deleteRelationshipTuples
.map(
({ user, relation, object, _description }) => `
\t\t\t{
\t\t\t\t${_description ? `// ${_description}\n\t\t\t\t` : ''}User: openfga.PtrString("${user}"),
\t\t\t\tRelation: openfga.PtrString("${relation}"),
\t\t\t\tObject: openfga.PtrString("${object}"),
\t\t\t}, `,
({ user, relation, object, _description }) =>
` {${ _description ? `
// ${_description}` : '' }
User: "${user}",
Relation: "${relation}",
Object: "${object}",
}, `,
)
.join('')
: '';
const writes = `\tWrites: &[]ClientTupleKey{${writeTuples}}`;
const deletes = `\tDeletes: &[]ClientTupleKey{${deleteTuples}}`;

const writes =
` Writes: []ClientTupleKey{${writeTuples.length > 0 ? `\n${writeTuples}
},` : '},'}`

const deletes =
`\n Deletes: []ClientTupleKeyWithoutCondition{${deleteTuples.length > 0 ? `\n${deleteTuples}
},` : '},'}`

return `
options := ClientWriteOptions{
\tAuthorizationModelId: openfga.PtrString("${modelId}"),
AuthorizationModelId: "${modelId}",
}
body := fgaClient.ClientWriteRequest{
${opts.relationshipTuples ? writes : ''}${opts.deleteRelationshipTuples ? deletes : ''} }
${opts.relationshipTuples ? writes : ''}${opts.deleteRelationshipTuples ? deletes : ''}
}
data, err := fgaClient.Write(context.Background()).Body(requestBody).Options(options).Execute()
if err != nil {
// .. Handle error
}
`;
}`;
}

case SupportedLanguage.DOTNET_SDK: {
Expand Down

0 comments on commit 675a3e8

Please sign in to comment.