36 lines
637 B
Go
Executable File
36 lines
637 B
Go
Executable File
package storage
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
|
|
"github.com/buger/jsonparser"
|
|
)
|
|
|
|
const prefix_answer = "answer_"
|
|
|
|
type Answer struct {
|
|
Pair
|
|
Right bool `json:"right"`
|
|
}
|
|
|
|
func NewAnswer(text string) Answer {
|
|
return Answer{Pair: New(prefix_answer+NewID(), text)}
|
|
}
|
|
|
|
func IsAnswer(ID string) bool {
|
|
return strings.HasPrefix(ID, prefix_answer)
|
|
}
|
|
|
|
func (a *Answer) UnmarshalJSON(b []byte) error {
|
|
if err := json.Unmarshal(b, &a.Pair); err != nil {
|
|
return err
|
|
}
|
|
if right, err := jsonparser.GetBoolean(b, "right"); err != nil && err != jsonparser.KeyPathNotFoundError {
|
|
return err
|
|
} else {
|
|
a.Right = right
|
|
}
|
|
return nil
|
|
}
|