Skip to content

Commit

Permalink
types declaration and json marshaling
Browse files Browse the repository at this point in the history
  • Loading branch information
GeremWD committed Jun 13, 2023
1 parent 01d03de commit ced06a7
Show file tree
Hide file tree
Showing 10 changed files with 660 additions and 0 deletions.
83 changes: 83 additions & 0 deletions types/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package ogreetypes

import (
"encoding/json"
"fmt"
"time"
)

type Vector2 struct {
X float64 `json:"x"`
Y float64 `json:"y"`
}

type Vector2Wrapper struct {
v Vector2
}

func (v Vector2Wrapper) MarshalJSON() ([]byte, error) {
return json.Marshal(fmt.Sprintf("{\"x\":%f,\"y\":%f}", v.v.X, v.v.Y))
}

func (v *Vector2Wrapper) UnmarshalJSON(data []byte) error {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
return json.Unmarshal([]byte(s), &v.v)
}

type Vector3 struct {
X float64 `json:"x"`
Y float64 `json:"y"`
Z float64 `json:"z"`
}

type Vector3Wrapper struct {
v Vector3
}

func (v Vector3Wrapper) MarshalJSON() ([]byte, error) {
return json.Marshal(fmt.Sprintf("{\"x\":%f,\"y\":%f,\"z\":%f}", v.v.X, v.v.Y, v.v.Z))
}

func (v *Vector3Wrapper) UnmarshalJSON(data []byte) error {
var s string
err := json.Unmarshal(data, &s)
if err != nil {
return err
}
return json.Unmarshal([]byte(s), &v.v)
}

type Color string

type MetricImperialUnit string

const (
MetricMM MetricImperialUnit = "mm"
MetricCM MetricImperialUnit = "cm"
MetricM MetricImperialUnit = "m"
MetricF MetricImperialUnit = "f"
)

type FloorMetric string

const (
FloorM FloorMetric = "m"
FloorT FloorMetric = "t"
FloorF FloorMetric = "f"
)

type Slug string

type Header struct {
Description []string `json:"description"`
Domain string `json:"domain"`
CreatedDate *time.Time `json:"createdDate,omitempty"`
LastUpdated *time.Time `json:"lastUpdated,omitempty"`
Name string `json:"name"`
Id string `json:"id,omitempty"`
ParentId string `json:"parentId"`
}
73 changes: 73 additions & 0 deletions types/building.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package ogreetypes

import (
"encoding/json"
)

type BuildingTemplate struct {
Slug Slug `json:"slug"`
Center Vector2 `json:"center"`
SizeWDHm Vector3 `json:"sizeWDHm"`
Vertices []Vector2 `json:"vertices"`
}

func (b BuildingTemplate) MarshalJSON() ([]byte, error) {
type Alias BuildingTemplate
return json.Marshal(struct {
Category string `json:"category"`
Alias
}{
Category: "building",
Alias: Alias(b),
})
}

type Building struct {
Header `json:"-"`
Height float64 `json:"height"`
HeightUnit MetricImperialUnit `json:"heightUnit"`
PosXY Vector2 `json:"-"`
PosXYUnit MetricImperialUnit `json:"posXYUnit"`
Size Vector2 `json:"-"`
SizeUnit MetricImperialUnit `json:"sizeUnit"`
Rotation float64 `json:"rotation"`
Template string `json:"template,omitempty"`
}

type BuildingAlias Building

type BuildingJsonAttributes struct {
BuildingAlias
PosXYAux Vector2Wrapper `json:"posXY"`
SizeAux Vector2Wrapper `json:"size"`
}

type BuildingJson struct {
Category string `json:"category"`
Header
Attributes BuildingJsonAttributes `json:"attributes"`
}

func (b Building) MarshalJSON() ([]byte, error) {
return json.Marshal(BuildingJson{
Category: "building",
Header: b.Header,
Attributes: BuildingJsonAttributes{
BuildingAlias: BuildingAlias(b),
PosXYAux: Vector2Wrapper{b.PosXY},
SizeAux: Vector2Wrapper{b.Size},
},
})
}

