-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
types declaration and json marshaling
- Loading branch information
Showing
10 changed files
with
660 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module ogreetypes | ||
|
||
go 1.19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.