mirror of
https://github.com/pikami/cosmium.git
synced 2025-12-19 08:50:46 +00:00
Move out repository models; Hidrate collection on create
This commit is contained in:
@@ -1,53 +1,60 @@
|
||||
package repositories
|
||||
|
||||
var collections = []Collection{
|
||||
import (
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
structhidrators "github.com/pikami/cosmium/internal/struct_hidrators"
|
||||
)
|
||||
|
||||
var collections = []repositorymodels.Collection{
|
||||
{ID: "db1"},
|
||||
{ID: "db2"},
|
||||
}
|
||||
|
||||
func GetAllCollections(databaseId string) ([]Collection, RepositoryStatus) {
|
||||
dbCollections := make([]Collection, 0)
|
||||
func GetAllCollections(databaseId string) ([]repositorymodels.Collection, repositorymodels.RepositoryStatus) {
|
||||
dbCollections := make([]repositorymodels.Collection, 0)
|
||||
|
||||
for _, coll := range collections {
|
||||
if coll.internals.databaseId == databaseId {
|
||||
if coll.Internals.DatabaseId == databaseId {
|
||||
dbCollections = append(dbCollections, coll)
|
||||
}
|
||||
}
|
||||
|
||||
return dbCollections, StatusOk
|
||||
return dbCollections, repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
func GetCollection(databaseId string, id string) (Collection, RepositoryStatus) {
|
||||
func GetCollection(databaseId string, id string) (repositorymodels.Collection, repositorymodels.RepositoryStatus) {
|
||||
for _, coll := range collections {
|
||||
if coll.internals.databaseId == databaseId && coll.ID == id {
|
||||
return coll, StatusOk
|
||||
if coll.Internals.DatabaseId == databaseId && coll.ID == id {
|
||||
return coll, repositorymodels.StatusOk
|
||||
}
|
||||
}
|
||||
|
||||
return Collection{}, StatusNotFound
|
||||
return repositorymodels.Collection{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
func DeleteCollection(databaseId string, id string) RepositoryStatus {
|
||||
func DeleteCollection(databaseId string, id string) repositorymodels.RepositoryStatus {
|
||||
for index, coll := range collections {
|
||||
if coll.internals.databaseId == databaseId && coll.ID == id {
|
||||
if coll.Internals.DatabaseId == databaseId && coll.ID == id {
|
||||
collections = append(collections[:index], collections[index+1:]...)
|
||||
return StatusOk
|
||||
return repositorymodels.StatusOk
|
||||
}
|
||||
}
|
||||
|
||||
return StatusNotFound
|
||||
return repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
func CreateCollection(databaseId string, newCollection Collection) RepositoryStatus {
|
||||
func CreateCollection(databaseId string, newCollection repositorymodels.Collection) (repositorymodels.Collection, repositorymodels.RepositoryStatus) {
|
||||
for _, coll := range collections {
|
||||
if coll.internals.databaseId == databaseId && coll.ID == newCollection.ID {
|
||||
return Conflict
|
||||
if coll.Internals.DatabaseId == databaseId && coll.ID == newCollection.ID {
|
||||
return repositorymodels.Collection{}, repositorymodels.Conflict
|
||||
}
|
||||
}
|
||||
|
||||
newCollection.internals = struct{ databaseId string }{
|
||||
databaseId: databaseId,
|
||||
newCollection = structhidrators.Hidrate(newCollection).(repositorymodels.Collection)
|
||||
|
||||
newCollection.Internals = struct{ DatabaseId string }{
|
||||
DatabaseId: databaseId,
|
||||
}
|
||||
collections = append(collections, newCollection)
|
||||
return StatusOk
|
||||
return newCollection, repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
@@ -1,42 +1,44 @@
|
||||
package repositories
|
||||
|
||||
var databases = []Database{
|
||||
import repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
|
||||
var databases = []repositorymodels.Database{
|
||||
{ID: "db1"},
|
||||
{ID: "db2"},
|
||||
}
|
||||
|
||||
func GetAllDatabases() ([]Database, RepositoryStatus) {
|
||||
return databases, StatusOk
|
||||
func GetAllDatabases() ([]repositorymodels.Database, repositorymodels.RepositoryStatus) {
|
||||
return databases, repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
func GetDatabase(id string) (Database, RepositoryStatus) {
|
||||
func GetDatabase(id string) (repositorymodels.Database, repositorymodels.RepositoryStatus) {
|
||||
for _, db := range databases {
|
||||
if db.ID == id {
|
||||
return db, StatusOk
|
||||
return db, repositorymodels.StatusOk
|
||||
}
|
||||
}
|
||||
|
||||
return Database{}, StatusNotFound
|
||||
return repositorymodels.Database{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
func DeleteDatabase(id string) RepositoryStatus {
|
||||
func DeleteDatabase(id string) repositorymodels.RepositoryStatus {
|
||||
for index, db := range databases {
|
||||
if db.ID == id {
|
||||
databases = append(databases[:index], databases[index+1:]...)
|
||||
return StatusOk
|
||||
return repositorymodels.StatusOk
|
||||
}
|
||||
}
|
||||
|
||||
return StatusNotFound
|
||||
return repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
func CreateDatabase(newDatabase Database) RepositoryStatus {
|
||||
func CreateDatabase(newDatabase repositorymodels.Database) repositorymodels.RepositoryStatus {
|
||||
for _, db := range databases {
|
||||
if db.ID == newDatabase.ID {
|
||||
return Conflict
|
||||
return repositorymodels.Conflict
|
||||
}
|
||||
}
|
||||
|
||||
databases = append(databases, newDatabase)
|
||||
return StatusOk
|
||||
return repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
@@ -4,15 +4,16 @@ import (
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
"github.com/pikami/cosmium/parsers"
|
||||
"github.com/pikami/cosmium/parsers/nosql"
|
||||
memoryexecutor "github.com/pikami/cosmium/query_executors/memory_executor"
|
||||
)
|
||||
|
||||
var documents = []Document{}
|
||||
var documents = []repositorymodels.Document{}
|
||||
|
||||
func GetAllDocuments(databaseId string, collectionId string) ([]Document, RepositoryStatus) {
|
||||
filteredDocuments := make([]Document, 0)
|
||||
func GetAllDocuments(databaseId string, collectionId string) ([]repositorymodels.Document, repositorymodels.RepositoryStatus) {
|
||||
filteredDocuments := make([]repositorymodels.Document, 0)
|
||||
|
||||
for _, doc := range documents {
|
||||
docDbId := doc["_internal"].(map[string]interface{})["databaseId"]
|
||||
@@ -24,10 +25,10 @@ func GetAllDocuments(databaseId string, collectionId string) ([]Document, Reposi
|
||||
}
|
||||
}
|
||||
|
||||
return filteredDocuments, StatusOk
|
||||
return filteredDocuments, repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
func GetDocument(databaseId string, collectionId string, documentId string) (Document, RepositoryStatus) {
|
||||
func GetDocument(databaseId string, collectionId string, documentId string) (repositorymodels.Document, repositorymodels.RepositoryStatus) {
|
||||
for _, doc := range documents {
|
||||
docDbId := doc["_internal"].(map[string]interface{})["databaseId"]
|
||||
docCollId := doc["_internal"].(map[string]interface{})["collectionId"]
|
||||
@@ -35,14 +36,14 @@ func GetDocument(databaseId string, collectionId string, documentId string) (Doc
|
||||
|
||||
if docDbId == databaseId && docCollId == collectionId && docId == documentId {
|
||||
doc["_partitionKeyValue"] = doc["_internal"].(map[string]interface{})["partitionKeyValue"]
|
||||
return doc, StatusOk
|
||||
return doc, repositorymodels.StatusOk
|
||||
}
|
||||
}
|
||||
|
||||
return Document{}, StatusNotFound
|
||||
return repositorymodels.Document{}, repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
func DeleteDocument(databaseId string, collectionId string, documentId string) RepositoryStatus {
|
||||
func DeleteDocument(databaseId string, collectionId string, documentId string) repositorymodels.RepositoryStatus {
|
||||
for index, doc := range documents {
|
||||
docDbId := doc["_internal"].(map[string]interface{})["databaseId"]
|
||||
docCollId := doc["_internal"].(map[string]interface{})["collectionId"]
|
||||
@@ -50,21 +51,21 @@ func DeleteDocument(databaseId string, collectionId string, documentId string) R
|
||||
|
||||
if docDbId == databaseId && docCollId == collectionId && docId == documentId {
|
||||
documents = append(documents[:index], documents[index+1:]...)
|
||||
return StatusOk
|
||||
return repositorymodels.StatusOk
|
||||
}
|
||||
}
|
||||
|
||||
return StatusNotFound
|
||||
return repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
func CreateDocument(databaseId string, collectionId string, document map[string]interface{}) RepositoryStatus {
|
||||
func CreateDocument(databaseId string, collectionId string, document map[string]interface{}) repositorymodels.RepositoryStatus {
|
||||
if document["id"] == "" {
|
||||
return BadRequest
|
||||
return repositorymodels.BadRequest
|
||||
}
|
||||
|
||||
collection, status := GetCollection(databaseId, collectionId)
|
||||
if status != StatusOk {
|
||||
return StatusNotFound
|
||||
if status != repositorymodels.StatusOk {
|
||||
return repositorymodels.StatusNotFound
|
||||
}
|
||||
|
||||
for _, doc := range documents {
|
||||
@@ -73,7 +74,7 @@ func CreateDocument(databaseId string, collectionId string, document map[string]
|
||||
docId := doc["id"]
|
||||
|
||||
if docDbId == databaseId && docCollId == collectionId && docId == document["id"] {
|
||||
return Conflict
|
||||
return repositorymodels.Conflict
|
||||
}
|
||||
}
|
||||
|
||||
@@ -99,18 +100,18 @@ func CreateDocument(databaseId string, collectionId string, document map[string]
|
||||
}
|
||||
documents = append(documents, document)
|
||||
|
||||
return StatusOk
|
||||
return repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
func ExecuteQueryDocuments(databaseId string, collectionId string, query string) ([]memoryexecutor.RowType, RepositoryStatus) {
|
||||
func ExecuteQueryDocuments(databaseId string, collectionId string, query string) ([]memoryexecutor.RowType, repositorymodels.RepositoryStatus) {
|
||||
parsedQuery, err := nosql.Parse("", []byte(query))
|
||||
if err != nil {
|
||||
log.Printf("Failed to parse query: %s\nerr: %v", query, err)
|
||||
return nil, BadRequest
|
||||
return nil, repositorymodels.BadRequest
|
||||
}
|
||||
|
||||
collectionDocuments, status := GetAllDocuments(databaseId, collectionId)
|
||||
if status != StatusOk {
|
||||
if status != repositorymodels.StatusOk {
|
||||
return nil, status
|
||||
}
|
||||
|
||||
@@ -119,5 +120,5 @@ func ExecuteQueryDocuments(databaseId string, collectionId string, query string)
|
||||
covDocs = append(covDocs, map[string]interface{}(doc))
|
||||
}
|
||||
|
||||
return memoryexecutor.Execute(parsedQuery.(parsers.SelectStmt), covDocs), StatusOk
|
||||
return memoryexecutor.Execute(parsedQuery.(parsers.SelectStmt), covDocs), repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
@@ -1,106 +0,0 @@
|
||||
package repositories
|
||||
|
||||
type Database struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
type RepositoryStatus int
|
||||
|
||||
const (
|
||||
StatusOk = 1
|
||||
StatusNotFound = 2
|
||||
Conflict = 3
|
||||
BadRequest = 4
|
||||
)
|
||||
|
||||
type Collection struct {
|
||||
ID string `json:"id"`
|
||||
IndexingPolicy struct {
|
||||
IndexingMode string `json:"indexingMode"`
|
||||
Automatic bool `json:"automatic"`
|
||||
IncludedPaths []struct {
|
||||
Path string `json:"path"`
|
||||
Indexes []struct {
|
||||
Kind string `json:"kind"`
|
||||
DataType string `json:"dataType"`
|
||||
Precision int `json:"precision"`
|
||||
} `json:"indexes"`
|
||||
} `json:"includedPaths"`
|
||||
ExcludedPaths []any `json:"excludedPaths"`
|
||||
} `json:"indexingPolicy"`
|
||||
PartitionKey struct {
|
||||
Paths []string `json:"paths"`
|
||||
Kind string `json:"kind"`
|
||||
Version int `json:"Version"`
|
||||
} `json:"partitionKey"`
|
||||
Rid string `json:"_rid"`
|
||||
Ts int `json:"_ts"`
|
||||
Self string `json:"_self"`
|
||||
Etag string `json:"_etag"`
|
||||
Docs string `json:"_docs"`
|
||||
Sprocs string `json:"_sprocs"`
|
||||
Triggers string `json:"_triggers"`
|
||||
Udfs string `json:"_udfs"`
|
||||
Conflicts string `json:"_conflicts"`
|
||||
internals struct {
|
||||
databaseId string
|
||||
}
|
||||
}
|
||||
|
||||
type UserDefinedFunction struct {
|
||||
Body string `json:"body"`
|
||||
ID string `json:"id"`
|
||||
Rid string `json:"_rid"`
|
||||
Ts int `json:"_ts"`
|
||||
Self string `json:"_self"`
|
||||
Etag string `json:"_etag"`
|
||||
internals struct {
|
||||
databaseId string
|
||||
collectionId string
|
||||
}
|
||||
}
|
||||
|
||||
type StoredProcedure struct {
|
||||
Body string `json:"body"`
|
||||
ID string `json:"id"`
|
||||
Rid string `json:"_rid"`
|
||||
Ts int `json:"_ts"`
|
||||
Self string `json:"_self"`
|
||||
Etag string `json:"_etag"`
|
||||
internals struct {
|
||||
databaseId string
|
||||
collectionId string
|
||||
}
|
||||
}
|
||||
|
||||
type Trigger struct {
|
||||
Body string `json:"body"`
|
||||
ID string `json:"id"`
|
||||
TriggerOperation string `json:"triggerOperation"`
|
||||
TriggerType string `json:"triggerType"`
|
||||
Rid string `json:"_rid"`
|
||||
Ts int `json:"_ts"`
|
||||
Self string `json:"_self"`
|
||||
Etag string `json:"_etag"`
|
||||
internals struct {
|
||||
databaseId string
|
||||
collectionId string
|
||||
}
|
||||
}
|
||||
|
||||
type Document map[string]interface{}
|
||||
|
||||
type PartitionKeyRange struct {
|
||||
Rid string `json:"_rid"`
|
||||
ID string `json:"id"`
|
||||
Etag string `json:"_etag"`
|
||||
MinInclusive string `json:"minInclusive"`
|
||||
MaxExclusive string `json:"maxExclusive"`
|
||||
RidPrefix int `json:"ridPrefix"`
|
||||
Self string `json:"_self"`
|
||||
ThroughputFraction int `json:"throughputFraction"`
|
||||
Status string `json:"status"`
|
||||
Parents []any `json:"parents"`
|
||||
Ts int `json:"_ts"`
|
||||
Lsn int `json:"lsn"`
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
package repositories
|
||||
|
||||
func GetPartitionKeyRanges(databaseId string, collectionId string) ([]PartitionKeyRange, RepositoryStatus) {
|
||||
import repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
|
||||
func GetPartitionKeyRanges(databaseId string, collectionId string) ([]repositorymodels.PartitionKeyRange, repositorymodels.RepositoryStatus) {
|
||||
// I have no idea what this is tbh
|
||||
return []PartitionKeyRange{
|
||||
return []repositorymodels.PartitionKeyRange{
|
||||
{
|
||||
Rid: "ZxlyAP7rKwACAAAAAAAAUA==",
|
||||
ID: "0",
|
||||
@@ -17,5 +19,5 @@ func GetPartitionKeyRanges(databaseId string, collectionId string) ([]PartitionK
|
||||
Ts: 1707431241,
|
||||
Lsn: 17,
|
||||
},
|
||||
}, StatusOk
|
||||
}, repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
package repositories
|
||||
|
||||
var storedProcedures = []StoredProcedure{}
|
||||
import repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
|
||||
func GetAllStoredProcedures(databaseId string, collectionId string) ([]StoredProcedure, RepositoryStatus) {
|
||||
sps := make([]StoredProcedure, 0)
|
||||
var storedProcedures = []repositorymodels.StoredProcedure{}
|
||||
|
||||
func GetAllStoredProcedures(databaseId string, collectionId string) ([]repositorymodels.StoredProcedure, repositorymodels.RepositoryStatus) {
|
||||
sps := make([]repositorymodels.StoredProcedure, 0)
|
||||
|
||||
for _, coll := range storedProcedures {
|
||||
if coll.internals.databaseId == databaseId && coll.internals.collectionId == collectionId {
|
||||
if coll.Internals.DatabaseId == databaseId && coll.Internals.CollectionId == collectionId {
|
||||
sps = append(sps, coll)
|
||||
}
|
||||
}
|
||||
|
||||
return sps, StatusOk
|
||||
return sps, repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
package repositories
|
||||
|
||||
var triggers = []Trigger{}
|
||||
import repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
|
||||
func GetAllTriggers(databaseId string, collectionId string) ([]Trigger, RepositoryStatus) {
|
||||
filteredTriggers := make([]Trigger, 0)
|
||||
var triggers = []repositorymodels.Trigger{}
|
||||
|
||||
func GetAllTriggers(databaseId string, collectionId string) ([]repositorymodels.Trigger, repositorymodels.RepositoryStatus) {
|
||||
filteredTriggers := make([]repositorymodels.Trigger, 0)
|
||||
|
||||
for _, coll := range triggers {
|
||||
if coll.internals.databaseId == databaseId && coll.internals.collectionId == collectionId {
|
||||
if coll.Internals.DatabaseId == databaseId && coll.Internals.CollectionId == collectionId {
|
||||
filteredTriggers = append(filteredTriggers, coll)
|
||||
}
|
||||
}
|
||||
|
||||
return filteredTriggers, StatusOk
|
||||
return filteredTriggers, repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
package repositories
|
||||
|
||||
var userDefinedFunctions = []UserDefinedFunction{}
|
||||
import repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
|
||||
func GetAllUserDefinedFunctions(databaseId string, collectionId string) ([]UserDefinedFunction, RepositoryStatus) {
|
||||
udfs := make([]UserDefinedFunction, 0)
|
||||
var userDefinedFunctions = []repositorymodels.UserDefinedFunction{}
|
||||
|
||||
func GetAllUserDefinedFunctions(databaseId string, collectionId string) ([]repositorymodels.UserDefinedFunction, repositorymodels.RepositoryStatus) {
|
||||
udfs := make([]repositorymodels.UserDefinedFunction, 0)
|
||||
|
||||
for _, coll := range userDefinedFunctions {
|
||||
if coll.internals.databaseId == databaseId && coll.internals.collectionId == collectionId {
|
||||
if coll.Internals.DatabaseId == databaseId && coll.Internals.CollectionId == collectionId {
|
||||
udfs = append(udfs, coll)
|
||||
}
|
||||
}
|
||||
|
||||
return udfs, StatusOk
|
||||
return udfs, repositorymodels.StatusOk
|
||||
}
|
||||
|
||||
112
internal/repository_models/models.go
Normal file
112
internal/repository_models/models.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package repositorymodels
|
||||
|
||||
type Database struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
type RepositoryStatus int
|
||||
|
||||
const (
|
||||
StatusOk = 1
|
||||
StatusNotFound = 2
|
||||
Conflict = 3
|
||||
BadRequest = 4
|
||||
)
|
||||
|
||||
type Collection struct {
|
||||
ID string `json:"id"`
|
||||
IndexingPolicy CollectionIndexingPolicy `json:"indexingPolicy"`
|
||||
PartitionKey CollectionPartitionKey `json:"partitionKey"`
|
||||
Rid string `json:"_rid"`
|
||||
Ts int `json:"_ts"`
|
||||
Self string `json:"_self"`
|
||||
Etag string `json:"_etag"`
|
||||
Docs string `json:"_docs"`
|
||||
Sprocs string `json:"_sprocs"`
|
||||
Triggers string `json:"_triggers"`
|
||||
Udfs string `json:"_udfs"`
|
||||
Conflicts string `json:"_conflicts"`
|
||||
Internals struct {
|
||||
DatabaseId string
|
||||
}
|
||||
}
|
||||
|
||||
type CollectionIndexingPolicy struct {
|
||||
IndexingMode string `json:"indexingMode"`
|
||||
Automatic bool `json:"automatic"`
|
||||
IncludedPaths []CollectionIndexingPolicyPath `json:"includedPaths"`
|
||||
ExcludedPaths []CollectionIndexingPolicyPath `json:"excludedPaths"`
|
||||
}
|
||||
|
||||
type CollectionIndexingPolicyPath struct {
|
||||
Path string `json:"path"`
|
||||
Indexes []struct {
|
||||
Kind string `json:"kind"`
|
||||
DataType string `json:"dataType"`
|
||||
Precision int `json:"precision"`
|
||||
} `json:"indexes"`
|
||||
}
|
||||
|
||||
type CollectionPartitionKey struct {
|
||||
Paths []string `json:"paths"`
|
||||
Kind string `json:"kind"`
|
||||
Version int `json:"Version"`
|
||||
}
|
||||
|
||||
type UserDefinedFunction struct {
|
||||
Body string `json:"body"`
|
||||
ID string `json:"id"`
|
||||
Rid string `json:"_rid"`
|
||||
Ts int `json:"_ts"`
|
||||
Self string `json:"_self"`
|
||||
Etag string `json:"_etag"`
|
||||
Internals struct {
|
||||
DatabaseId string
|
||||
CollectionId string
|
||||
}
|
||||
}
|
||||
|
||||
type StoredProcedure struct {
|
||||
Body string `json:"body"`
|
||||
ID string `json:"id"`
|
||||
Rid string `json:"_rid"`
|
||||
Ts int `json:"_ts"`
|
||||
Self string `json:"_self"`
|
||||
Etag string `json:"_etag"`
|
||||
Internals struct {
|
||||
DatabaseId string
|
||||
CollectionId string
|
||||
}
|
||||
}
|
||||
|
||||
type Trigger struct {
|
||||
Body string `json:"body"`
|
||||
ID string `json:"id"`
|
||||
TriggerOperation string `json:"triggerOperation"`
|
||||
TriggerType string `json:"triggerType"`
|
||||
Rid string `json:"_rid"`
|
||||
Ts int `json:"_ts"`
|
||||
Self string `json:"_self"`
|
||||
Etag string `json:"_etag"`
|
||||
Internals struct {
|
||||
DatabaseId string
|
||||
CollectionId string
|
||||
}
|
||||
}
|
||||
|
||||
type Document map[string]interface{}
|
||||
|
||||
type PartitionKeyRange struct {
|
||||
Rid string `json:"_rid"`
|
||||
ID string `json:"id"`
|
||||
Etag string `json:"_etag"`
|
||||
MinInclusive string `json:"minInclusive"`
|
||||
MaxExclusive string `json:"maxExclusive"`
|
||||
RidPrefix int `json:"ridPrefix"`
|
||||
Self string `json:"_self"`
|
||||
ThroughputFraction int `json:"throughputFraction"`
|
||||
Status string `json:"status"`
|
||||
Parents []any `json:"parents"`
|
||||
Ts int `json:"_ts"`
|
||||
Lsn int `json:"lsn"`
|
||||
}
|
||||
32
internal/struct_hidrators/collection_defaults.go
Normal file
32
internal/struct_hidrators/collection_defaults.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package structhidrators
|
||||
|
||||
import (
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
var defaultCollection repositorymodels.Collection = repositorymodels.Collection{
|
||||
IndexingPolicy: repositorymodels.CollectionIndexingPolicy{
|
||||
IndexingMode: "consistent",
|
||||
Automatic: true,
|
||||
IncludedPaths: []repositorymodels.CollectionIndexingPolicyPath{
|
||||
{Path: "/*"},
|
||||
},
|
||||
ExcludedPaths: []repositorymodels.CollectionIndexingPolicyPath{
|
||||
{Path: "/\"_etag\"/?"},
|
||||
},
|
||||
},
|
||||
PartitionKey: repositorymodels.CollectionPartitionKey{
|
||||
Paths: []string{"/_partitionKey"},
|
||||
Kind: "Hash",
|
||||
Version: 2,
|
||||
},
|
||||
Rid: "nFFFFFFFFFF=",
|
||||
Ts: 0,
|
||||
Self: "",
|
||||
Etag: "\"00000000-0000-0000-0000-000000000000\"",
|
||||
Docs: "docs/",
|
||||
Sprocs: "sprocs/",
|
||||
Triggers: "triggers/",
|
||||
Udfs: "udfs/",
|
||||
Conflicts: "conflicts/",
|
||||
}
|
||||
60
internal/struct_hidrators/hidrate.go
Normal file
60
internal/struct_hidrators/hidrate.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package structhidrators
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
repositorymodels "github.com/pikami/cosmium/internal/repository_models"
|
||||
)
|
||||
|
||||
func Hidrate(input interface{}) interface{} {
|
||||
if reflect.TypeOf(input) == reflect.TypeOf(repositorymodels.Collection{}) {
|
||||
return hidrate(input, defaultCollection)
|
||||
}
|
||||
return input
|
||||
}
|
||||
|
||||
func hidrate(input interface{}, defaults interface{}) interface{} {
|
||||
inputVal := reflect.ValueOf(input)
|
||||
defaultsVal := reflect.ValueOf(defaults)
|
||||
|
||||
if inputVal.Kind() != reflect.Struct || defaultsVal.Kind() != reflect.Struct {
|
||||
panic("Both input and defaults must be structs")
|
||||
}
|
||||
|
||||
output := reflect.New(inputVal.Type()).Elem()
|
||||
for i := 0; i < inputVal.NumField(); i++ {
|
||||
inputField := inputVal.Field(i)
|
||||
defaultField := defaultsVal.Field(i)
|
||||
|
||||
if inputField.Kind() == reflect.Struct && defaultField.Kind() == reflect.Struct {
|
||||
filledNested := hidrate(inputField.Interface(), defaultField.Interface())
|
||||
output.Field(i).Set(reflect.ValueOf(filledNested))
|
||||
} else {
|
||||
if isEmptyValue(inputField) {
|
||||
output.Field(i).Set(defaultField)
|
||||
} else {
|
||||
output.Field(i).Set(inputField)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return output.Interface()
|
||||
}
|
||||
|
||||
func isEmptyValue(v reflect.Value) bool {
|
||||
switch v.Kind() {
|
||||
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
|
||||
return v.Len() == 0
|
||||
case reflect.Bool:
|
||||
return !v.Bool()
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return v.Int() == 0
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
return v.Uint() == 0
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return v.Float() == 0
|
||||
case reflect.Interface, reflect.Ptr:
|
||||
return v.IsNil()
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user