Improve parsing error reporting

* Better validation error for WWFF and SOTA ref
* Better validation error for callsign
* Improve frequency parsing errors
pull/14/head
Jean-Marc MEESSEN 4 years ago committed by GitHub
parent 156ac9c875
commit d60bab231f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -137,10 +137,10 @@ func ParseLine(inputStr string, previousLine LogLine) (logLine LogLine, errorMsg
logLine.Frequency = fmt.Sprintf("%.3f", qrg) logLine.Frequency = fmt.Sprintf("%.3f", qrg)
} else { } else {
logLine.Frequency = "" logLine.Frequency = ""
errorMsg = errorMsg + " Frequency " + element + " is invalid for " + logLine.Band + " band" errorMsg = errorMsg + "Frequency [" + element + "] is invalid for " + logLine.Band + " band."
} }
} else { } else {
errorMsg = errorMsg + " Unable to load frequency " + element + ": no band defined." errorMsg = errorMsg + "Unable to load frequency [" + element + "]: no band defined for that frequency."
} }
continue continue
} }
@ -214,7 +214,7 @@ func ParseLine(inputStr string, previousLine LogLine) (logLine LogLine, errorMsg
workRST = element workRST = element
} else { } else {
workRST = "*" + element workRST = "*" + element
errorMsg = errorMsg + "Invalid report (" + element + ") for " + logLine.ModeType + " mode " errorMsg = errorMsg + "Invalid report [" + element + "] for " + logLine.ModeType + " mode."
} }
} }
if haveSentRST { if haveSentRST {
@ -248,7 +248,7 @@ func ParseLine(inputStr string, previousLine LogLine) (logLine LogLine, errorMsg
} }
//If we come here, we could not make sense of what we found //If we come here, we could not make sense of what we found
errorMsg = errorMsg + "Unable to parse " + element + " " errorMsg = errorMsg + "Unable to make sense of [" + element + "]."
} }

