Skip to content

Commit

Permalink
Property change (#13)
Browse files Browse the repository at this point in the history
Co-authored-by: Tiger Wang <[email protected]>
  • Loading branch information
LinkLeong and tigerinus authored Nov 23, 2022
1 parent 81c9cec commit c1a8245
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 94 deletions.
47 changes: 17 additions & 30 deletions api/message_bus/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,6 @@ components:
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/Property"

RegisterActionType:
Expand All @@ -348,9 +346,7 @@ components:
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/Property"
$ref: "#/components/schemas/Property"

responses:
ResponseInternalServerError:
Expand Down Expand Up @@ -465,6 +461,12 @@ components:
type: string
description: property name
example: "local-storage:vendor"
description:
type: string
example: "Hardware vendor of this storage"
example:
type: string
example: "SanDisk"

EventType:
type: object
Expand Down Expand Up @@ -495,19 +497,8 @@ components:

Property:
type: object
required:
- "name"
- "value"
properties:
name:
type: string
description: property name
example: "local-storage:vendor"
value:
type: string
description: property value
example: "SanDisk"

additionalProperties:
type: string
Event:
type: object
required:
Expand All @@ -524,17 +515,14 @@ components:
description: event name
example: "local-storage:disk:added"
properties:
type: array
type: object
description: event properties
items:
$ref: "#/components/schemas/Property"
additionalProperties:
type: string
example:
- name: local-storage:vendor
value: SanDisk
- name: local-storage:model
value: Cruzer
- name: local-storage:uuid
value: 442e0e5b-9d3e-4fe8-b46f-9c4141fdecd7
local-storage:vendor: SanDisk
local-storage:model: Cruzer
local-storage:uuid: 442e0e5b-9d3e-4fe8-b46f-9c4141fdecd7
timestamp:
type: string
description: timestamp this event took place
Expand Down Expand Up @@ -575,10 +563,9 @@ components:
description: action name
example: "local-storage:disk:format"
properties:
type: array
type: object
description: action properties
items:
$ref: "#/components/schemas/Property"
$ref: "#/components/schemas/Property"
timestamp:
type: string
description: timestamp this action took place
Expand Down
30 changes: 15 additions & 15 deletions model/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ type EventType struct {
}

type Event struct {
ID uint `gorm:"primaryKey"`
SourceID string `gorm:"index"`
Name string `gorm:"index"`
Properties []Property `gorm:"foreignKey:Id"`
Timestamp int64 `gorm:"autoCreateTime:milli"`
ID uint `gorm:"primaryKey"`
SourceID string `gorm:"index"`
Name string `gorm:"index"`
Properties map[string]string `gorm:"foreignKey:Id"`
Timestamp int64 `gorm:"autoCreateTime:milli"`
}

type ActionType struct {
Expand All @@ -27,22 +27,22 @@ type ActionType struct {
}

type Action struct {
ID uint `gorm:"primaryKey"`
SourceID string `gorm:"index"`
Name string `gorm:"index"`
Properties []Property `gorm:"foreignKey:Id"`
Timestamp int64 `gorm:"autoCreateTime:milli"`
ID uint `gorm:"primaryKey"`
SourceID string `gorm:"index"`
Name string `gorm:"index"`
Properties map[string]string `gorm:"foreignKey:Id"`
Timestamp int64 `gorm:"autoCreateTime:milli"`
}

type PropertyType struct {
Name string `gorm:"primaryKey"`
}

type Property struct {
ID uint `gorm:"primaryKey"`
Name string
Value string
}
// type Property struct {
// ID uint `gorm:"primaryKey"`
// Name string
// Value string
// }

type GenericType struct {
SourceID string `gorm:"primaryKey"`
Expand Down
6 changes: 1 addition & 5 deletions route/adapter/in/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import (
)

func ActionAdapter(action codegen.Action) model.Action {
properties := make([]model.Property, 0)
for _, property := range *action.Properties {
properties = append(properties, PropertyAdapter(property))
}

var timestamp int64
if action.Timestamp != nil {
Expand All @@ -19,7 +15,7 @@ func ActionAdapter(action codegen.Action) model.Action {
return model.Action{
SourceID: *action.SourceID,
Name: *action.Name,
Properties: properties,
Properties: *action.Properties,
Timestamp: timestamp,
}
}
10 changes: 5 additions & 5 deletions route/adapter/in/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
)

func EventAdapter(event codegen.Event) model.Event {
properties := make([]model.Property, 0)
for _, property := range event.Properties {
properties = append(properties, PropertyAdapter(property))
}
// properties := make([]model.Property, 0)
// for _, property := range {
// properties = append(properties, PropertyAdapter(property))
// }

var timestamp int64
if event.Timestamp != nil {
Expand All @@ -19,7 +19,7 @@ func EventAdapter(event codegen.Event) model.Event {
return model.Event{
SourceID: event.SourceID,
Name: event.Name,
Properties: properties,
Properties: event.Properties,
Timestamp: timestamp,
}
}
17 changes: 6 additions & 11 deletions route/adapter/in/property.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package in

import (
"github.com/IceWhaleTech/CasaOS-MessageBus/codegen"
"github.com/IceWhaleTech/CasaOS-MessageBus/model"
)

func PropertyAdapter(property codegen.Property) model.Property {
return model.Property{
Name: property.Name,
Value: property.Value,
}
}
// func PropertyAdapter(property codegen.Property) model.Property {
// return model.Property{
// Name: property.Name,
// Value: property.Value,
// }
// }
9 changes: 5 additions & 4 deletions route/adapter/out/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
)

func ActionAdapter(action model.Action) codegen.Action {
properties := make([]codegen.Property, 0)
for _, property := range action.Properties {
properties = append(properties, PropertyAdapter(property))
}
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,
Expand Down
10 changes: 5 additions & 5 deletions route/adapter/out/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
)

func EventAdapter(event model.Event) codegen.Event {
properties := make([]codegen.Property, 0)
for _, property := range event.Properties {
properties = append(properties, PropertyAdapter(property))
}
// properties := make([]codegen.Property, 0)
// for _, property := range event.Properties {
// properties = append(properties, PropertyAdapter(property))
// }

return codegen.Event{
SourceID: event.SourceID,
Name: event.Name,
Properties: properties,
Properties: event.Properties,
Timestamp: utils.Ptr(time.Unix(event.Timestamp, 0)),
}
}
17 changes: 6 additions & 11 deletions route/adapter/out/property.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package out

import (
"github.com/IceWhaleTech/CasaOS-MessageBus/codegen"
"github.com/IceWhaleTech/CasaOS-MessageBus/model"
)

func PropertyAdapter(property model.Property) codegen.Property {
return codegen.Property{
Name: property.Name,
Value: property.Value,
}
}
// func PropertyAdapter(property model.Property) codegen.Property {
// return codegen.Property{
// Name: property.Name,
// Value: property.Value,
// }
// }
6 changes: 4 additions & 2 deletions route/api_route_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,18 @@ func (r *APIRoute) TriggerAction(c echo.Context, sourceID codegen.SourceID, name
return c.JSON(http.StatusNotFound, codegen.ResponseNotFound{Message: utils.Ptr("not found")})
}

var properties []codegen.Property
var properties map[string]string
if err := c.Bind(&properties); err != nil {
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: &properties,
Properties: &pro,
Timestamp: utils.Ptr(time.Now()),
}

Expand Down
14 changes: 12 additions & 2 deletions route/api_route_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package route

import (
"fmt"
"io/ioutil"
"net"
"net/http"
"time"
Expand Down Expand Up @@ -86,10 +87,19 @@ func (r *APIRoute) PublishEvent(ctx echo.Context, sourceID codegen.SourceID, nam
return ctx.JSON(http.StatusNotFound, codegen.ResponseNotFound{Message: utils.Ptr("not found")})
}

var properties []codegen.Property
if err := ctx.Bind(&properties); err != nil {
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})
}
}

event := codegen.Event{
Expand Down
8 changes: 4 additions & 4 deletions service/event_type_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ func TestEventTypeService(t *testing.T) {
expectedEvent := model.Event{
SourceID: sourceID,
Name: name,
Properties: []model.Property{
{Name: "Property1", Value: "Value1"},
{Name: "Property2", Value: "Value2"},
},
// Properties: []model.Property{
// {Name: "Property1", Value: "Value1"},
// {Name: "Property2", Value: "Value2"},
// },
}

actualEvent1, err := service.Publish(expectedEvent)
Expand Down

0 comments on commit c1a8245

Please sign in to comment.