Skip to content

Commit

Permalink
lint and read me update
Browse files Browse the repository at this point in the history
  • Loading branch information
remade committed Sep 13, 2024
1 parent a10518d commit 04e3e47
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 79 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,41 @@ func main() {
- Run `go run main.go` to run the example.

## Data Models
This package facilitates communication between client and the backend service using the Concise

This comment has been minimized.

Copy link
@phughk

phughk Sep 13, 2024

Contributor

By having 1 sentence per line you can better review line changes and see if sentences are too long. Please could you fix this to be 1 sentence per line.

Binary Object Representation (CBOR) format. It streamlines data serialization and deserialization
while ensuring efficient and lightweight communication. The library also provides custom models
tailored to specific Data models recognised by SurrealDb, which cannot be covered by idiomatic go, enabling seamless interaction between
the client and the backend.

See the [documetation on data models](https://surrealdb.com/docs/surrealql/datamodel) on support data types

| CBOR Type | Go Representation | Example |
|-------------------|-----------------------------|----------------------------|
| Null | `nil` | `var x interface{} = nil` |
| None | `surrealdb.None` | `map[string]interface{}{"customer": surrealdb.None}` |
| Boolean | `bool` | `true`, `false` |
| Array | `[]interface{}` | `[]MyStruct{item1, item2}` |
| Date/Time | `time.Time` | `time.Now()` |
| Duration | `time.Duration` | `time.Duration(8821356)` |
| UUID (string representation) | `surrealdb.UUID(string)` | `surrealdb.UUID("123e4567-e89b-12d3-a456-426614174000")` |
| UUID (binary representation) | `surrealdb.UUIDBin([]bytes)`| `surrealdb.UUIDBin([]byte{0x01, 0x02, ...}`)` |
| Integer | `uint`, `uint64`, `int`, `int64` | `42`, `uint64(100000)`, `-42`, `int64(-100000)` |
| Floating Point | `float32`, `float64` | `3.14`, `float64(2.71828)` |
| Byte String, Binary Encoded Data | `[]byte` | `[]byte{0x01, 0x02}` |
| Text String | `string` | `"Hello, World!"` |
| Map | `map[interface{}]interface{}` | `map[string]float64{"one": 1.0}` |
| Table name| `surrealdb.Table(name)` | `surrealdb.Table("users")` |
| Record ID| `surrealdb.RecordID{Table: string, ID: interface{}}` | `surrealdb.RecordID{Table: "customers", ID: 1}, surrealdb.NewRecordID("customers", 1)` |
| Geometry Point | `surrealdb.GeometryPoint{Latitude: float64, Longitude: float64}` | `surrealdb.GeometryPoint{Latitude: 11.11, Longitude: 22.22` |
| Geometry Line | `surrealdb.GeometryLine{GeometricPoint1, GeometricPoint2,... }` | |
| Geometry Polygon | `surrealdb.GeometryPolygon{GeometryLine1, GeometryLine2,... }` | |
| Geometry Multipoint | `surrealdb.GeometryMultiPoint{GeometryPoint1, GeometryPoint2,... }` | |
| Geometry MultiLine | `surrealdb.GeometryMultiLine{GeometryLine1, GeometryLine2,... }` | |
| Geometry MultiPolygon | `surrealdb.GeometryMultiPolygon{GeometryPolygon1, GeometryPolygon2,... }` | |
| Geometry Collection| `surrealdb.GeometryMultiPolygon{GeometryPolygon1, GeometryLine2, GeometryPoint3, GeometryMultiPoint4,... }` | |




## Contributing

Expand Down
10 changes: 8 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package surrealdb

import (
"context"
"fmt"
"github.com/surrealdb/surrealdb.go/pkg/connection"
"github.com/surrealdb/surrealdb.go/pkg/constants"
Expand All @@ -10,6 +11,7 @@ import (

// DB is a client for the SurrealDB database that holds the connection.
type DB struct {
ctx context.Context
conn connection.Connection
liveHandler connection.LiveHandler
}
Expand Down Expand Up @@ -61,13 +63,17 @@ func New(connectionURL string) (*DB, error) {
// Public methods
// --------------------------------------------------

// WithContext
func (db *DB) WithContext(ctx context.Context) *DB {
db.ctx = ctx
return db
}

// Close closes the underlying WebSocket connection.
func (db *DB) Close() error {
return db.conn.Close()
}

// --------------------------------------------------

// Use is a method to select the namespace and table to use.
func (db *DB) Use(ns, database string) (interface{}, error) {
return db.send("use", ns, database)
Expand Down
2 changes: 1 addition & 1 deletion pkg/connection/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (h *HTTPConnection) MakeRequest(req *http.Request) ([]byte, error) {
return io.ReadAll(resp.Body)
}

func (h *HTTPConnection) Use(namespace string, database string) error {
func (h *HTTPConnection) Use(namespace, database string) error {
h.variables.Store("namespace", namespace)
h.variables.Store("database", database)

Expand Down
1 change: 0 additions & 1 deletion pkg/connection/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func TestEngine_HttpMakeRequest(t *testing.T) {
map[string]interface{}{
"datetime": time.Now(),
"testnil": nil,
//"duration": Duration(340),
},
}
res, err := con.Send("query", params)
Expand Down
70 changes: 1 addition & 69 deletions pkg/model/cbor.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package model

import (
"fmt"
"github.com/fxamacker/cbor/v2"
"github.com/surrealdb/surrealdb.go/internal/codec"
"io"
Expand All @@ -11,6 +10,7 @@ import (
type CustomCBORTag uint64

var (
NoneTag CustomCBORTag = 6
TableNameTag CustomCBORTag = 7
RecordIDTag CustomCBORTag = 8
UUIDStringTag CustomCBORTag = 9
Expand Down Expand Up @@ -114,71 +114,3 @@ func getCborDecoder() cbor.DecMode {

return dm
}

func (r *RecordID) MarshalCBOR() ([]byte, error) {
enc := getCborEncoder()

return enc.Marshal(cbor.Tag{
Number: uint64(RecordIDTag),
Content: []interface{}{r.ID, r.Table},
})
}

func (r *RecordID) UnmarshalCBOR(data []byte) error {
dec := getCborDecoder()

var temp []interface{}
err := dec.Unmarshal(data, &temp)
if err != nil {
return err
}

r.Table = temp[0].(string)
r.ID = temp[1]

return nil
}

func (d *CustomDateTime) MarshalCBOR() ([]byte, error) {
enc := getCborEncoder()

return enc.Marshal(cbor.Tag{
Number: uint64(DateTimeCompactString),
Content: [2]int64{1213, 123},
})
}

func (d *CustomDateTime) UnmarshalCBOR(data []byte) error {
dec := getCborDecoder()

var temp [2]interface{}
err := dec.Unmarshal(data, &temp)
if err != nil {
return err
}

fmt.Println(temp)
return nil
}

func (d *CustomDuration) MarshalCBOR() ([]byte, error) {
enc := getCborEncoder()

return enc.Marshal(cbor.Tag{
Number: uint64(DurationCompactTag),
Content: [2]int64{1213, 123},
})
}

func (d *CustomDuration) UnmarshalCBOR(data []byte) error {
dec := getCborDecoder()

var temp [2]interface{}
err := dec.Unmarshal(data, &temp)
if err != nil {
return err
}

fmt.Println(temp)
return nil
}
104 changes: 98 additions & 6 deletions pkg/model/model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model

import (
"fmt"
"github.com/fxamacker/cbor/v2"
"time"
)
Expand All @@ -10,14 +11,14 @@ type GeometryPoint struct {
Longitude float64
}

func NewGeometryPoint(latitude float64, longitude float64) GeometryPoint {
func NewGeometryPoint(latitude, longitude float64) GeometryPoint {
return GeometryPoint{
Latitude: latitude, Longitude: longitude,
}
}

func (g *GeometryPoint) GetCoordinates() [2]float64 {
return [2]float64{g.Latitude, g.Longitude}
func (gp *GeometryPoint) GetCoordinates() [2]float64 {
return [2]float64{gp.Latitude, gp.Longitude}
}

func (gp *GeometryPoint) MarshalCBOR() ([]byte, error) {
Expand All @@ -29,7 +30,7 @@ func (gp *GeometryPoint) MarshalCBOR() ([]byte, error) {
})
}

func (g *GeometryPoint) UnmarshalCBOR(data []byte) error {
func (gp *GeometryPoint) UnmarshalCBOR(data []byte) error {
dec := getCborDecoder()

var temp [2]float64
Expand All @@ -38,8 +39,8 @@ func (g *GeometryPoint) UnmarshalCBOR(data []byte) error {
return err
}

g.Latitude = temp[0]
g.Longitude = temp[1]
gp.Latitude = temp[0]
gp.Longitude = temp[1]

return nil
}
Expand Down Expand Up @@ -67,12 +68,84 @@ type RecordID struct {
ID interface{}
}

func NewRecordID(table string, id interface{}) *RecordID {
return &RecordID{Table: table, ID: id}
}

func (r *RecordID) MarshalCBOR() ([]byte, error) {
enc := getCborEncoder()

return enc.Marshal(cbor.Tag{
Number: uint64(RecordIDTag),
Content: []interface{}{r.ID, r.Table},
})
}

func (r *RecordID) UnmarshalCBOR(data []byte) error {
dec := getCborDecoder()

var temp []interface{}
err := dec.Unmarshal(data, &temp)
if err != nil {
return err
}

r.Table = temp[0].(string)
r.ID = temp[1]

return nil
}

type Decimal string

type CustomDateTime time.Time

func (d *CustomDateTime) MarshalCBOR() ([]byte, error) {
enc := getCborEncoder()

return enc.Marshal(cbor.Tag{
Number: uint64(DateTimeCompactString),
Content: [2]int64{1213, 123},
})
}

func (d *CustomDateTime) UnmarshalCBOR(data []byte) error {
dec := getCborDecoder()

var temp [2]interface{}
err := dec.Unmarshal(data, &temp)
if err != nil {
return err
}

fmt.Println(temp)

This comment has been minimized.

Copy link
@phughk

phughk Sep 13, 2024

Contributor

log instead of print, but preferably remove - this can include sensitive data and the client might not want to display it

return nil
}

type CustomDuration time.Duration

func (d *CustomDuration) MarshalCBOR() ([]byte, error) {
enc := getCborEncoder()

return enc.Marshal(cbor.Tag{
Number: uint64(DurationCompactTag),
Content: [2]int64{1213, 123},
})
}

func (d *CustomDuration) UnmarshalCBOR(data []byte) error {
dec := getCborDecoder()

var temp [2]interface{}
err := dec.Unmarshal(data, &temp)
if err != nil {
return err
}

fmt.Println(temp)

This comment has been minimized.

Copy link
@phughk

phughk Sep 13, 2024

Contributor

print

return nil
}

// Auth is a struct that holds surrealdb auth data for login.
type Auth struct {
Namespace string `json:"NS,omitempty"`
Expand All @@ -81,3 +154,22 @@ type Auth struct {
Username string `json:"user,omitempty"`
Password string `json:"pass,omitempty"`
}

type CustomNil struct {
}

func (c *CustomNil) MarshalCBOR() ([]byte, error) {
enc := getCborEncoder()

return enc.Marshal(cbor.Tag{
Number: uint64(NoneTag),
Content: nil,
})
}

func (c *CustomNil) CustomNil(data []byte) error {
c = &CustomNil{}
return nil
}

var None = CustomNil{}

0 comments on commit 04e3e47

Please sign in to comment.