Repository: yorkie-team/yorkie
Pull request #449 (2023-01-25 08:24:31)

Increase CRDT Counter in local change
<!--  Thanks for sending a pull request! -->

**What this PR does / why we need it**:
When changing value of Counter through calling `increase()`, local value in `Document.doc` is not updated. The problem was hidden because `Document.clone` and remote value has been updated normally.

**Which issue(s) this PR fixes**:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->
Address [yorkie-team/yorkie-js-sdk#438](https://github.com/yorkie-team/yorkie-js-sdk/issues/438)

**Special notes for your reviewer**:

**Does this PR introduce a user-facing change?**:
<!--
If no, just write "NONE" in the release-note block below.
If yes, a release note is required:
Enter your extended release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required".
-->
```release-note

```

**Additional documentation**:

<!--
This section can be blank if this pull request does not require a release note.

Please use the following format for linking documentation:
- [Usage]: <link>
- [Other doc]: <link>
-->
```docs

```

**Checklist**:
- [x] Added relevant tests or not required
- [x] Didn't break anything


codecov[bot]: # [Codecov](https://codecov.io/gh/yorkie-team/yorkie/pull/449?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team) Report
> Merging [#449](https://codecov.io/gh/yorkie-team/yorkie/pull/449?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team) (e901127) into [main](https://codecov.io/gh/yorkie-team/yorkie/commit/121b33726719d8050b9ff2dbec05271b8019fc9b?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team) (121b337) will **decrease** coverage by `0.01%`.
> The diff coverage is `n/a`.

```diff
@@            Coverage Diff             @@
##             main     #449      +/-   ##
==========================================
- Coverage   46.48%   46.47%   -0.01%     
==========================================
  Files          70       70              
  Lines        5731     5730       -1     
==========================================
- Hits         2664     2663       -1     
  Misses       2773     2773              
  Partials      294      294              
```


| [Impacted Files](https://codecov.io/gh/yorkie-team/yorkie/pull/449?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team) | Coverage Δ | |
|---|---|---|
| [api/converter/from\_pb.go](https://codecov.io/gh/yorkie-team/yorkie/pull/449?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team#diff-YXBpL2NvbnZlcnRlci9mcm9tX3BiLmdv) | `54.92% <0.00%> (-0.22%)` | :arrow_down: |
| [api/converter/to\_bytes.go](https://codecov.io/gh/yorkie-team/yorkie/pull/449?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team#diff-YXBpL2NvbnZlcnRlci90b19ieXRlcy5nbw==) | `83.33% <0.00%> (-0.16%)` | :arrow_down: |
| [pkg/document/crdt/text.go](https://codecov.io/gh/yorkie-team/yorkie/pull/449?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team#diff-cGtnL2RvY3VtZW50L2NyZHQvdGV4dC5nbw==) | `60.20% <0.00%> (+0.82%)` | :arrow_up: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=yorkie-team)


Patch:
diff --git a/pkg/document/json/counter.go b/pkg/document/json/counter.go
index 1a1abddd..668ebd23 100644
--- a/pkg/document/json/counter.go
+++ b/pkg/document/json/counter.go
@@ -71,6 +71,8 @@ func (p *Counter) Increase(v interface{}) *Counter {
 		panic("unsupported type")
 	}
 
+	p.Counter.Increase(primitive)
+
 	p.context.Push(operations.NewIncrease(
 		p.CreatedAt(),
 		primitive,


Test patch:
diff --git a/pkg/document/document_test.go b/pkg/document/document_test.go
index 815a1670..4158b05c 100644
--- a/pkg/document/document_test.go
+++ b/pkg/document/document_test.go
@@ -373,6 +373,7 @@ func TestDocument(t *testing.T) {
 		})
 		assert.NoError(t, err)
 		assert.Equal(t, `{"age":128}`, doc.Marshal())
+		assert.Equal(t, "128", doc.Root().GetCounter("age").Counter.Marshal())
 
 		// long type test
 		err = doc.Update(func(root *json.Object) error {