-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdefinition.go
More file actions
778 lines (671 loc) · 21.7 KB
/
definition.go
File metadata and controls
778 lines (671 loc) · 21.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
package conval
import (
"crypto/sha1"
"fmt"
"io"
"log"
"os"
"os/user"
"path/filepath"
"regexp"
"slices"
"strconv"
"strings"
"time"
"github.com/ftl/hamradio/callsign"
"github.com/ftl/hamradio/locator"
"github.com/ftl/hamradio/scp"
"github.com/ftl/localcopy"
"gopkg.in/yaml.v3"
)
// A ContestIdentifier aka. Cabrillo name is a unique identifier for a contest.
// See https://www.contestcalendar.com/cabnames.php
type ContestIdentifier string
// A Definition of a contest.
type Definition struct {
Name string `yaml:"name"`
Identifier ContestIdentifier `yaml:"identifier"`
OfficialRules string `yaml:"official_rules"`
ARRLCountryList bool `yaml:"arrl_country_list"`
UploadURL string `yaml:"upload_url"`
UploadFormat string `yaml:"upload_format"`
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"`
Properties []PropertyDefinition `yaml:"properties,omitempty"`
Exchange []ExchangeDefinition `yaml:"exchange"`
Scoring Scoring `yaml:"scoring"`
Examples []Example `yaml:"examples,omitempty"`
}
func (d Definition) ExchangeFields() []ExchangeField {
fieldCount := 0
for _, definition := range d.Exchange {
definitionFieldCount := len(definition.Fields)
if fieldCount < definitionFieldCount {
fieldCount = definitionFieldCount
}
}
result := make([]ExchangeField, fieldCount)
usedProperties := make([]map[Property]bool, fieldCount)
appendProperty := func(field ExchangeField, usedProperties map[Property]bool, property Property) (ExchangeField, map[Property]bool) {
if usedProperties[property] {
return field, usedProperties
}
usedProperties[property] = true
field = append(field, property)
return field, usedProperties
}
for _, definition := range d.Exchange {
for i := range result {
if usedProperties[i] == nil {
usedProperties[i] = make(map[Property]bool)
}
if i >= len(definition.Fields) {
result[i], usedProperties[i] = appendProperty(result[i], usedProperties[i], EmptyProperty)
continue
}
field := definition.Fields[i]
for _, property := range field {
result[i], usedProperties[i] = appendProperty(result[i], usedProperties[i], property)
}
}
}
return result
}
func (d Definition) MyPropertyGetter(property Property) (PropertyGetter, bool) {
getter, ok := myPropertyGetters[property]
return getter, ok
}
func (d Definition) MyQTCPropertyGetter(property Property) (QTCPropertyGetter, bool) {
getter, ok := myQTCPropertyGetters[property]
return getter, ok
}
func (d Definition) PropertyGetter(property Property) (PropertyGetter, bool) {
definition, ok := d.propertyDefinition(property)
if ok {
return definition, true
}
getter, ok := commonPropertyGetters[property]
return getter, ok
}
func (d Definition) QTCPropertyGetter(property Property) (QTCPropertyGetter, bool) {
definition, ok := d.propertyDefinition(property)
if ok {
return definition, true
}
getter, ok := commonQTCPropertyGetters[property]
return getter, ok
}
func (d Definition) PropertyValidator(property Property) (PropertyValidator, bool) {
definition, ok := d.propertyDefinition(property)
if ok {
return definition, true
}
validator, ok := commonPropertyValidators[property]
return validator, ok
}
func (d Definition) propertyDefinition(property Property) (*PropertyDefinition, bool) {
for i, definition := range d.Properties {
if definition.Name == property {
// use the address of the slice element to avoid copying
return &d.Properties[i], true
}
}
return nil, false
}
func (d Definition) HasQTCs() bool {
return d.Scoring.HasQTCs()
}
type ConstrainedDuration struct {
Constraint `yaml:",inline"`
Duration time.Duration `yaml:"duration"`
Mode DurationConstraintMode `yaml:"constraint_mode,omitempty"`
}
type DurationConstraintMode string
const (
// TotalTime counts from the timestamp of the first QSO until the timestamp of the last QSO without considering breaks.
TotalTime DurationConstraintMode = "total_time"
// OperationTime counts from the timestamp of the first QSO until the timestamp of the last QSO, breaks in between are subtracted.
OperationTime DurationConstraintMode = "operation_time"
)
type BandChangeRule struct {
Constraint `yaml:",inline"`
GracePeriod time.Duration `yaml:"grace_period"`
MultiplierException bool `yaml:"multiplier_exception"`
// see https://euhf.s5cc.eu/rules/euhfc_rules_latest.pdf, chapter 9 for more details
ChangesPerHour int `yaml:"changes_per_hour"`
IncludeModeChanges bool `yaml:"include_mode_changes"`
}
type Category struct {
Name string `yaml:"name"`
Operator OperatorMode `yaml:"operator_mode,omitempty"`
TX TXMode `yaml:"tx,omitempty"`
Power PowerMode `yaml:"power,omitempty"`
BandCount BandCount `yaml:"band_count"`
Bands []ContestBand `yaml:"bands,omitempty"`
Modes []Mode `yaml:"modes,omitempty"`
Assisted bool `yaml:"assisted,omitempty"`
Overlay Overlay `yaml:"overlay,omitempty"`
ScoreMode ScoreMode `yaml:"score_mode,omitempty"`
Duration time.Duration `yaml:"duration,omitempty"`
}
type ScoreMode string
const (
// StrictScore allows only the number of bands defined in the category (single, <any number>, all). If more bands were worked, the claimed score is zero.
StrictScore ScoreMode = "strict"
// BestScore counts only the best n bands, according to the number of bands defined in the category (single, <any number>, all).
BestScore ScoreMode = "best"
)
type PropertyDefinition struct {
Name Property `yaml:"name"`
Label string `yaml:"label,omitempty"`
Values []string `yaml:"values,omitempty"`
Expression string `yaml:"expression,omitempty"`
Source Property `yaml:"source,omitempty"`
MemberOf string `yaml:"member_of,omitempty"`
definition *Definition
re *regexp.Regexp
membersDB *scp.Database
membersCache map[string]string
}
func (d *PropertyDefinition) GetLabel() string {
if d.Label != "" {
return d.Label
}
return string(d.Name)
}
func (d *PropertyDefinition) ValidateProperty(value string, _ PrefixDatabase) error {
switch {
case d.Expression != "":
return d.validatePropertyExpression(value)
case len(d.Values) > 0:
return d.validatePropertyValue(value)
case d.MemberOf != "":
// MemberOf does not make use of the value, but of QSO.TheirCall; it does not matter if this is a valid callsign
return nil
default:
return fmt.Errorf("%s is not defined properly", d.GetLabel())
}
}
func (d *PropertyDefinition) validatePropertyExpression(value string) error {
if d.re == nil {
re, err := regexp.Compile(d.Expression)
if err != nil {
return err
}
d.re = re
}
sanitize := func(s string) string {
return strings.ToUpper(strings.TrimSpace(s))
}
sanitizedValue := sanitize(value)
match := d.re.FindString(sanitizedValue)
if len(match) == 0 || len(match) != len(sanitizedValue) {
return fmt.Errorf("%s is not a valid %s", value, d.GetLabel())
}
return nil
}
func (d *PropertyDefinition) validatePropertyValue(value string) error {
sanitize := func(s string) string {
return strings.ToLower(strings.TrimSpace(s))
}
sanitizedValue := sanitize(value)
for _, v := range d.Values {
if sanitizedValue == sanitize(v) {
return nil
}
}
return fmt.Errorf("%s is not a valid %s", value, d.GetLabel())
}
func (d *PropertyDefinition) GetProperty(qso QSO, setup Setup, prefixes PrefixDatabase) string {
switch {
case d.Source != "":
return d.getPropertyFromSource(qso, setup, prefixes)
case d.MemberOf != "":
return d.getMemberOfProperty(qso, setup, prefixes)
default:
return qso.TheirExchange[d.Name]
}
}
func (d *PropertyDefinition) getPropertyFromSource(qso QSO, setup Setup, prefixes PrefixDatabase) string {
getter, getterOK := d.definition.PropertyGetter(d.Source)
if !getterOK {
return ""
}
sourceValue := getter.GetProperty(qso, setup, prefixes)
sanitize := func(s string) string {
return strings.ToUpper(strings.TrimSpace(s))
}
sourceValue = sanitize(sourceValue)
if len(sourceValue) == 0 {
return ""
}
if d.re == nil {
re, err := regexp.Compile(d.Expression)
if err != nil {
return ""
}
d.re = re
}
matches := d.re.FindStringSubmatch(sourceValue)
if len(matches) != 2 {
return ""
}
return sanitize(matches[1])
}
func (d *PropertyDefinition) getMemberOfProperty(qso QSO, _ Setup, _ PrefixDatabase) string {
result := "false"
if d.membersDB == nil {
return result
}
theirCall := qso.TheirCall.String()
if fromCache, ok := d.membersCache[theirCall]; ok {
return fromCache
}
defer func() {
d.membersCache[theirCall] = result
}()
matches, err := d.membersDB.FindStrings(theirCall)
if err != nil {
return result
}
if slices.Contains(matches, theirCall) {
result = "true"
}
return result
}
func (d *PropertyDefinition) GetQTCProperty(qtc QTC, setup Setup, prefixes PrefixDatabase) string {
switch {
case d.Source != "":
return d.getQTCPropertyFromSource(qtc, setup, prefixes)
case d.MemberOf != "":
return d.getQTCMemberOfProperty(qtc, setup, prefixes)
default:
return ""
}
}
func (d *PropertyDefinition) getQTCPropertyFromSource(qtc QTC, setup Setup, prefixes PrefixDatabase) string {
getter, getterOK := d.definition.QTCPropertyGetter(d.Source)
if !getterOK {
return ""
}
sourceValue := getter.GetQTCProperty(qtc, setup, prefixes)
sanitize := func(s string) string {
return strings.ToUpper(strings.TrimSpace(s))
}
sourceValue = sanitize(sourceValue)
if len(sourceValue) == 0 {
return ""
}
if d.re == nil {
re, err := regexp.Compile(d.Expression)
if err != nil {
return ""
}
d.re = re
}
matches := d.re.FindStringSubmatch(sourceValue)
if len(matches) != 2 {
return ""
}
return sanitize(matches[1])
}
func (d *PropertyDefinition) getQTCMemberOfProperty(qtc QTC, _ Setup, _ PrefixDatabase) string {
result := "false"
if d.membersDB == nil {
return result
}
theirCall := qtc.TheirCall.String()
if fromCache, ok := d.membersCache[theirCall]; ok {
return fromCache
}
defer func() {
d.membersCache[theirCall] = result
}()
matches, err := d.membersDB.FindStrings(theirCall)
if err != nil {
return result
}
if slices.Contains(matches, theirCall) {
result = "true"
}
return result
}
type ExchangeDefinition struct {
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
func (f ExchangeField) Strings() []string {
result := make([]string, len(f))
for i, p := range f {
result[i] = string(p)
}
return result
}
func (f ExchangeField) Contains(property Property) bool {
return slices.Contains(f, property)
}
type Scoring struct {
QSORules []ScoringRule `yaml:"qsos,omitempty"`
QSOBandRule BandRule `yaml:"qso_band_rule,omitempty"`
QTCRules []ScoringRule `yaml:"qtcs,omitempty"`
MultiRules []ScoringRule `yaml:"multis,omitempty"`
MultiOperation MultiOperation `yaml:"multi_operation,omitempty"`
}
func (s Scoring) HasQTCs() bool {
return len(s.QTCRules) > 0
}
type ScoringRule struct {
MyContinent []Continent `yaml:"my_continent,omitempty"`
MyCountry []DXCCEntity `yaml:"my_country,omitempty"`
MyPrefix []string `yaml:"my_prefix,omitempty"`
MyWorkingCondition []string `yaml:"my_working_condition,omitempty"`
TheirContinent []Continent `yaml:"their_continent,omitempty"`
TheirCountry []DXCCEntity `yaml:"their_country,omitempty"`
TheirPrefix []string `yaml:"their_prefix,omitempty"`
TheirWorkingCondition []string `yaml:"their_working_condition,omitempty"`
Bands []ContestBand `yaml:"bands,omitempty"`
Property Property `yaml:"property,omitempty"` // only useful for multis
Except []string `yaml:"except,omitempty"` // only useful for multis
PropertyConstraints []PropertyConstraint `yaml:"property_constraints,omitempty"`
BandRule BandRule `yaml:"band_rule,omitempty"`
AdditionalWeight int `yaml:"additional_weight,omitempty"`
Value int `yaml:"value,omitempty"`
ValueOfProperty Property `yaml:"value_of_property,omitempty"`
QTCKind QTCKind `yaml:"kind,omitempty"` // only useful for QTCs
}
type MultiOperation string
const (
DefaultMultiOperation MultiOperation = ""
MultiplyMultis MultiOperation = "multiply"
AddMultis MultiOperation = "add"
)
type PropertyConstraint struct {
Name Property `yaml:"name"`
Min string `yaml:"min,omitempty"`
Max string `yaml:"max,omitempty"`
MyValue string `yaml:"my_value,omitempty"`
MyValueEmpty bool `yaml:"my_value_empty,omitempty"`
MyValueNotEmpty bool `yaml:"my_value_not_empty,omitempty"`
TheirValue string `yaml:"their_value,omitempty"`
TheirValueEmpty bool `yaml:"their_value_empty,omitempty"`
TheirValueNotEmpty bool `yaml:"their_value_not_empty,omitempty"`
SameValue bool `yaml:"same,omitempty"`
OtherValue bool `yaml:"other,omitempty"`
}
func (c PropertyConstraint) Matches(myValue string, theirValue string) bool {
myValue = sanitizePropertyValue(myValue)
theirValue = sanitizePropertyValue(theirValue)
result := true
if c.Min != "" || c.Max != "" {
result = result && c.matchesMinMax(theirValue)
}
if c.MyValue != "" {
result = result && myValue == c.MyValue
}
if c.MyValueEmpty {
result = result && (myValue == "")
} else if c.MyValueNotEmpty {
result = result && (myValue != "")
}
if c.TheirValue != "" {
result = result && (theirValue == c.TheirValue)
}
if c.TheirValueEmpty {
result = result && (theirValue == "")
} else if c.TheirValueNotEmpty {
result = result && (theirValue != "")
}
if c.SameValue {
result = result && myValue == theirValue
} else if c.OtherValue {
result = result && myValue != theirValue
}
return result
}
func (c PropertyConstraint) matchesMinMax(value string) bool {
intValue, err := strconv.Atoi(value)
if err != nil {
return false
}
if c.Min != "" && c.Max != "" {
min, err := strconv.Atoi(c.Min)
if err != nil {
return false
}
max, err := strconv.Atoi(c.Max)
if err != nil {
return false
}
if intValue < min || intValue > max {
return false
}
} else if c.Min != "" {
min, err := strconv.Atoi(c.Min)
if err != nil {
return false
}
if intValue < min {
return false
}
} else if c.Max != "" {
max, err := strconv.Atoi(c.Max)
if err != nil {
return false
}
if intValue > max {
return false
}
}
return true
}
func sanitizePropertyValue(s string) string {
return strings.ToLower(strings.TrimSpace(s))
}
type Example struct {
Setup SetupExample `yaml:"setup"`
QSOs []QSOExample `yaml:"qsos"`
QTCs []QTCExample `yaml:"qtcs,omitempty"`
Score ScoreExample `yaml:"score"`
}
type SetupExample struct {
MyCall string `yaml:"my_call,omitempty"`
MyContinent Continent `yaml:"my_continent,omitempty"`
MyCountry DXCCEntity `yaml:"my_country,omitempty"`
GridLocator string `yaml:"grid_locator,omitempty"`
Operators []string `yaml:"operators,omitempty"`
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,omitempty"`
}
func (s SetupExample) ToSetup() Setup {
myCall, err := callsign.Parse(s.MyCall)
if err != nil {
myCall = callsign.Callsign{}
}
gridLocator, err := locator.Parse(s.GridLocator)
if err != nil {
gridLocator = locator.Locator{}
}
operators := make([]callsign.Callsign, 0, len(s.Operators))
for _, operator := range s.Operators {
operatorCall, err := callsign.Parse(operator)
if err == nil {
operators = append(operators, operatorCall)
}
}
return Setup{
MyCall: myCall,
MyContinent: s.MyContinent,
MyCountry: s.MyCountry,
GridLocator: gridLocator,
Operators: operators,
OperatorMode: s.OperatorMode,
Overlay: s.Overlay,
Power: s.Power,
Bands: s.Bands,
Modes: s.Modes,
MyExchange: s.MyExchange,
}
}
type QSOExample struct {
TheirCall string `yaml:"their_call,omitempty"`
TheirContinent Continent `yaml:"their_continent,omitempty"`
TheirCountry DXCCEntity `yaml:"their_country,omitempty"`
Timestamp time.Time `yaml:"time,omitempty"`
Band ContestBand `yaml:"band,omitempty"`
Mode Mode `yaml:"mode,omitempty"`
TheirExchange []string `yaml:"their_exchange,omitempty"`
Score QSOScore `yaml:",inline"`
}
func (q QSOExample) ToQSO(fields []ExchangeField, myExchange QSOExchange, prefixes PrefixDatabase, propertyValidators PropertyValidators) QSO {
return QSO{
TheirCall: callsign.MustParse(q.TheirCall),
TheirContinent: q.TheirContinent,
TheirCountry: q.TheirCountry,
Timestamp: q.Timestamp,
Band: q.Band,
Mode: q.Mode,
MyExchange: myExchange,
TheirExchange: ParseExchange(fields, q.TheirExchange, prefixes, propertyValidators),
}
}
type QTCExample struct {
Kind string `yaml:"kind,omitempty"`
TheirCall string `yaml:"their_call,omitempty"`
TheirContinent Continent `yaml:"their_continent,omitempty"`
TheirCountry DXCCEntity `yaml:"their_country,omitempty"`
Header string `yaml:"header,omitempty"`
Band ContestBand `yaml:"band,omitempty"`
Mode Mode `yaml:"mode,omitempty"`
Count int `yaml:"count,omitempty"`
Score QTCScore `yaml:",inline"`
}
func (q QTCExample) ToQTC() QTC {
return QTC{
Kind: QTCKind(q.Kind),
TheirCall: callsign.MustParse(q.TheirCall),
TheirContinent: q.TheirContinent,
TheirCountry: q.TheirCountry,
Header: q.Header,
Band: q.Band,
Mode: q.Mode,
Count: q.Count,
}
}
type ScoreExample struct {
QSOs int `yaml:"qsos,omitempty"`
QTCs int `yaml:"qtcs,omitempty"`
Points int `yaml:"points,omitempty"`
Multis int `yaml:"multis,omitempty"`
Total int `yaml:"total,omitempty"`
}
func LoadDefinitionFromFile(filename string) (*Definition, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
return LoadDefinitionYAML(file)
}
func LoadDefinitionYAML(r io.Reader) (*Definition, error) {
decoder := yaml.NewDecoder(r)
var result Definition
err := decoder.Decode(&result)
if err != nil {
return nil, err
}
return initDefinition(&result), nil
}
func initDefinition(d *Definition) *Definition {
for i, pd := range d.Properties {
pd.definition = d
if pd.MemberOf != "" {
db, err := loadMembersDB(pd.MemberOf)
if err != nil {
log.Printf("failed to load members list from %s: %v", pd.MemberOf, err)
pd.membersDB = scp.NewDatabase()
} else {
pd.membersDB = db
}
pd.membersCache = make(map[string]string)
}
d.Properties[i] = pd
}
return d
}
func loadMembersDB(url string) (*scp.Database, error) {
filename, err := localMembersFilename(url)
if err != nil {
return nil, fmt.Errorf("failed to calculate the local filename for %s: %w", url, err)
}
_, err = localcopy.Update(url, filename, nil)
if err != nil {
return nil, fmt.Errorf("failed to update members database from %s: %w", url, err)
}
database, err := localcopy.LoadLocal(filename, func(r io.Reader) (any, error) {
return scp.ReadCallHistory(r)
})
if err != nil {
return nil, fmt.Errorf("failed to load members database from %s: %w", url, err)
}
return database.(*scp.Database), nil
}
func localMembersFilename(url string) (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}
path := filepath.Join(usr.HomeDir, ".cache", "conval")
hash := sha1.New()
_, err = hash.Write([]byte(url))
if err != nil {
return "", err
}
filename := fmt.Sprintf("%x.txt", hash.Sum(nil))
return filepath.Join(path, filename), 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 {
return nil, err
}
defer file.Close()
return LoadSetupYAML(file)
}
func LoadSetupYAML(r io.Reader) (*Setup, error) {
decoder := yaml.NewDecoder(r)
var setup SetupExample
err := decoder.Decode(&setup)
if err != nil {
return nil, err
}
result := setup.ToSetup()
return &result, nil
}