func (b *Building) UnmarshalJSON(data []byte) error {
var bjson BuildingJson
if err := json.Unmarshal(data, &bjson); err != nil {
return err
}
*b = Building(bjson.Attributes.BuildingAlias)
b.Header = bjson.Header
b.PosXY = bjson.Attributes.PosXYAux.v
b.Size = bjson.Attributes.SizeAux.v
return nil
}
42 changes: 42 additions & 0 deletions types/corridor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ogreetypes

import "encoding/json"

type Temperature string

const (
COLD Temperature = "cold"
WARM Temperature = "warm"
)

type Corridor struct {
Header
Content string `json:"content"`
Temperature Temperature `json:"temperature"`
}

type CorridorAlias Corridor

type CorridorJson struct {
Category string `json:"category"`
Header
Attributes CorridorAlias `json:"attributes"`
}

func (c Corridor) MarshalJSON() ([]byte, error) {
return json.Marshal(CorridorJson{
Category: "corridor",
Header: c.Header,
Attributes: CorridorAlias(c),
})
}

func (c *Corridor) UnmarshalJSON(data []byte) error {
var cjson CorridorJson
if err := json.Unmarshal(data, &cjson); err != nil {
return err
}
*c = Corridor(cjson.Attributes)
c.Header = cjson.Header
return nil
}
64 changes: 64 additions & 0 deletions types/device.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ogreetypes

import "encoding/json"

type DeviceOrientation string

const (
DeviceFront DeviceOrientation = "front"
DeviceRear DeviceOrientation = "rear"
DeviceFrontflipped DeviceOrientation = "frontflipped"
DeviceRearflipped DeviceOrientation = "rearflipped"
)

type DeviceTemplate = RackTemplate

type Device struct {
Header
FbxModel string `json:"fbxModel,omitempty"`
Height float64 `json:"height"`
HeightUnit RackUnit `json:"heightUnit"`
Orientation DeviceOrientation `json:"orientation"`
Size Vector2 `json:"-"`
SizeUnit MetricImperialUnit `json:"sizeUnit"`
Slot string `json:"slot,omitempty"`
Template string `json:"template"`
Type string `json:"type,omitempty"`
PosU *int `json:"posU,omitempty"`
SizeU *int `json:"sizeU,omitempty"`
}

type DeviceAlias Device

type DeviceJsonAttributes struct {
DeviceAlias
SizeAux Vector2Wrapper `json:"size"`
}

type DeviceJson struct {
Category string `json:"category"`
Header
Attributes DeviceJsonAttributes `json:"attributes"`
}

func (d Device) MarshalJSON() ([]byte, error) {
return json.Marshal(DeviceJson{
Category: "device",
Header: d.Header,
Attributes: DeviceJsonAttributes{
DeviceAlias: DeviceAlias(d),
SizeAux: Vector2Wrapper{d.Size},
},
})
}

func (d *Device) UnmarshalJSON(data []byte) error {
var djson DeviceJson
if err := json.Unmarshal(data, &djson); err != nil {
return err
}
*d = Device(djson.Attributes.DeviceAlias)
d.Header = djson.Header
d.Size = djson.Attributes.SizeAux.v
return nil
}
3 changes: 3 additions & 0 deletions types/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module ogreetypes

go 1.19
34 changes: 34 additions & 0 deletions types/group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ogreetypes

import "encoding/json"

type Group struct {
Header `json:"-"`
Content string `json:"content"`
}

type GroupAlias Group

type GroupJson struct {
Category string `json:"category"`
Header
Attributes GroupAlias `json:"attributes"`
}

func (g Group) MarshalJSON() ([]byte, error) {
return json.Marshal(GroupJson{
Category: "group",
Header: g.Header,
Attributes: GroupAlias(g),
})
}

func (g *Group) UnmarshalJSON(data []byte) error {
var gjson GroupJson
if err := json.Unmarshal(data, &gjson); err != nil {
return err
}
*g = Group(gjson.Attributes)
g.Header = gjson.Header
return nil
}
Loading

0 comments on commit ced06a7

Please sign in to comment.