Skip to content

Commit

Permalink
fix: Enable scanning of JSON types from SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
Muirrum committed Oct 25, 2023
1 parent 779129e commit 96b35f5
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions internal/models/models.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package models

import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
)

type User struct {
Username string `json:"username" validate:"required"`
Password string `json:"password" validate:"required"`
Expand Down Expand Up @@ -32,8 +39,8 @@ type Stead struct {
Id int
Username string
Password string
Inventory map[string]interface{}
Ephemeral_statuses map[string]interface{}
Inventory JsonValue
Ephemeral_statuses JsonValue
}

type PlantKind string
Expand All @@ -45,6 +52,24 @@ const (
HVV PlantKind = "hvv"
)

type JsonValue map[string]interface{}

func (jval *JsonValue) Scan(val interface{}) error {
switch v := val.(type) {
case []byte:
json.Unmarshal(v, &jval)
return nil
case string:
json.Unmarshal([]byte(v), &jval)
return nil
default:
return errors.New(fmt.Sprintf("Unsupported type: %T", v))
}
}
func (jval *JsonValue) Value() (driver.Value, error) {
return json.Marshal(jval)
}

//func (s *PlantKind) Scan(value interface{}) error {
// asBytes, ok := value.([]byte)
// if !ok {
Expand Down

0 comments on commit 96b35f5

Please sign in to comment.