generated from okp4/template-go
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(graphql): use a composite cursor
the implementation is now an hexadecimal representation of a byte array with a length of 20, the first 8 bytes contains the validator points and the 12 others the validator id.
- Loading branch information
Showing
2 changed files
with
60 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package nemeton | ||
|
||
import ( | ||
"encoding/binary" | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
) | ||
|
||
type Cursor struct { | ||
points uint64 | ||
objectID primitive.ObjectID | ||
} | ||
|
||
func (c *Cursor) Hex() string { | ||
return hex.EncodeToString(c.MarshalBinary()) | ||
} | ||
|
||
func (c *Cursor) FromHex(enc string) error { | ||
bin, err := hex.DecodeString(enc) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return c.UnmarshalBinary(bin) | ||
} | ||
|
||
func (c *Cursor) MarshalBinary() []byte { | ||
var enc [20]byte | ||
|
||
binary.LittleEndian.PutUint64(enc[:8], c.points) | ||
|
||
rawID := [12]byte(c.objectID) | ||
copy(enc[8:], rawID[:]) | ||
|
||
return enc[:] | ||
} | ||
|
||
func (c *Cursor) UnmarshalBinary(enc []byte) error { | ||
if len(enc) != 20 { | ||
return fmt.Errorf("expected a 20 length byte array") | ||
} | ||
|
||
c.points = binary.LittleEndian.Uint64(enc[:8]) | ||
|
||
var decID [12]byte | ||
copy(decID[:], enc[8:]) | ||
c.objectID = decID | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters