This repository has been archived on 2021-09-07. You can view files and clone it, but cannot push or open issues or pull requests.

82 lines
1.2 KiB
Go
Raw Permalink Normal View History

2018-01-01 21:29:51 +03:00
package chain_of_responsibility
import (
"errors"
"fmt"
"encoding/json"
)
type Handler interface {
Handle(interface{}) error
}
type Book struct {
Title string
Author string
}
func (b Book) String() string {
return fmt.Sprintf("%b", b)
}
type Deserializer struct {
next Handler
}
func (h *Deserializer) Handle(data interface{}) error {
var book Book
bytes, ok := data.([]byte)
if !ok {
return errors.New("Unsupported type!")
}
if err := json.Unmarshal(bytes, &book); err != nil {
return err
}
if h.next != nil {
return h.next.Handle(book)
}
return nil
}
type Logger struct {
next Handler
}
func (h *Logger) Handle(data interface{}) error {
book, ok := data.(Book)
if !ok {
return errors.New("Argument is not a Book instance")
}
fmt.Println("Book received! Titlle: '%s' Author: '%s", book.Title, book.Author)
if h.next != nil {
return h.next.Handle(book)
}
return nil
}
type BookDb struct {
store map[string]Book
next Handler
}
func (h *BookDb) Handle(data interface{}) error {
book, ok := data.(Book)
if !ok {
return errors.New("Argument is not a Book instance")
}
h.store[book.Title]=book
if h.next != nil {
return h.next.Handle(book)
}
return nil
}