@ -54,7 +54,7 @@ func TestParseLine(t *testing.T) {
{ {
"Wrong mode", "Wrong mode",
args{inputStr: "cww", previousLine: LogLine{Mode: "SSB"}}, args{inputStr: "cww", previousLine: LogLine{Mode: "SSB"}},
LogLine{Mode: "SSB", RSTsent: "59", RSTrcvd: "59"}, "Unable to parse cww ", LogLine{Mode: "SSB", RSTsent: "59", RSTrcvd: "59"}, "Unable to make sense of [cww].",
}, },
{ {
"Parse OM name", "Parse OM name",
@ -74,7 +74,7 @@ func TestParseLine(t *testing.T) {
{ {
"Parse frequency out of limit", "Parse frequency out of limit",
args{inputStr: "14.453 on4kjm", previousLine: LogLine{Mode: "SSB", Band: "20m", BandLowerLimit: 14.0, BandUpperLimit: 14.35}}, args{inputStr: "14.453 on4kjm", previousLine: LogLine{Mode: "SSB", Band: "20m", BandLowerLimit: 14.0, BandUpperLimit: 14.35}},
LogLine{Band: "20m", BandLowerLimit: 14.0, BandUpperLimit: 14.35, Call: "ON4KJM", Mode: "SSB", RSTsent: "59", RSTrcvd: "59"}, " Frequency 14.453 is invalid for 20m band", LogLine{Band: "20m", BandLowerLimit: 14.0, BandUpperLimit: 14.35, Call: "ON4KJM", Mode: "SSB", RSTsent: "59", RSTrcvd: "59"}, "Frequency [14.453] is invalid for 20m band.",
}, },
{ {
"parse partial RST (sent) - CW", "parse partial RST (sent) - CW",
@ -104,7 +104,7 @@ func TestParseLine(t *testing.T) {
{ {
"Incompatible report", "Incompatible report",
args{inputStr: "1230 on4kjm 5 599", previousLine: LogLine{Mode: "FM", ModeType: "PHONE"}}, args{inputStr: "1230 on4kjm 5 599", previousLine: LogLine{Mode: "FM", ModeType: "PHONE"}},
LogLine{Call: "ON4KJM", Time: "1230", ActualTime: "1230", RSTsent: "55", RSTrcvd: "*599", Mode: "FM", ModeType: "PHONE"}, "Invalid report (599) for PHONE mode ", LogLine{Call: "ON4KJM", Time: "1230", ActualTime: "1230", RSTsent: "55", RSTrcvd: "*599", Mode: "FM", ModeType: "PHONE"}, "Invalid report [599] for PHONE mode.",
}, },
{ {
"SOTA keywork ", "SOTA keywork ",

@ -33,7 +33,8 @@ func ValidateSota(inputStr string) (ref, errorMsg string) {
if validSotaRegexp.MatchString(inputStr) { if validSotaRegexp.MatchString(inputStr) {
return inputStr, "" return inputStr, ""
} }
return wrongInputStr, "Invalid SOTA reference" errorMsg = "[" + inputStr + "] is an invalid SOTA reference"
return wrongInputStr, errorMsg
} }
var validWwffRegexp = regexp.MustCompile(`^[\d]{0,1}[A-Z]{1,2}FF-[\d]{4}$`) var validWwffRegexp = regexp.MustCompile(`^[\d]{0,1}[A-Z]{1,2}FF-[\d]{4}$`)
@ -46,7 +47,8 @@ func ValidateWwff(inputStr string) (ref, errorMsg string) {
if validWwffRegexp.MatchString(inputStr) { if validWwffRegexp.MatchString(inputStr) {
return inputStr, "" return inputStr, ""
} }
return wrongInputStr, "Invalid WWFF reference" errorMsg = "[" + inputStr + "] is an invalid WWFF reference"
return wrongInputStr, errorMsg
} }
var validCallRegexp = regexp.MustCompile(`[\d]{0,1}[A-Z]{1,2}\d([A-Z]{1,4}|\d{3,3}|\d{1,3}[A-Z])[A-Z]{0,5}`) var validCallRegexp = regexp.MustCompile(`[\d]{0,1}[A-Z]{1,2}\d([A-Z]{1,4}|\d{3,3}|\d{1,3}[A-Z])[A-Z]{0,5}`)
@ -64,7 +66,7 @@ func ValidateCall(sign string) (call, errorMsg string) {
if validCallRegexp.MatchString(sign) { if validCallRegexp.MatchString(sign) {
return sign, "" return sign, ""
} }
return wrongSign, "Invalid call" return wrongSign, "[" + sign + "] is an invalid call"
case 2: case 2:
// some ambiguity here we need to resolve, could be a prefix or a suffix // some ambiguity here we need to resolve, could be a prefix or a suffix
if validCallRegexp.MatchString(sp[0]) { if validCallRegexp.MatchString(sp[0]) {
@ -74,26 +76,26 @@ func ValidateCall(sign string) (call, errorMsg string) {
//else we are dealing with a prefixed Callsign //else we are dealing with a prefixed Callsign
//validate the part that should contain the call (sp[1]) //validate the part that should contain the call (sp[1])
if !validCallRegexp.MatchString(sp[1]) { if !validCallRegexp.MatchString(sp[1]) {
return wrongSign, "Invalid call" return wrongSign, "[" + sp[1] + "] is an invalid call"
} }
//validate the prefix //validate the prefix
if !validPrefixRegexp.MatchString(sp[0]) { if !validPrefixRegexp.MatchString(sp[0]) {
return wrongSign, "Invalid prefix" return wrongSign, "[" + sp[0] + "] is an invalid prefix"
} }
return sign, "" return sign, ""
case 3: case 3:
//validate the part that should contain the call (sp[1]) //validate the part that should contain the call (sp[1])
if !validCallRegexp.MatchString(sp[1]) { if !validCallRegexp.MatchString(sp[1]) {
return wrongSign, "Invalid call" return wrongSign, "[" + sp[1] + "] is an invalid call"
} }
//validate the prefix //validate the prefix
if !validPrefixRegexp.MatchString(sp[0]) { if !validPrefixRegexp.MatchString(sp[0]) {
return wrongSign, "Invalid prefix" return wrongSign, "[" + sp[0] + "] is an invalid prefix"
} }
//We don't check the suffix //We don't check the suffix
return sign, "" return sign, ""
} }
return wrongSign, "Too many '/'" return wrongSign, "[" + sign + "] is invalid: too many '/'"
} }
// ValidateDate verifies whether the string is a valid date (YYYY-MM-DD). // ValidateDate verifies whether the string is a valid date (YYYY-MM-DD).

@ -32,22 +32,22 @@ func TestValidateWwff(t *testing.T) {
{ {
"Bad ref (no country prefix)", "Bad ref (no country prefix)",
args{inputStr: "ff-0258"}, args{inputStr: "ff-0258"},
"*FF-0258", "Invalid WWFF reference", "*FF-0258", "[FF-0258] is an invalid WWFF reference",
}, },
{ {
"Bad ref (wrong separator)", "Bad ref (wrong separator)",
args{inputStr: "gff/0258"}, args{inputStr: "gff/0258"},
"*GFF/0258", "Invalid WWFF reference", "*GFF/0258", "[GFF/0258] is an invalid WWFF reference",
}, },
{ {
"Bad ref (reference too short)", "Bad ref (reference too short)",
args{inputStr: "onff-258"}, args{inputStr: "onff-258"},
"*ONFF-258", "Invalid WWFF reference", "*ONFF-258", "[ONFF-258] is an invalid WWFF reference",
}, },
{ {
"Bad ref (no country prefix)", "Bad ref (no country prefix)",
args{inputStr: "onff-02589"}, args{inputStr: "onff-02589"},
"*ONFF-02589", "Invalid WWFF reference", "*ONFF-02589", "[ONFF-02589] is an invalid WWFF reference",
}, },
} }
for _, tt := range tests { for _, tt := range tests {
@ -96,37 +96,37 @@ func TestValidateSota(t *testing.T) {
{ {
"Bad ref (long prefix)", "Bad ref (long prefix)",
args{inputStr: "xxxx/ON-001"}, args{inputStr: "xxxx/ON-001"},
"*XXXX/ON-001", "Invalid SOTA reference", "*XXXX/ON-001", "[XXXX/ON-001] is an invalid SOTA reference",
}, },
{ {
"Bad ref (missing slash)", "Bad ref (missing slash)",
args{inputStr: "on ON-001"}, args{inputStr: "on ON-001"},
"*ON ON-001", "Invalid SOTA reference", "*ON ON-001", "[ON ON-001] is an invalid SOTA reference",
}, },
{ {
"Bad ref (numerical region)", "Bad ref (numerical region)",
args{inputStr: "on/9N-001"}, args{inputStr: "on/9N-001"},
"*ON/9N-001", "Invalid SOTA reference", "*ON/9N-001", "[ON/9N-001] is an invalid SOTA reference",
}, },
{ {
"Bad ref (too long region)", "Bad ref (too long region)",
args{inputStr: "on/ONA-001"}, args{inputStr: "on/ONA-001"},
"*ON/ONA-001", "Invalid SOTA reference", "*ON/ONA-001", "[ON/ONA-001] is an invalid SOTA reference",
}, },
{ {
"Bad ref (no dash)", "Bad ref (no dash)",
args{inputStr: "on/ON/001"}, args{inputStr: "on/ON/001"},
"*ON/ON/001", "Invalid SOTA reference", "*ON/ON/001", "[ON/ON/001] is an invalid SOTA reference",
}, },
{ {
"Bad ref (number too short)", "Bad ref (number too short)",
args{inputStr: "on/ON-01"}, args{inputStr: "on/ON-01"},
"*ON/ON-01", "Invalid SOTA reference", "*ON/ON-01", "[ON/ON-01] is an invalid SOTA reference",
}, },
{ {
"Bad ref (Number too long)", "Bad ref (Number too long)",
args{inputStr: "on/ON-9001"}, args{inputStr: "on/ON-9001"},
"*ON/ON-9001", "Invalid SOTA reference", "*ON/ON-9001", "[ON/ON-9001] is an invalid SOTA reference",
}, },
} }
for _, tt := range tests { for _, tt := range tests {
@ -196,32 +196,32 @@ func TestValidateCall(t *testing.T) {
{ {
"Pure junk passed", "Pure junk passed",
args{sign: "aaaaaa"}, args{sign: "aaaaaa"},
"*AAAAAA", "Invalid call", "*AAAAAA", "[AAAAAA] is an invalid call",
}, },
{ {
"empty string", "empty string",
args{sign: ""}, args{sign: ""},
"*", "Invalid call", "*", "[] is an invalid call",
}, },
{ {
"string with spaces", "string with spaces",
args{sign: " "}, args{sign: " "},
"*", "Invalid call", "*", "[] is an invalid call",
}, },
{ {
"invalid prefix", "invalid prefix",
args{sign: "xyz4/on4kjm"}, args{sign: "xyz4/on4kjm"},
"*XYZ4/ON4KJM", "Invalid prefix", "*XYZ4/ON4KJM", "[XYZ4] is an invalid prefix",
}, },
{ {
"Too many /", "Too many /",
args{sign: "F/on4kjm/p/x"}, args{sign: "F/on4kjm/p/x"},
"*F/ON4KJM/P/X", "Too many '/'", "*F/ON4KJM/P/X", "[F/ON4KJM/P/X] is invalid: too many '/'",
}, },
{ {
"signe /", "signe /",
args{sign: "/"}, args{sign: "/"},
"*/", "Invalid call", "*/", "[] is an invalid call",
}, },
} }
for _, tt := range tests { for _, tt := range tests {

Loading…
Cancel
Save