Skip to content

Commit

Permalink
add a function to save a definition in YAML
Browse files Browse the repository at this point in the history
  • Loading branch information
ftl committed Dec 11, 2022
1 parent c24afe4 commit d0bf35c
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 51 deletions.
118 changes: 67 additions & 51 deletions definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ type Definition struct {
OfficialRules string `yaml:"official_rules"`
UploadURL string `yaml:"upload_url"`
UploadFormat string `yaml:"upload_format"`
Duration time.Duration `yaml:"duration"`
DurationConstraints []ConstrainedDuration `yaml:"duration-constraints"`
Breaks []ConstrainedDuration `yaml:"breaks"`
Categories []Category `yaml:"categories"`
Overlays []Overlay `yaml:"overlays"`
Modes []Mode `yaml:"modes"`
Bands []ContestBand `yaml:"bands"`
BandChangeRules []BandChangeRule `yaml:"band_change_rules"`
Duration time.Duration `yaml:"duration,omitempty"`
DurationConstraints []ConstrainedDuration `yaml:"duration-constraints,omitempty"`
Breaks []ConstrainedDuration `yaml:"breaks,omitempty"`
Categories []Category `yaml:"categories,omitempty"`
Overlays []Overlay `yaml:"overlays,omitempty"`
Modes []Mode `yaml:"modes,omitempty"`
Bands []ContestBand `yaml:"bands,omitempty"`
BandChangeRules []BandChangeRule `yaml:"band_change_rules,omitempty"`
Exchange []ExchangeDefinition `yaml:"exchange"`
Scoring Scoring `yaml:"scoring"`
Examples []Example `yaml:"examples"`
Examples []Example `yaml:"examples,omitempty"`
}

