44 lines
727 B
Go
44 lines
727 B
Go
|
package quote
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"path"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
func NewQuoteFromFile(fileName string) (*Quote, error) {
|
||
|
jsonBytes, err := os.ReadFile(fileName)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
var quJson QuoteJson
|
||
|
if err := json.Unmarshal(jsonBytes, &quJson); err != nil {
|
||
|
return nil, fmt.Errorf("%s: %v\n", fileName, err)
|
||
|
} else {
|
||
|
_, fileNameShort := path.Split(fileName)
|
||
|
id := strings.TrimSuffix(fileNameShort, ".json")
|
||
|
qu := &Quote{
|
||
|
id: id,
|
||
|
source: quJson.Source,
|
||
|
quote: quJson.Quote,
|
||
|
}
|
||
|
|
||
|
return qu, nil
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (qu *Quote) GetId() string {
|
||
|
return qu.id
|
||
|
}
|
||
|
|
||
|
func (qu *Quote) GetSource() string {
|
||
|
return qu.source
|
||
|
}
|
||
|
|
||
|
func (qu *Quote) GetQuote() string {
|
||
|
return qu.quote
|
||
|
}
|