1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-05-01 13:48:56 +08:00

Merge pull request #114 from mainflux/godoc-fixes

Ensure codestyle adherence
This commit is contained in:
Drasko DRASKOVIC 2017-10-06 23:12:42 +02:00 committed by GitHub
commit 288f437b4d
10 changed files with 34 additions and 28 deletions

View File

@ -27,14 +27,14 @@ const (
envNatsURL string = "MESSAGE_WRITER_NATS_URL"
)
var logger *zap.Logger
type config struct {
Cluster string
Keyspace string
NatsURL string
}
var logger *zap.Logger = nil
func main() {
cfg := loadConfig()

View File

@ -13,6 +13,6 @@ func NewService(mr writer.MessageRepository) Service {
return &adapterService{mr}
}
func (as *adapterService) Send(msg writer.RawMessage) {
as.mr.Save(msg)
func (as *adapterService) Publish(msg writer.RawMessage) error {
return as.mr.Save(msg)
}

View File

@ -11,7 +11,7 @@ import (
func sendMessageEndpoint(svc http.Service) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
msg := request.(writer.RawMessage)
svc.Send(msg)
return nil, nil
err := svc.Publish(msg)
return nil, err
}
}

View File

@ -20,13 +20,13 @@ func NewLoggingService(logger log.Logger, s http.Service) http.Service {
return &loggingService{logger, s}
}
func (ls *loggingService) Send(msg writer.RawMessage) {
func (ls *loggingService) Publish(msg writer.RawMessage) error {
defer func(begin time.Time) {
ls.logger.Log(
"method", "send",
"method", "publish",
"took", time.Since(begin),
)
}(time.Now())
ls.Service.Send(msg)
return ls.Service.Publish(msg)
}

View File

@ -25,11 +25,11 @@ func NewMetricService(counter metrics.Counter, latency metrics.Histogram, s http
}
}
func (ms *metricService) Send(msg writer.RawMessage) {
func (ms *metricService) Publish(msg writer.RawMessage) error {
defer func(begin time.Time) {
ms.counter.With("method", "send").Add(1)
ms.latency.With("method", "send").Observe(time.Since(begin).Seconds())
ms.counter.With("method", "publish").Add(1)
ms.latency.With("method", "publish").Observe(time.Since(begin).Seconds())
}(time.Now())
ms.Service.Send(msg)
return ms.Service.Publish(msg)
}

View File

@ -48,7 +48,6 @@ func MakeHandler(svc adapter.Service) http.Handler {
}
func decodeRequest(_ context.Context, r *http.Request) (interface{}, error) {
ct, err := checkContentType(r)
if err != nil {
return nil, err

View File

@ -5,5 +5,7 @@ import "github.com/mainflux/mainflux/writer"
// Service specifies an API that must be fullfiled by the domain service
// implementation, and all of its decorators (e.g. logging & metrics).
type Service interface {
Send(writer.RawMessage)
// Publish accepts the raw SenML message and publishes it to the event bus
// for post processing.
Publish(writer.RawMessage) error
}

View File

@ -18,6 +18,7 @@ var clientTypes map[string]bool = map[string]bool{
"device": true,
}
// Validate returns an error if client representation is invalid.
func (c *Client) Validate() error {
if c.Type = strings.ToLower(c.Type); !clientTypes[c.Type] {
return ErrMalformedEntity

View File

@ -9,6 +9,7 @@ type User struct {
Password string
}
// Validate returns an error if user representation is invalid.
func (u *User) Validate() error {
if u.Email == "" || u.Password == "" {
return ErrMalformedEntity

View File

@ -31,28 +31,31 @@ func normalize(msg writer.RawMessage) ([]writer.Message, error) {
msgs := make([]writer.Message, len(nm.Records))
for k, v := range nm.Records {
m := writer.Message{}
m.Channel = msg.Channel
m.Publisher = msg.Publisher
m.Protocol = msg.Protocol
m := writer.Message{
Channel: msg.Channel,
Publisher: msg.Publisher,
Protocol: msg.Protocol,
Version: v.BaseVersion,
Name: v.Name,
Unit: v.Unit,
StringValue: v.StringValue,
DataValue: v.DataValue,
Time: v.Time,
UpdateTime: v.UpdateTime,
Link: v.Link,
}
m.Version = v.BaseVersion
m.Name = v.Name
m.Unit = v.Unit
if v.Value != nil {
m.Value = *v.Value
}
m.StringValue = v.StringValue
if v.BoolValue != nil {
m.BoolValue = *v.BoolValue
}
m.DataValue = v.DataValue
if v.Sum != nil {
m.ValueSum = *v.Sum
}
m.Time = v.Time
m.UpdateTime = v.UpdateTime
m.Link = v.Link
msgs[k] = m
}
@ -71,7 +74,6 @@ func (repo *msgRepository) Save(raw writer.RawMessage) error {
}
for _, msg := range msgs {
cql := `INSERT INTO messages_by_channel
(channel, id, publisher, protocol, bver, n, u, v, vs, vb, vd, s, t, ut, l)
VALUES (?, now(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
@ -79,6 +81,7 @@ func (repo *msgRepository) Save(raw writer.RawMessage) error {
err = repo.session.Query(cql, msg.Channel, msg.Publisher, msg.Protocol,
msg.Version, msg.Name, msg.Unit, msg.Value, msg.StringValue, msg.BoolValue,
msg.DataValue, msg.ValueSum, msg.Time, msg.UpdateTime, msg.Link).Exec()
if err != nil {
return err
}