Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swap usage of the large switch statement. #9

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rpsl-parser/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package rpsl
type KeyWord int

const (
colon = rune(':')
eof = rune(0)
octothorpe = rune('#')
colon = rune(':')
)
180 changes: 12 additions & 168 deletions rpsl-parser/coverage.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@
<div id="nav">
<select id="files">

<option value="file0">./irr.go (92.6%)</option>
<option value="file0">/home/morrowc/scripts/git/contrib/rpsl-parser/irr.go (86.2%)</option>

<option value="file1">./util.go (85.0%)</option>
<option value="file1">/home/morrowc/scripts/git/contrib/rpsl-parser/util.go (85.0%)</option>

</select>
</div>
Expand Down Expand Up @@ -139,173 +139,17 @@
return rppb.Type_UNKNOWN, buf.String()
}</span>

<span class="cov8" title="1">switch strings.ToUpper(buf.String()) </span>{
case "ROUTE":<span class="cov8" title="1">
return rppb.Type_ROUTE, buf.String()</span>
case "ROUTE6":<span class="cov8" title="1">
return rppb.Type_ROUTE6, buf.String()</span>
case "AUT-NUM":<span class="cov8" title="1">
return rppb.Type_AUTNUM, buf.String()</span>
case "AS-SET":<span class="cov8" title="1">
return rppb.Type_ASSET, buf.String()</span>
case "ROUTE-SET":<span class="cov8" title="1">
return rppb.Type_ROUTESET, buf.String()</span>
case "MNTNER":<span class="cov8" title="1">
return rppb.Type_MNTNER, buf.String()</span>
case "PERSON":<span class="cov8" title="1">
return rppb.Type_PERSON, buf.String()</span>
case "INETNUM":<span class="cov8" title="1">
return rppb.Type_INETNUM, buf.String()</span>
case "KEY-CERT":<span class="cov8" title="1">
return rppb.Type_KEYCERT, buf.String()</span>
case "ROLE":<span class="cov8" title="1">
return rppb.Type_ROLE, buf.String()</span>
case "INET-RTR":<span class="cov8" title="1">
return rppb.Type_INETRTR, buf.String()</span>
case "INET6NUM":<span class="cov8" title="1">
return rppb.Type_INET6NUM, buf.String()</span>
case "FILTER-SET":<span class="cov8" title="1">
return rppb.Type_FILTERSET, buf.String()</span>
case "RTR-SET":<span class="cov8" title="1">
return rppb.Type_RTRSET, buf.String()</span>
case "PEERING-SET":<span class="cov8" title="1">
return rppb.Type_PEERINGSET, buf.String()</span>
case "ADDRESS":<span class="cov8" title="1">
return rppb.Type_ADDRESS, buf.String()</span>
case "ADMIN-C":<span class="cov8" title="1">
return rppb.Type_ADMINC, buf.String()</span>
case "AGGR-BNDRY":<span class="cov8" title="1">
return rppb.Type_AGGRBNDRY, buf.String()</span>
case "AGGR-MTD":<span class="cov8" title="1">
return rppb.Type_AGGRMTD, buf.String()</span>
case "ALIAS":<span class="cov8" title="1">
return rppb.Type_ALIAS, buf.String()</span>
case "AS-NAME":<span class="cov8" title="1">
return rppb.Type_ASNAME, buf.String()</span>
case "AUTH":<span class="cov8" title="1">
return rppb.Type_AUTH, buf.String()</span>
case "CERTIF":<span class="cov8" title="1">
return rppb.Type_CERTIF, buf.String()</span>
case "CHANGED":<span class="cov8" title="1">
return rppb.Type_CHANGED, buf.String()</span>
case "COMPONENTS":<span class="cov8" title="1">
return rppb.Type_COMPONENTS, buf.String()</span>
case "COUNTRY":<span class="cov8" title="1">
return rppb.Type_COUNTRY, buf.String()</span>
case "DEFAULT":<span class="cov8" title="1">
return rppb.Type_DEFAULT, buf.String()</span>
case "DESCR":<span class="cov8" title="1">
return rppb.Type_DESCR, buf.String()</span>
case "E-MAIL":<span class="cov8" title="1">
return rppb.Type_EMAIL, buf.String()</span>
case "EXPORT":<span class="cov8" title="1">
return rppb.Type_EXPORT, buf.String()</span>
case "EXPORT-COMPS":<span class="cov8" title="1">
return rppb.Type_EXPORTCOMPS, buf.String()</span>
case "EXPORT-VIA":<span class="cov8" title="1">
return rppb.Type_EXPORTVIA, buf.String()</span>
case "FAX-NO":<span class="cov8" title="1">
return rppb.Type_FAXNO, buf.String()</span>
case "FILTER":<span class="cov8" title="1">
return rppb.Type_FILTER, buf.String()</span>
case "FINGERPR":<span class="cov8" title="1">
return rppb.Type_FINGERPR, buf.String()</span>
case "GEOIDX":<span class="cov8" title="1">
return rppb.Type_GEOIDX, buf.String()</span>
case "HOLES":<span class="cov8" title="1">
return rppb.Type_HOLES, buf.String()</span>
case "IFADDR":<span class="cov8" title="1">
return rppb.Type_IFADDR, buf.String()</span>
case "IMPORT":<span class="cov8" title="1">
return rppb.Type_IMPORT, buf.String()</span>
case "IMPORT-VIA":<span class="cov8" title="1">
return rppb.Type_IMPORTVIA, buf.String()</span>
case "INTERFACE":<span class="cov8" title="1">
return rppb.Type_INTERFACE, buf.String()</span>
case "LOCAL-AS":<span class="cov8" title="1">
return rppb.Type_LOCALAS, buf.String()</span>
case "MBRS-BY-REF":<span class="cov8" title="1">
return rppb.Type_MBRSBYREF, buf.String()</span>
case "MEMBER-OF":<span class="cov8" title="1">
return rppb.Type_MEMBEROF, buf.String()</span>
case "MEMBERS":<span class="cov8" title="1">
return rppb.Type_MEMBERS, buf.String()</span>
case "METHOD":<span class="cov8" title="1">
return rppb.Type_METHOD, buf.String()</span>
case "MNT-BY":<span class="cov8" title="1">
return rppb.Type_MNTBY, buf.String()</span>
case "MNT-NFY":<span class="cov8" title="1">
return rppb.Type_MNTNFY, buf.String()</span>
case "MP-EXPORT":<span class="cov8" title="1">
return rppb.Type_MPEXPORT, buf.String()</span>
case "MP-FILTER":<span class="cov8" title="1">
return rppb.Type_MPFILTER, buf.String()</span>
case "MP-IMPORT":<span class="cov8" title="1">
return rppb.Type_MPIMPORT, buf.String()</span>
case "MP-MEMBERS":<span class="cov8" title="1">
return rppb.Type_MPMEMBERS, buf.String()</span>
case "MP-PEER":<span class="cov8" title="1">
return rppb.Type_MPPEER, buf.String()</span>
case "MP-PEERING":<span class="cov8" title="1">
return rppb.Type_MPPEERING, buf.String()</span>
case "NETNAME":<span class="cov8" title="1">
return rppb.Type_NETNAME, buf.String()</span>
case "NIC-HDL":<span class="cov8" title="1">
return rppb.Type_NICHDL, buf.String()</span>
case "NOTIFY":<span class="cov8" title="1">
return rppb.Type_NOTIFY, buf.String()</span>
case "ORIGIN":<span class="cov8" title="1">
return rppb.Type_ORIGIN, buf.String()</span>
case "OWNER":<span class="cov8" title="1">
return rppb.Type_OWNER, buf.String()</span>
case "PEER":<span class="cov8" title="1">
return rppb.Type_PEER, buf.String()</span>
case "PEERING":<span class="cov8" title="1">
return rppb.Type_PEERING, buf.String()</span>
case "PHONE":<span class="cov8" title="1">
return rppb.Type_PHONE, buf.String()</span>
case "REMARKS":<span class="cov8" title="1">
return rppb.Type_REMARKS, buf.String()</span>
case "ROA-URI":<span class="cov8" title="1">
return rppb.Type_ROAURI, buf.String()</span>
case "RS-IN":<span class="cov8" title="1">
return rppb.Type_RSIN, buf.String()</span>
case "RS-OUT":<span class="cov8" title="1">
return rppb.Type_RSOUT, buf.String()</span>
case "SOURCE":<span class="cov8" title="1">
return rppb.Type_SOURCE, buf.String()</span>
case "STATUS":<span class="cov8" title="1">
return rppb.Type_STATUS, buf.String()</span>
case "TECH-C":<span class="cov8" title="1">
return rppb.Type_TECHC, buf.String()</span>
case "TROUBLE":<span class="cov8" title="1">
return rppb.Type_TROUBLE, buf.String()</span>
case "UPD-TO":<span class="cov8" title="1">
return rppb.Type_UPDTO, buf.String()</span>
case "*XXE":<span class="cov8" title="1">
return rppb.Type_XXE, buf.String()</span>
case "*XXNER":<span class="cov8" title="1">
return rppb.Type_XXNER, buf.String()</span>
case "*XX-NUM":<span class="cov8" title="1">
return rppb.Type_XXNUM, buf.String()</span>
case "*XXRING-SET":<span class="cov8" title="1">
return rppb.Type_XXRINGSET, buf.String()</span>
case "*XXSET":<span class="cov8" title="1">
return rppb.Type_XXSET, buf.String()</span>
case "*XXSON":<span class="cov8" title="1">
return rppb.Type_XXSON, buf.String()</span>
case "*XXTE":<span class="cov8" title="1">
return rppb.Type_XXTE, buf.String()</span>
case "*XXTE6":<span class="cov8" title="1">
return rppb.Type_XXTE6, buf.String()</span>
case "*XXTE-SET":<span class="cov8" title="1">
return rppb.Type_XXTESET, buf.String()</span>
default:<span class="cov8" title="1">
// TODO(morrowc): Log this output instead of printing it to the console.
// fmt.Printf("failed to match whatever is in buf currently: %v\n", buf.String())
return rppb.Type_UNKNOWN, buf.String()</span>
}
<span class="cov8" title="1">key := strings.ToUpper(buf.String())
ret := buf.String()
// Some RPSL keywords have '-' or '*' in them, the proto enum does not have
// these characters included, remove them from the key comparison.
key = strings.Replace(key, "-", "", -1)
key = strings.Replace(key, "*", "", -1)

