Sync implementation using cr-sqlite + websockets, across JS and CLI clients #1

Merged
aynish merged 4 commits from testing-sync into main 2024-08-31 18:06:10 -07:00
Showing only changes of commit b4e554866e - Show all commits

View file

@ -2,15 +2,16 @@ package main
import ( import (
"database/sql" "database/sql"
"strconv" "encoding/base64"
"strings"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"encoding/base64" "strconv"
"strings"
"mast/db" "mast/db"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
sqlite3 "github.com/mattn/go-sqlite3" sqlite3 "github.com/mattn/go-sqlite3"
) )
@ -80,7 +81,7 @@ type CRSQLChange struct {
Value []byte Value []byte
ColVersion int64 ColVersion int64
DBVersion int64 DBVersion int64
SiteID []int SiteID []byte
CL int64 CL int64
Seq int64 Seq int64
} }
@ -97,18 +98,18 @@ func mapToCRSQLChange(m map[string]interface{}) (CRSQLChange, error) {
change.Seq = int64(m["Seq"].(float64)) change.Seq = int64(m["Seq"].(float64))
// Handle []byte fields // Handle []byte fields
change.PK, err = base64.URLEncoding.DecodeString(m["PK"].(string)) change.PK, err = decodeBase64ToByteArray(m["PK"].(string))
if err != nil { if err != nil {
return change, err return change, err
} }
//fmt.Println(m["PK"]) //fmt.Println(m["PK"])
//fmt.Println(m["PK"].(string)) //fmt.Println(m["PK"].(string))
//fmt.Println(base64.URLEncoding.DecodeString(m["PK"].(string))) //fmt.Println(base64.URLEncoding.DecodeString(m["PK"].(string)))
change.Value, err = base64.URLEncoding.DecodeString(m["Value"].(string)) change.Value, err = decodeBase64ToByteArray(m["Value"].(string))
if err != nil { if err != nil {
return change, err return change, err
} }
change.SiteID, err = decodeBase64ToIntArray(string(m["SiteID"].(string))) change.SiteID, err = decodeBase64ToByteArray(m["SiteID"].(string))
fmt.Println(change.SiteID) fmt.Println(change.SiteID)
if err != nil { if err != nil {
return change, err return change, err
@ -118,7 +119,7 @@ func mapToCRSQLChange(m map[string]interface{}) (CRSQLChange, error) {
} }
func decodeBase64ToIntArray(encodedString string) ([]int, error) { func decodeBase64ToByteArray(encodedString string) ([]uint8, error) {
// Decode the base64 string // Decode the base64 string
decodedBytes, err := base64.StdEncoding.DecodeString(encodedString) decodedBytes, err := base64.StdEncoding.DecodeString(encodedString)
if err != nil { if err != nil {
@ -132,13 +133,13 @@ func decodeBase64ToIntArray(encodedString string) ([]int, error) {
stringSlice := strings.Split(decodedString, ",") stringSlice := strings.Split(decodedString, ",")
// Convert each string to an integer // Convert each string to an integer
intSlice := make([]int, len(stringSlice)) intSlice := make([]uint8, len(stringSlice))
for i, s := range stringSlice { for i, s := range stringSlice {
num, err := strconv.Atoi(s) num, err := strconv.Atoi(s)
if err != nil { if err != nil {
return nil, fmt.Errorf("error converting string to int: %w", err) return nil, fmt.Errorf("error converting string to int: %w", err)
} }
intSlice[i] = num intSlice[i] = byte(num)
} }
return intSlice, nil return intSlice, nil