Skip to content

Commit c871f7d

Browse files
committed
Support .jsonl tuple files
1 parent cf53c70 commit c871f7d

4 files changed

Lines changed: 28 additions & 22 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ fga tuple **write** <user> <relation> <object> --store-id=<store-id>
702702
* `--condition-context`: Condition context (optional)
703703
* `--store-id`: Specifies the store id
704704
* `--model-id`: Specifies the model id to target (optional)
705-
* `--file`: Specifies the file name, `json`, `yaml` and `csv` files are supported
705+
* `--file`: Specifies the file name, `json`, `jsonl`, `yaml` and `csv` files are supported
706706
* When using a `json` file, tuples can be provided as an array or newline-delimited (`jsonl`) objects
707707
* `--max-tuples-per-write`: Max tuples to send in a single write (optional, default=1, or 40 if `--max-rps` is set and this flag is omitted)
708708
* `--max-parallel-requests`: Max requests to send in parallel (optional, default=4, or `max-rps/5` if `--max-rps` is set and this flag is omitted)
@@ -773,7 +773,7 @@ If using a `json` file, the format should be:
773773
}
774774
]
775775
```
776-
JSON files can also be provided in JSONL format, with one JSON object per line.
776+
JSON files can also be provided in JSONL format (use the `.jsonl` extension), with one JSON object per line.
777777

778778
###### Response
779779
```json5

cmd/tuple/write_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ func TestParseTuplesFileData(t *testing.T) {
149149
},
150150
},
151151
{
152-
name: "it can correctly parse a jsonl file",
153-
file: "testdata/tuples_jsonl.json",
152+
name: "it can correctly parse a jsonl file",
153+
file: "testdata/tuples_jsonl.jsonl",
154154
expectedTuples: []client.ClientTupleKey{
155155
{
156156
User: "user:anne",

internal/tuplefile/read.go

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,26 @@ func ReadTupleFile(fileName string) ([]client.ClientTupleKey, error) {
2020

2121
var tuples []client.ClientTupleKey
2222

23-
switch path.Ext(fileName) {
24-
case ".json":
25-
err = yaml.Unmarshal(data, &tuples)
26-
if err != nil {
27-
err = parseTuplesFromJSONL(data, &tuples)
28-
}
29-
if err == nil && len(tuples) == 0 {
30-
err = clierrors.EmptyTuplesFileError("json")
31-
}
32-
case ".yaml", ".yml":
33-
err = yaml.Unmarshal(data, &tuples)
34-
if err == nil && len(tuples) == 0 {
35-
err = clierrors.EmptyTuplesFileError(strings.TrimPrefix(path.Ext(fileName), "."))
36-
}
23+
ext := path.Ext(fileName)
24+
switch ext {
25+
case ".json":
26+
err = yaml.Unmarshal(data, &tuples)
27+
if err != nil {
28+
err = parseTuplesFromJSONL(data, &tuples, "json")
29+
}
30+
if err == nil && len(tuples) == 0 {
31+
err = clierrors.EmptyTuplesFileError("json")
32+
}
33+
case ".jsonl":
34+
err = parseTuplesFromJSONL(data, &tuples, "jsonl")
35+
if err == nil && len(tuples) == 0 {
36+
err = clierrors.EmptyTuplesFileError("jsonl")
37+
}
38+
case ".yaml", ".yml":
39+
err = yaml.Unmarshal(data, &tuples)
40+
if err == nil && len(tuples) == 0 {
41+
err = clierrors.EmptyTuplesFileError(strings.TrimPrefix(ext, "."))
42+
}
3743
case ".csv":
3844
err = parseTuplesFromCSV(data, &tuples)
3945
default:
@@ -47,7 +53,7 @@ func ReadTupleFile(fileName string) ([]client.ClientTupleKey, error) {
4753
return tuples, nil
4854
}
4955

50-
func parseTuplesFromJSONL(data []byte, tuples *[]client.ClientTupleKey) error {
56+
func parseTuplesFromJSONL(data []byte, tuples *[]client.ClientTupleKey, format string) error {
5157
lines := strings.Split(strings.TrimSpace(string(data)), "\n")
5258
for index, line := range lines {
5359
trimmed := strings.TrimSpace(line)
@@ -63,9 +69,9 @@ func parseTuplesFromJSONL(data []byte, tuples *[]client.ClientTupleKey) error {
6369
*tuples = append(*tuples, tuple)
6470
}
6571

66-
if len(*tuples) == 0 {
67-
return clierrors.EmptyTuplesFileError("json")
68-
}
72+
if len(*tuples) == 0 {
73+
return clierrors.EmptyTuplesFileError(format)
74+
}
6975

7076
return nil
7177
}

0 commit comments

Comments
 (0)