fix: dependancies

This commit is contained in:
2018-11-07 20:26:48 +01:00
parent 4097c67a58
commit 54dd2191d6
2 changed files with 77 additions and 2 deletions

View File

@ -1,9 +1,43 @@
package types
import (
"encoding/json"
"fmt"
"io"
)
// TypeObject ...
type TypeObject interface {
EncodeToJSON(w io.Writer) error
DecodeFromJSON(r io.Reader) error
}
// Device ...
type Device struct {
DeviceID string `json:"device_id"`
}
// EncodeToJson needs a writer to write the struct into json string
func (d *Device) EncodeToJSON(w io.Writer) error {
encoder := json.NewEncoder(w)
encoder.SetIndent("", " ")
err := encoder.Encode(&d)
if err != nil {
return fmt.Errorf("Error in encoding struct to json: %v", err)
}
return nil
}
// DecodeFromJson decode a json string from a reader into a struct
func (d *Device) DecodeFromJSON(r io.Reader) error {
decoder := json.NewDecoder(r)
if err := decoder.Decode(&d); err != nil {
return fmt.Errorf("Can not unmarshal JSON: %v", err)
}
return nil
}
// Humidity ...
type Humidity struct {
HumidityID string `json:"humidity_id"`
HumidityValue string `json:"humidity_value"`
@ -11,9 +45,50 @@ type Humidity struct {
DeviceID string `json:"device_id"`
}
// EncodeToJson needs a writer to write the struct into json string
func (h *Humidity) EncodeToJSON(w io.Writer) error {
encoder := json.NewEncoder(w)
encoder.SetIndent("", " ")
err := encoder.Encode(&h)
if err != nil {
return fmt.Errorf("Error in encoding struct to json: %v", err)
}
return nil
}
// DecodeFromJson decode a json string from a reader into a struct
func (h *Humidity) DecodeFromJSON(r io.Reader) error {
decoder := json.NewDecoder(r)
if err := decoder.Decode(&h); err != nil {
return fmt.Errorf("Can not unmarshal JSON: %v", err)
}
return nil
}
// Temperature ...
type Temperature struct {
TemperatureID string `json:"temperature_id"`
TemperatureValue string `json:"temperature_value"`
TemperatureDate string `json:"temperature_date"`
DeviceID string `json:"device_id"`
}
// EncodeToJson needs a writer to write the struct into json string
func (t *Temperature) EncodeToJSON(w io.Writer) error {
encoder := json.NewEncoder(w)
encoder.SetIndent("", " ")
err := encoder.Encode(&t)
if err != nil {
return fmt.Errorf("Error in encoding struct to json: %v", err)
}
return nil
}
// DecodeFromJson decode a json string from a reader into a struct
func (t *Temperature) DecodeFromJSON(r io.Reader) error {
decoder := json.NewDecoder(r)
if err := decoder.Decode(&t); err != nil {
return fmt.Errorf("Can not unmarshal JSON: %v", err)
}
return nil
}