go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/nodes/pkg/dbmodel/node_value.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package dbmodel 9 10 import ( 11 "go.charczuk.com/projects/nodes/pkg/types" 12 "go.charczuk.com/sdk/uuid" 13 ) 14 15 // NodeValue is the current value for a given node 16 // in a graph. 17 type NodeValue struct { 18 GraphID uuid.UUID `db:"graph_id,pk"` 19 NodeID uuid.UUID `db:"node_id,pk"` 20 UserID uuid.UUID `db:"user_id"` 21 ValueType string `db:"value_type"` 22 Value any `db:"value,json"` 23 } 24 25 func (nv NodeValue) TableName() string { return "node_value" } 26 27 // ParsedValue returns the value according to the underlying type. 28 func (nv NodeValue) ParsedValue() (any, error) { 29 switch nv.ValueType { 30 case "table": 31 typed, ok := nv.Value.(map[string]any) 32 if !ok { 33 return new(types.Table), nil 34 } 35 return types.TableFromJSON(typed) 36 case "svg": 37 typed, ok := nv.Value.(map[string]any) 38 if !ok { 39 return types.SVG{}, nil 40 } 41 return types.SVGFromJSON(typed) 42 default: 43 return nv.Value, nil 44 } 45 } 46 47 func DetectValueType(value any) string { 48 switch value.(type) { 49 case *types.Table, types.Table: 50 return "table" 51 case *types.SVG, types.SVG: 52 return "svg" 53 default: 54 return "scalar" 55 } 56 }