commit ae4354d73b85d27ecf5d0d07074015d546864b0a
parent af0d10c7f25199b95807f7f12cc4c2b48a1b552f
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Fri, 4 Oct 2024 10:47:35 -0700
Move ObjectID stuff to it's own file
I'm going to start using these IDs on other objects such as users
and floorplans themselves, so it makes sense to not have them in
the floorplan data specific file.
Also, an ObjectType type was added so object types only need to be
validated once, to reduce the need for error handling.
Diffstat:
2 files changed, 104 insertions(+), 72 deletions(-)
diff --git a/internal/backend/floorplan_data.go b/internal/backend/floorplan_data.go
@@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
- "strconv"
"strings"
)
@@ -84,11 +83,6 @@ type DBObject interface {
Delete(e *Env, tx *sql.Tx, user, floorplan string) (DBObject, error)
}
-type ObjectID struct {
- Type string
- Seq int64
-}
-
type FurnitureType struct {
Varieties map[string]FurnitureVariety `json:"varieties,omit_empty"`
}
@@ -102,21 +96,15 @@ type Mappable interface {
Key() any
}
-var (
- IDTypePoint = "pnt"
- IDTypePointMap = "pntmap"
- IDTypeFurniture = "fur"
- IDTypeFurnitureMap = "furmap"
-)
-var tables map[string]string
+var tables map[ObjectType]string
func init() {
- tables = map[string]string{
- "pnt": "points",
- "pntmap": "pointmaps",
- "fur": "furniture",
- "furmap": "furniture_maps",
+ tables = map[ObjectType]string{
+ IDTypePoint: "points",
+ IDTypePointMap: "pointmaps",
+ IDTypeFurniture: "furniture",
+ IDTypeFurnitureMap: "furniture_maps",
}
}
@@ -793,25 +781,7 @@ func parsePath(p string) (ObjectID, error) {
return id, nil
}
-func parseID(s string) (ObjectID, error) {
- a := strings.Split(s, "_")
- if len(a) != 2 {
- return ObjectID{}, errors.New(s + ": Invalid ID")
- }
-
- seq, err := strconv.ParseInt(a[1], 10, 64)
- if err != nil {
- return ObjectID{}, errors.New(s + ": Invalid ID (invalid sequence number)")
- }
-
- if (a[0] != IDTypePoint && a[0] != IDTypePointMap &&
- a[0] != IDTypeFurniture && a[0] != IDTypeFurnitureMap) {
- return ObjectID{}, errors.New("Invalid ID type")
- }
- return makeID(a[0], seq), nil
-}
-
-func pathIDType(p string) (string, error) {
+func pathIDType(p string) (ObjectType, error) {
switch p {
case "points":
return IDTypePoint, nil
@@ -906,46 +876,11 @@ func mapFurnitureMap(m FurnitureMap) (ObjectID, error) {
return m.id, nil
}
-func makeID(typ string, seq int64) ObjectID {
- return ObjectID{
- Type: typ,
- Seq: seq,
- }
-}
-
-func (o ObjectID) String() string {
- return fmt.Sprintf("%s_%d", o.Type, o.Seq)
-}
-
func (o ObjectID) Path() string {
/* Type should be verified, so this shouldn't panic */
return fmt.Sprintf("/%s/%s", tables[o.Type], o)
}
-func (id ObjectID) MarshalJSON() ([]byte, error) {
- return json.Marshal(id.String())
-}
-
-func (id *ObjectID) UnmarshalJSON(b []byte) error {
- var s string
- if err := json.Unmarshal(b, &s); err != nil {
- return err
- }
- d, err := parseID(s)
- *id = d
- return err
-}
-
-func (id ObjectID) MarshalText() ([]byte, error) {
- return []byte(id.String()), nil
-}
-
-func (id *ObjectID) UnmarshalText(b []byte) error {
- d, err := parseID(string(b))
- *id = d
- return err
-}
-
func (id ObjectID) Error(context string, reason error) PatchError {
return patchError(&id, context, reason)
}
diff --git a/internal/backend/id.go b/internal/backend/id.go
@@ -0,0 +1,97 @@
+package backend
+
+import (
+ "encoding/json"
+ "errors"
+ "fmt"
+ "strconv"
+ "strings"
+)
+
+type ObjectType string
+
+type ObjectID struct {
+ Type ObjectType
+ Seq int64
+}
+
+var (
+ IDTypePoint ObjectType = "pnt"
+ IDTypePointMap ObjectType = "pntmap"
+ IDTypeFurniture ObjectType = "fur"
+ IDTypeFurnitureMap ObjectType = "furmap"
+)
+
+var objectTypes []ObjectType
+
+func init() {
+ objectTypes = []ObjectType{
+ IDTypePoint,
+ IDTypePointMap,
+ IDTypeFurniture,
+ IDTypeFurnitureMap,
+ }
+}
+
+func parseID(s string) (ObjectID, error) {
+ a := strings.Split(s, "_")
+ if len(a) != 2 {
+ return ObjectID{}, errors.New(s + ": Invalid ID")
+ }
+
+ seq, err := strconv.ParseInt(a[1], 10, 64)
+ if err != nil {
+ return ObjectID{}, errors.New(s + ": Invalid ID (invalid sequence number)")
+ }
+
+ typ, err := objectType(a[0])
+ if err != nil {
+ return ObjectID{}, err
+ }
+ return makeID(typ, seq), nil
+}
+
+func makeID(typ ObjectType, seq int64) ObjectID {
+ return ObjectID{
+ Type: typ,
+ Seq: seq,
+ }
+}
+
+func objectType(typStr string) (ObjectType, error) {
+ typ := ObjectType(typStr)
+ for i := range objectTypes {
+ if typ == objectTypes[i] {
+ return typ, nil
+ }
+ }
+ return typ, errors.New(typStr + ": Invalid ID type")
+}
+
+func (o ObjectID) String() string {
+ return fmt.Sprintf("%s_%d", o.Type, o.Seq)
+}
+
+func (id ObjectID) MarshalJSON() ([]byte, error) {
+ return json.Marshal(id.String())
+}
+
+func (id *ObjectID) UnmarshalJSON(b []byte) error {
+ var s string
+ if err := json.Unmarshal(b, &s); err != nil {
+ return err
+ }
+ d, err := parseID(s)
+ *id = d
+ return err
+}
+
+func (id ObjectID) MarshalText() ([]byte, error) {
+ return []byte(id.String()), nil
+}
+
+func (id *ObjectID) UnmarshalText(b []byte) error {
+ d, err := parseID(string(b))
+ *id = d
+ return err
+}