Skip to content
Open
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
33 changes: 19 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

[![Go](https://github.com/cyberstudio/jsonj/actions/workflows/go.yml/badge.svg)](https://github.com/cyberstudio/jsonj/actions/workflows/go.yml) [![GoDoc](https://godoc.org/github.com/cyberstudio/jsonj?status.svg)](https://godoc.org/github.com/cyberstudio/jsonj) [![Go Report Card](https://goreportcard.com/badge/github.com/cyberstudio/jsonj)](https://goreportcard.com/report/github.com/cyberstudio/jsonj)

JSONJ can be used to manipulate raw json input using _marks_ and custom _fragments generators_.
* Library guarantees valid json output syntax;
* Library doesn't validate output json semantic like unique keys.
The library seeks for marks in input json and applies fragment generators, producing a new json.
It works with rules that are combinations or marks and generators.

The library guarantees a valid output json syntax on valid input json.
Also, it is possible to generate a semantically invalid json if fragment generators are not correct.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Звучит как ещё одна приятная фича библиотеки, хотя это не так :)

Лучше без Also, а после этого предложения написать что-нибудь вроде "Test your fragment generators".

For example, an incorrect fragment generator can produce duplicate object keys.

## Marks

One can apply generator to _marks_ of json input. Each _mark_ is json key name.
For example, `uuid` and `id` maybe used as _mark_.
Each _mark_ is an object key name. One can apply generator to _marks_ of input.
For example, `uuid` and `id` can be used as _mark_.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Надо бы придумать пример получше, от этого куча wtf-ов вроде "зачем тут два id (и uuid, и id)?". Наверное, просто удалить uuid ок.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, id can be used as mark.

[
    {
        "id": 1234
    },
    {
        "id": 1234
    }
]

Mark can be renamed in result of operation. It depends on operation mode and its rules.

Advice: wrap marks in special symbols, i.e. __id__ and unwrap during operation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ты вот так предлагаешь?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ага, так хорошо.

```json
[
{
Expand All @@ -23,26 +26,26 @@ For example, `uuid` and `id` maybe used as _mark_.
]
```

_Mark_ maybe renamed in result of _operation_. It depends on `operation` mode and its rules.
_Mark_ can be renamed in result of _operation_. It depends on `operation` mode and its rules.

Advice: wrap _marks_ by special chars, i.e. `__uuid__` and unwrap during `operation`.
Advice: wrap _marks_ in special symbols, i.e. `__uuid__` and unwrap during `operation`.


## Operations
Library supports number of operations, named _Mode_:
* `ModeInsert`: insert key/value pair after the _mark_.
* `ModeReplaceValue`: replace value, or convert it;
* `ModeReplace`: replace entire key/value pair;

The library supports a number of operations, named _Mode_:
* `ModeInsert`: insert key/value pair after the _mark_,
* `ModeReplace`: replace the entire key/value pair,
* `ModeReplaceValue`: replace or convert value and keep key as is,
* `ModeDelete`: delete key/value.

## Fragments generators

Type `GenerateFragmentBatchFunc` describes interface of generators.
Key feature of generators is batch processing. Batches speed up result output.
Implement GenerateFragmentBatchFunc interface to create a custom fragment generator.

Example:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Совсем непонятный пример, конечно. Для того, чтобы он был понятным, нужно добавить пример входного и выходного json-а. Добавить больше комментов в коде.

Возможно, не помешал бы какой-нибудь более жизненный и простой пример, а то сложно людям объяснить, в чём профит подклеивать поле url в объекты.

```go
// GeneratorParams customize generator behavior
// GeneratorParams customizes generator behavior
type GeneratorParams struct{
EmbedObjectURL bool
BaseURL string
Expand Down Expand Up @@ -74,3 +77,5 @@ func Generator(ctx context.Context, iterator jsonj.FragmentIterator, p interface
return result, nil
}
```

Batch processing is a key feature of generators. It speeds up the result output.