type ConstrainedDuration struct {
Expand All @@ -47,22 +47,22 @@ type BandChangeRule struct {

type Category struct {
Name string `yaml:"name"`
Operator OperatorMode `yaml:"operator"`
TX TXMode `yaml:"tx"`
Power PowerMode `yaml:"power"`
Bands BandMode `yaml:"bands"`
Modes []Mode `yaml:"modes"`
Assisted bool `yaml:"assisted"`
Operator OperatorMode `yaml:"operator,omitempty"`
TX TXMode `yaml:"tx,omitempty"`
Power PowerMode `yaml:"power,omitempty"`
Bands BandMode `yaml:"bands,omitempty"`
Modes []Mode `yaml:"modes,omitempty"`
Assisted bool `yaml:"assisted,omitempty"`
}

type ExchangeDefinition struct {
MyContinent []Continent `yaml:"my_continent"`
MyCountry []DXCCEntity `yaml:"my_country"`
TheirContinent []Continent `yaml:"their_continent"`
TheirCountry []DXCCEntity `yaml:"their_country"`
TheirWorkingCondition string `yaml:"their_working_condition"`
AdditionalWeight int `yaml:"additional_weight"`
Fields []ExchangeField `yaml:"fields"`
MyContinent []Continent `yaml:"my_continent,omitempty"`
MyCountry []DXCCEntity `yaml:"my_country,omitempty"`
TheirContinent []Continent `yaml:"their_continent,omitempty"`
TheirCountry []DXCCEntity `yaml:"their_country,omitempty"`
TheirWorkingCondition string `yaml:"their_working_condition,omitempty"`
AdditionalWeight int `yaml:"additional_weight,omitempty"`
Fields []ExchangeField `yaml:"fields,omitempty"`
}

type ExchangeField []Property
Expand All @@ -74,16 +74,16 @@ type Scoring struct {
}

type ScoringRule struct {
MyContinent []Continent `yaml:"my_continent"`
MyCountry []DXCCEntity `yaml:"my_country"`
TheirContinent []Continent `yaml:"their_continent"`
TheirCountry []DXCCEntity `yaml:"their_country"`
TheirWorkingCondition string `yaml:"their_working_condition"`
Bands []ContestBand `yaml:"bands"`
Property Property `yaml:"property"`
BandRule BandRule `yaml:"band_rule"`
AdditionalWeight int `yaml:"additional_weight"`
Value int `yaml:"value"`
MyContinent []Continent `yaml:"my_continent,omitempty"`
MyCountry []DXCCEntity `yaml:"my_country,omitempty"`
TheirContinent []Continent `yaml:"their_continent,omitempty"`
TheirCountry []DXCCEntity `yaml:"their_country,omitempty"`
TheirWorkingCondition string `yaml:"their_working_condition,omitempty"`
Bands []ContestBand `yaml:"bands,omitempty"`
Property Property `yaml:"property,omitempty"`
BandRule BandRule `yaml:"band_rule,omitempty"`
AdditionalWeight int `yaml:"additional_weight,omitempty"`
Value int `yaml:"value,omitempty"`
}

type Example struct {
Expand All @@ -93,20 +93,20 @@ type Example struct {
}

type SetupExample struct {
MyCall string `yaml:"my_call"`
MyContinent Continent `yaml:"my_continent"`
MyCountry DXCCEntity `yaml:"my_country"`
MyCall string `yaml:"my_call,omitempty"`
MyContinent Continent `yaml:"my_continent,omitempty"`
MyCountry DXCCEntity `yaml:"my_country,omitempty"`

GridLocator string `yaml:"grid_locator"`
Operators []string `yaml:"operators"`
GridLocator string `yaml:"grid_locator,omitempty"`
Operators []string `yaml:"operators,omitempty"`

OperatorMode OperatorMode `yaml:"operator_mode"`
Overlay Overlay `yaml:"overlay"`
Power PowerMode `yaml:"power"`
Bands []ContestBand `yaml:"bands"`
Modes []Mode `yaml:"modes"`
OperatorMode OperatorMode `yaml:"operator_mode,omitempty"`
Overlay Overlay `yaml:"overlay,omitempty"`
Power PowerMode `yaml:"power,omitempty"`
Bands []ContestBand `yaml:"bands,omitempty"`
Modes []Mode `yaml:"modes,omitempty"`

MyExchange QSOExchange `yaml:"my_exchange"`
MyExchange QSOExchange `yaml:"my_exchange,omitempty"`
}

func (s SetupExample) ToSetup() Setup {
Expand Down Expand Up @@ -142,16 +142,16 @@ func (s SetupExample) ToSetup() Setup {
}

type QSOExample struct {
TheirCall string `yaml:"their_call"`
TheirContinent Continent `yaml:"their_continent"`
TheirCountry DXCCEntity `yaml:"their_country"`
TheirCall string `yaml:"their_call,omitempty"`
TheirContinent Continent `yaml:"their_continent,omitempty"`
TheirCountry DXCCEntity `yaml:"their_country,omitempty"`

Timestamp time.Time `yaml:"time"`
Band ContestBand `yaml:"band"`
Mode Mode `yaml:"mode"`
Timestamp time.Time `yaml:"time,omitempty"`
Band ContestBand `yaml:"band,omitempty"`
Mode Mode `yaml:"mode,omitempty"`

MyExchange QSOExchange `yaml:"my_exchange"`
TheirExchange []string `yaml:"their_exchange"`
MyExchange QSOExchange `yaml:"my_exchange,omitempty"`
TheirExchange []string `yaml:"their_exchange,omitempty"`

Score QSOScore `yaml:",inline"`
}
Expand Down Expand Up @@ -191,6 +191,22 @@ func LoadDefinitionYAML(r io.Reader) (*Definition, error) {
return &result, nil
}

func SaveDefinitionYAML(w io.Writer, definition *Definition, withExamples bool) error {
if definition == nil {
return nil
}

encoder := yaml.NewEncoder(w)

if withExamples {
return encoder.Encode(definition)
}

definitionWithoutExamples := *definition
definitionWithoutExamples.Examples = nil
return encoder.Encode(definitionWithoutExamples)
}

func LoadSetupFromFile(filename string) (*Setup, error) {
file, err := os.Open(filename)
if err != nil {
Expand Down
46 changes: 46 additions & 0 deletions definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestLoadYAML(t *testing.T) {
Expand Down Expand Up @@ -195,3 +196,48 @@ scoring:
})
}
}

func TestSaveLoadYAMLRoundtrip(t *testing.T) {
names, err := IncludedDefinitionNames()
require.NoError(t, err)

for _, name := range names {
t.Run(name, func(t *testing.T) {
definition, err := IncludedDefinition(name)
require.NoError(t, err)

buffer := bytes.NewBuffer([]byte{})
err = SaveDefinitionYAML(buffer, definition, true)
assert.NoError(t, err, "save")

loadedDefinition, err := LoadDefinitionYAML(buffer)
assert.NoError(t, err, "load")

assert.Equal(t, *definition, *loadedDefinition)
})
}
}

func TestSaveYAMLWithoutExamples(t *testing.T) {
names, err := IncludedDefinitionNames()
require.NoError(t, err)

for _, name := range names {
t.Run(name, func(t *testing.T) {
definition, err := IncludedDefinition(name)
require.NoError(t, err)

definitionWithoutExamples := *definition
definitionWithoutExamples.Examples = nil

buffer := bytes.NewBuffer([]byte{})
err = SaveDefinitionYAML(buffer, definition, false)
assert.NoError(t, err, "save")

loadedDefinition, err := LoadDefinitionYAML(buffer)
assert.NoError(t, err, "load")

assert.Equal(t, definitionWithoutExamples, *loadedDefinition)
})
}
}

0 comments on commit d0bf35c

Please sign in to comment.