From 675a3e852c7209717ff772e85e70953634dc0666 Mon Sep 17 00:00:00 2001 From: Jonathan Whitaker Date: Thu, 7 Dec 2023 17:50:46 -0700 Subject: [PATCH] docs(abac): fix Go SDK sample generator for WriteRequestViewer --- docs/content/modeling/abac-conditions.mdx | 4 +- .../Docs/SnippetViewer/CheckRequestViewer.tsx | 4 +- .../Docs/SnippetViewer/WriteRequestViewer.tsx | 67 +++++++++++-------- 3 files changed, 44 insertions(+), 31 deletions(-) diff --git a/docs/content/modeling/abac-conditions.mdx b/docs/content/modeling/abac-conditions.mdx index 558398819..4ee00f84d 100644 --- a/docs/content/modeling/abac-conditions.mdx +++ b/docs/content/modeling/abac-conditions.mdx @@ -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={[ diff --git a/src/components/Docs/SnippetViewer/CheckRequestViewer.tsx b/src/components/Docs/SnippetViewer/CheckRequestViewer.tsx index 2438d3b2b..e2f1d00a6 100644 --- a/src/components/Docs/SnippetViewer/CheckRequestViewer.tsx +++ b/src/components/Docs/SnippetViewer/CheckRequestViewer.tsx @@ -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}', }); @@ -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}", diff --git a/src/components/Docs/SnippetViewer/WriteRequestViewer.tsx b/src/components/Docs/SnippetViewer/WriteRequestViewer.tsx index 59cb5cd41..843d0b680 100644 --- a/src/components/Docs/SnippetViewer/WriteRequestViewer.tsx +++ b/src/components/Docs/SnippetViewer/WriteRequestViewer.tsx @@ -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( @@ -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({ @@ -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: {