Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

[OSE-177] Added GetOperation and ReviewSubmission impl #201

Merged
merged 14 commits into from
Dec 9, 2022

Conversation

andresuribe87
Copy link
Contributor

Overview

This includes the implementation of the ReviewSubmission, and the GetOperation endpoints.

Description

Most of the meat and bones of this is in the pkg/storage/bolt.go file, where I made changes to operations and submissions are updated in a single transaction. Noteworthy items:

  • Key interfaces that were introduced in order to ensure that storage layer was decoupled from the service layer.
  • Moved the pkg/service/presentation/model.go into it's own package, to avoid import cycles.

How Has This Been Tested?

See all test files modified. A key aspect is that once the operation is done, the GetOperation will return something like below:

{
  "id": "presentations/submissions/1237489",
  "done": true,
  "result": {
    "response": {
      "status":"approved",
      "reason":"fancy operator said it was ok",
      "id": ...
      "definition_id":...
      "definition_map":...
    }
  }
}

Checklist

Before submitting this PR, please make sure:

  • I have read the CONTRIBUTING document.
  • My code is consistent with the rest of the project
  • I have tagged the relevant reviewers and/or interested parties
  • I have updated the READMEs and other documentation of affected packages

References

SIP6


func serviceToRouterType(result interface{}) any {
switch result.(type) {
case model.Submission:
Copy link
Member

@decentralgabe decentralgabe Dec 9, 2022

Choose a reason for hiding this comment

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

what's the point of this method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The idea is to once we have Operation object who's results are different types, this function will be responsible for the conversion. That said, I've removed it until that happens.

switch {
case strings.Contains(op.ID, "submissions"):
var s prestorage.StoredSubmission
if err := json.Unmarshal(op.Response, &s); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

nit: consider returning an Operation pointer to avoid this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think sticking to non-pointers makes sense here. This we don't need to do nil-checking.

I reused the existing variable to avoid extra allocation.

Copy link
Member

Choose a reason for hiding this comment

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

my stylistic preference but no dealbreaker 😄

storage.UpdaterWithMap
}

func (u opUpdater) SetUpdatedResponse(first []byte) {
Copy link
Member

Choose a reason for hiding this comment

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

why first?

Copy link
Member

Choose a reason for hiding this comment

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

maybe response []byte?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

}),
})
if err != nil {
return StoredSubmission{}, opstorage.StoredOperation{}, errors.Wrap(err, "updating value and operation")
Copy link
Member

Choose a reason for hiding this comment

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

nit: consider returning pointers to avoid needing to instantiate empty objects

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I always go back and forth with this. The nice thing about this is avoiding any possibility of dereferencing a nil pointer.
As you mentioned, the downside is instantiating empty objects. But I find that acceptable because it is typically very cheap, and most likely optimized away by the compiler.

Copy link
Member

Choose a reason for hiding this comment

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

I think there's a semantic difference between an empty object and a nil object (e.g. empty set vs no set at all)

I agree it makes the caller API a bit more annoying because of the ptr dereferencing issue. I really wish you could get around this in go with an Optional type. There is a library but it's fairly un-go-like.

one of my favorite parts of scala was the option type which I believe has made its way into java and kotlin too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely agree that it's very much not idiomatic go to use option type. Having used them in the past, I view (*T, error) in go as exactly the same.

In Java world (I assume it's similar in Scala), there is a slight different between something like Optional<int> and Optional<Integer>, since in the latter, the object can be null. That's a similar problem as what we're discussing, I think.

}

func (u UpdaterWithMap) Validate(v []byte) error {
return nil
Copy link
Member

Choose a reason for hiding this comment

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

hmm, is there any validation that can be done here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The only one I could think about was validating that v could be unmarshalled from json. But that seems like it's overkill. The design of Validate is so that it's a simple hook.

I considered an alternative approach, where you can pass in the validate function you want to run into the constructor of UpdaterWithMap. I like this approach better because it provides a default validating function.

Copy link
Member

Choose a reason for hiding this comment

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

sounds good to me. may be worth adding a comment to capture that for future readers wondering if this can be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added comment.

Comment on lines 194 to 195
err := updateTxFn(namespace, key, updater, &first)(tx)
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

nit: one liner here and below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Member

@decentralgabe decentralgabe left a comment

Choose a reason for hiding this comment

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

approving pending conflict reviews and linters

@codecov-commenter
Copy link

Codecov Report

Merging #201 (98e6b3a) into main (adbbcae) will increase coverage by 22.67%.
The diff coverage is 27.67%.

@@            Coverage Diff            @@
##           main     #201       +/-   ##
=========================================
+ Coverage      0   22.67%   +22.67%     
=========================================
  Files         0       26       +26     
  Lines         0     2284     +2284     
=========================================
+ Hits          0      518      +518     
- Misses        0     1684     +1684     
- Partials      0       82       +82     
Impacted Files Coverage Δ
pkg/server/router/operation.go 0.00% <0.00%> (ø)
pkg/server/router/presentation.go 3.65% <0.00%> (ø)
pkg/service/operation/model.go 50.00% <0.00%> (ø)
pkg/service/operation/service.go 0.00% <0.00%> (ø)
pkg/storage/bolt.go 70.58% <70.96%> (ø)
pkg/server/router/keystore.go 12.69% <0.00%> (ø)
pkg/server/server.go 81.59% <0.00%> (ø)
integration/common.go 0.00% <0.00%> (ø)
integration/testutil.go 29.41% <0.00%> (ø)
... and 21 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@andresuribe87 andresuribe87 merged commit 42bff04 into TBD54566975:main Dec 9, 2022
@andresuribe87 andresuribe87 mentioned this pull request Dec 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants