Skip to content

Commit

Permalink
update Action schema to align with Event schema in OpenAPI (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerinus authored Nov 25, 2022
1 parent 8ec941d commit b4dd5a7
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 39 deletions.
33 changes: 25 additions & 8 deletions api/message_bus/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,14 @@ components:
content:
application/json:
schema:
$ref: "#/components/schemas/Property"
type: object
description: event properties
additionalProperties:
type: string
example:
local-storage:vendor: SanDisk
local-storage:model: Cruzer
local-storage:uuid: 442e0e5b-9d3e-4fe8-b46f-9c4141fdecd7

RegisterActionType:
description: (TODO)
Expand All @@ -346,7 +353,12 @@ components:
content:
application/json:
schema:
$ref: "#/components/schemas/Property"
type: object
description: action properties
additionalProperties:
type: string
example:
local-storage:uuid: 442e0e5b-9d3e-4fe8-b46f-9c4141fdecd7

responses:
ResponseInternalServerError:
Expand Down Expand Up @@ -495,10 +507,6 @@ components:
- "local-storage:model"
- "local-storage:uuid"

Property:
type: object
additionalProperties:
type: string
Event:
type: object
required:
Expand Down Expand Up @@ -557,6 +565,10 @@ components:

Action:
type: object
required:
- "sourceID"
- "name"
- "properties"
properties:
sourceID:
type: string
Expand All @@ -568,8 +580,13 @@ components:
example: "local-storage:disk:format"
properties:
type: object
description: action properties
$ref: "#/components/schemas/Property"
description: event properties
additionalProperties:
type: string
example:
local-storage:vendor: SanDisk
local-storage:model: Cruzer
local-storage:uuid: 442e0e5b-9d3e-4fe8-b46f-9c4141fdecd7
timestamp:
type: string
description: timestamp this action took place
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/IceWhaleTech/CasaOS-MessageBus
go 1.19

require (
github.com/IceWhaleTech/CasaOS-Common v0.3.8-alpha1
github.com/IceWhaleTech/CasaOS-Common v0.4.0-alpha1
github.com/gobwas/ws v1.1.0
github.com/json-iterator/go v1.1.12
go.uber.org/goleak v1.1.11
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
github.com/IceWhaleTech/CasaOS-Common v0.3.8-alpha1 h1:tTZm14+aUP8vs9Ux2tSD7Z6ZRrlvanj8/tQDxGxgR28=
github.com/IceWhaleTech/CasaOS-Common v0.3.8-alpha1/go.mod h1:2MiivEMzvh41codhEKUcn46WK3Ffesop/04qa9jsvQk=
github.com/IceWhaleTech/CasaOS-Common v0.4.0-alpha1 h1:JeHZAwIriXKGVbaA65FQu/L80nwxb8oF/de5vZp5eHQ=
github.com/IceWhaleTech/CasaOS-Common v0.4.0-alpha1/go.mod h1:xcemiRsXcs1zrmQxYMyExDjZ7UHYwkJqYE71IDIV0xA=
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
github.com/andybalholm/brotli v1.0.1 h1:KqhlKozYbRtJvsPrrEeXcO+N2l6NYT5A2QAFmSULpEc=
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
Expand Down
2 changes: 1 addition & 1 deletion model/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Event struct {
Name string `gorm:"index"`
Properties map[string]string `gorm:"foreignKey:Id"`
Timestamp int64 `gorm:"autoCreateTime:milli"`
Uuid string `json:"uuid,omitempty"`
UUID string `json:"uuid,omitempty"`
}

type ActionType struct {
Expand Down
7 changes: 3 additions & 4 deletions route/adapter/in/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ import (
)

func ActionAdapter(action codegen.Action) model.Action {

var timestamp int64
if action.Timestamp != nil {
timestamp = action.Timestamp.Unix()
}

return model.Action{
SourceID: *action.SourceID,
Name: *action.Name,
Properties: *action.Properties,
SourceID: action.SourceID,
Name: action.Name,
Properties: action.Properties,
Timestamp: timestamp,
}
}
2 changes: 1 addition & 1 deletion route/adapter/in/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func EventAdapter(event codegen.Event) model.Event {
SourceID: event.SourceID,
Name: event.Name,
Properties: event.Properties,
Uuid: *event.Uuid,
UUID: *event.Uuid,

Timestamp: timestamp,
}
Expand Down
12 changes: 3 additions & 9 deletions route/adapter/out/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,10 @@ import (
)

func ActionAdapter(action model.Action) codegen.Action {
properties := make(codegen.Property)
// for k, v := range action.Properties {
// properties = append(properties, codegen.Property{Name: k, Value: v})
// }
properties = action.Properties

return codegen.Action{
SourceID: &action.SourceID,
Name: &action.Name,
Properties: &properties,
SourceID: action.SourceID,
Name: action.Name,
Properties: action.Properties,
Timestamp: utils.Ptr(time.Unix(action.Timestamp, 0)),
}
}
2 changes: 1 addition & 1 deletion route/adapter/out/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ func EventAdapter(event model.Event) codegen.Event {
Name: event.Name,
Properties: event.Properties,
Timestamp: utils.Ptr(time.Unix(event.Timestamp, 0)),
Uuid: &event.Uuid,
Uuid: &event.UUID,
}
}
8 changes: 3 additions & 5 deletions route/api_route_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,11 @@ func (r *APIRoute) TriggerAction(c echo.Context, sourceID codegen.SourceID, name
message := err.Error()
return c.JSON(http.StatusBadRequest, codegen.ResponseBadRequest{Message: &message})
}
pro := codegen.Property{}
pro = properties

action := codegen.Action{
SourceID: &sourceID,
Name: &name,
Properties: &pro,
SourceID: sourceID,
Name: name,
Properties: properties,
Timestamp: utils.Ptr(time.Now()),
}

Expand Down
13 changes: 6 additions & 7 deletions route/api_route_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,17 @@ func (r *APIRoute) PublishEvent(ctx echo.Context, sourceID codegen.SourceID, nam

var properties map[string]string
body, err := ioutil.ReadAll(ctx.Request().Body)

if err != nil {

message := err.Error()
return ctx.JSON(http.StatusBadRequest, codegen.ResponseBadRequest{Message: &message})
} else {
err = json.Unmarshal(body, &properties)
if err != nil {
message := err.Error()
return ctx.JSON(http.StatusBadRequest, codegen.ResponseBadRequest{Message: &message})
}
}

if err = json.Unmarshal(body, &properties); err != nil {
message := err.Error()
return ctx.JSON(http.StatusBadRequest, codegen.ResponseBadRequest{Message: &message})
}

uuidStr := uuid.New().String()
event := codegen.Event{
SourceID: sourceID,
Expand Down

0 comments on commit b4dd5a7

Please sign in to comment.