if _, ok := rppb.Type_value[key]; !ok </span><span class="cov8" title="1">{
return rppb.Type_UNKNOWN, ret
}</span>
<span class="cov8" title="1">return rppb.Type(rppb.Type_value[key]), ret</span>
}
// Each loop through accumulates more in the buffer.
<span class="cov8" title="1">_, _ = buf.WriteRune(ch)</span>
Expand Down
176 changes: 10 additions & 166 deletions rpsl-parser/irr.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,173 +80,17 @@ func (r *Reader) findKey() (rppb.Type, string) {
return rppb.Type_UNKNOWN, buf.String()
}

switch strings.ToUpper(buf.String()) {
case "ROUTE":
return rppb.Type_ROUTE, buf.String()
case "ROUTE6":
return rppb.Type_ROUTE6, buf.String()
case "AUT-NUM":
return rppb.Type_AUTNUM, buf.String()
case "AS-SET":
return rppb.Type_ASSET, buf.String()
case "ROUTE-SET":
return rppb.Type_ROUTESET, buf.String()
case "MNTNER":
return rppb.Type_MNTNER, buf.String()
case "PERSON":
return rppb.Type_PERSON, buf.String()
case "INETNUM":
return rppb.Type_INETNUM, buf.String()
case "KEY-CERT":
return rppb.Type_KEYCERT, buf.String()
case "ROLE":
return rppb.Type_ROLE, buf.String()
case "INET-RTR":
return rppb.Type_INETRTR, buf.String()
case "INET6NUM":
return rppb.Type_INET6NUM, buf.String()
case "FILTER-SET":
return rppb.Type_FILTERSET, buf.String()
case "RTR-SET":
return rppb.Type_RTRSET, buf.String()
case "PEERING-SET":
return rppb.Type_PEERINGSET, buf.String()
case "ADDRESS":
return rppb.Type_ADDRESS, buf.String()
case "ADMIN-C":
return rppb.Type_ADMINC, buf.String()
case "AGGR-BNDRY":
return rppb.Type_AGGRBNDRY, buf.String()
case "AGGR-MTD":
return rppb.Type_AGGRMTD, buf.String()
case "ALIAS":
return rppb.Type_ALIAS, buf.String()
case "AS-NAME":
return rppb.Type_ASNAME, buf.String()
case "AUTH":
return rppb.Type_AUTH, buf.String()
case "CERTIF":
return rppb.Type_CERTIF, buf.String()
case "CHANGED":
return rppb.Type_CHANGED, buf.String()
case "COMPONENTS":
return rppb.Type_COMPONENTS, buf.String()
case "COUNTRY":
return rppb.Type_COUNTRY, buf.String()
case "DEFAULT":
return rppb.Type_DEFAULT, buf.String()
case "DESCR":
return rppb.Type_DESCR, buf.String()
case "E-MAIL":
return rppb.Type_EMAIL, buf.String()
case "EXPORT":
return rppb.Type_EXPORT, buf.String()
case "EXPORT-COMPS":
return rppb.Type_EXPORTCOMPS, buf.String()
case "EXPORT-VIA":
return rppb.Type_EXPORTVIA, buf.String()
case "FAX-NO":
return rppb.Type_FAXNO, buf.String()
case "FILTER":
return rppb.Type_FILTER, buf.String()
case "FINGERPR":
return rppb.Type_FINGERPR, buf.String()
case "GEOIDX":
return rppb.Type_GEOIDX, buf.String()
case "HOLES":
return rppb.Type_HOLES, buf.String()
case "IFADDR":
return rppb.Type_IFADDR, buf.String()
case "IMPORT":
return rppb.Type_IMPORT, buf.String()
case "IMPORT-VIA":
return rppb.Type_IMPORTVIA, buf.String()
case "INTERFACE":
return rppb.Type_INTERFACE, buf.String()
case "LOCAL-AS":
return rppb.Type_LOCALAS, buf.String()
case "MBRS-BY-REF":
return rppb.Type_MBRSBYREF, buf.String()
case "MEMBER-OF":
return rppb.Type_MEMBEROF, buf.String()
case "MEMBERS":
return rppb.Type_MEMBERS, buf.String()
case "METHOD":
return rppb.Type_METHOD, buf.String()
case "MNT-BY":
return rppb.Type_MNTBY, buf.String()
case "MNT-NFY":
return rppb.Type_MNTNFY, buf.String()
case "MP-EXPORT":
return rppb.Type_MPEXPORT, buf.String()
case "MP-FILTER":
return rppb.Type_MPFILTER, buf.String()
case "MP-IMPORT":
return rppb.Type_MPIMPORT, buf.String()
case "MP-MEMBERS":
return rppb.Type_MPMEMBERS, buf.String()
case "MP-PEER":
return rppb.Type_MPPEER, buf.String()
case "MP-PEERING":
return rppb.Type_MPPEERING, buf.String()
case "NETNAME":
return rppb.Type_NETNAME, buf.String()
case "NIC-HDL":
return rppb.Type_NICHDL, buf.String()
case "NOTIFY":
return rppb.Type_NOTIFY, buf.String()
case "ORIGIN":
return rppb.Type_ORIGIN, buf.String()
case "OWNER":
return rppb.Type_OWNER, buf.String()
case "PEER":
return rppb.Type_PEER, buf.String()
case "PEERING":
return rppb.Type_PEERING, buf.String()
case "PHONE":
return rppb.Type_PHONE, buf.String()
case "REMARKS":
return rppb.Type_REMARKS, buf.String()
case "ROA-URI":
return rppb.Type_ROAURI, buf.String()
case "RS-IN":
return rppb.Type_RSIN, buf.String()
case "RS-OUT":
return rppb.Type_RSOUT, buf.String()
case "SOURCE":
return rppb.Type_SOURCE, buf.String()
case "STATUS":
return rppb.Type_STATUS, buf.String()
case "TECH-C":
return rppb.Type_TECHC, buf.String()
case "TROUBLE":
return rppb.Type_TROUBLE, buf.String()
case "UPD-TO":
return rppb.Type_UPDTO, buf.String()
case "*XXE":
return rppb.Type_XXE, buf.String()
case "*XXNER":
return rppb.Type_XXNER, buf.String()
case "*XX-NUM":
return rppb.Type_XXNUM, buf.String()
case "*XXRING-SET":
return rppb.Type_XXRINGSET, buf.String()
case "*XXSET":
return rppb.Type_XXSET, buf.String()
case "*XXSON":
return rppb.Type_XXSON, buf.String()
case "*XXTE":
return rppb.Type_XXTE, buf.String()
case "*XXTE6":
return rppb.Type_XXTE6, buf.String()
case "*XXTE-SET":
return rppb.Type_XXTESET, buf.String()
default:
// TODO(morrowc): Log this output instead of printing it to the console.
// fmt.Printf("failed to match whatever is in buf currently: %v\n", buf.String())
return rppb.Type_UNKNOWN, buf.String()
key := strings.ToUpper(buf.String())
ret := buf.String()
// Some RPSL keywords have '-' or '*' in them, the proto enum does not have
// these characters included, remove them from the key comparison.
key = strings.Replace(key, "-", "", -1)
key = strings.Replace(key, "*", "", -1)

if _, ok := rppb.Type_value[key]; !ok {
return rppb.Type_UNKNOWN, ret
}

return rppb.Type(rppb.Type_value[key]), ret
}
// Each loop through accumulates more in the buffer.
_, _ = buf.WriteRune(ch)
Expand Down