spruce/internal/cv/utils.go
Dan Anglin 9e0f5af2e6
fix: use JSON schema to auto-generate the CV type
- Use go generate and a third party tool called jsonschemagen to
  auto-generate the CV data type from the JSON schema.
- Update the schema by changing number to integer to ensure that the
  integers are set to the correct type.
- Refactor some code.
2023-08-18 23:47:41 +01:00

63 lines
1.3 KiB
Go

package cv
import (
"encoding/json"
"fmt"
"os"
"time"
)
// NewCVFromFile returns a new CV value from the given JSON file.
func NewCVFromFile(path string) (CV, error) {
file, err := os.Open(path)
if err != nil {
return CV{}, fmt.Errorf("unable to open %s; %w", path, err)
}
defer file.Close()
decoder := json.NewDecoder(file)
var c CV
if err = decoder.Decode(&c); err != nil {
return CV{}, fmt.Errorf("unable to decode JSON data; %w", err)
}
return c, nil
}
// NewCV returns a new value of type CV.
func NewCV(firstName, lastName, jobTitle string) CV {
cv := CV{
FirstName: firstName,
LastName: lastName,
JobTitle: jobTitle,
}
return cv
}
// After returns true if the Duration's end date is set after time t.
// An error is returned if the end date is not parsed successfully.
func (d Duration) After(t time.Time) (bool, error) {
endDate, err := d.End.Parse()
if err != nil {
return false, err
}
return endDate.After(t), nil
}
// Parse parses Date and returns a value of type time.Time.
// An error is returned if the parsing fails.
func (d Date) Parse() (time.Time, error) {
dateStr := fmt.Sprintf("%d-%02d-%02d", d.Year, d.Month, d.Day)
date, err := time.Parse(time.DateOnly, dateStr)
if err != nil {
return time.Time{}, fmt.Errorf("unable to parse the date; %w", err)
}
return date, nil
}