refactor(errorsx): improve errno generating code (#473)

No functional change, as it's clearly obvious from the output.

While there, also rename the generator for certifi. We are planning
on merging errorsx into netxlite. The first step is to give different
names to the code generating programs.

See https://github.com/ooni/probe/issues/1591
This commit is contained in:
Simone Basso 2021-09-07 14:25:42 +02:00 committed by GitHub
parent fe3c90479d
commit 8b38ea7e98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 90 additions and 105 deletions

View File

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// Generated: 2021-09-05 13:54:14.649711 +0200 CEST m=+0.136980959
// Generated: 2021-09-07 13:05:58.447943 +0200 CEST m=+0.137110292
package errorsx

View File

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// Generated: 2021-09-05 13:54:14.695896 +0200 CEST m=+0.183167084
// Generated: 2021-09-07 13:05:58.496075 +0200 CEST m=+0.185240417
package errorsx

View File

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// Generated: 2021-09-05 13:54:14.514032 +0200 CEST m=+0.001299626
// Generated: 2021-09-07 13:05:58.31181 +0200 CEST m=+0.000981501
package errorsx

View File

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT.
// Generated: 2021-09-05 13:54:14.622035 +0200 CEST m=+0.109304917
// Generated: 2021-09-07 13:05:58.421578 +0200 CEST m=+0.110745959
package errorsx

View File

@ -12,99 +12,82 @@ import (
// ErrorSpec specifies the error we care about.
type ErrorSpec struct {
// Errno is the error name as an errno value (e.g., ECONNREFUSED).
Errno string
// errno is the error name as an errno value (e.g., ECONNREFUSED).
errno string
// Failure is the error name according to OONI (e.g., FailureConnectionRefused).
Failure string
// failure is the error name according to OONI (e.g., FailureConnectionRefused).
failure string
}
// AsErrnoName returns the name of the corresponding errno, if this
// is a system error, or panics otherwise.
func (es *ErrorSpec) AsErrnoName() string {
if !es.IsSystemError() {
panic("not a system error")
}
return es.errno
}
// AsFailureVar returns the name of the failure var.
func (es *ErrorSpec) AsFailureVar() string {
return "Failure" + strcase.ToCamel(es.failure)
}
// AsFailureString returns the OONI failure string.
func (es *ErrorSpec) AsFailureString() string {
return es.failure
}
// NewSystemError constructs a new ErrorSpec representing a system
// error, i.e., an error returned by a system call.
func NewSystemError(errno, failure string) *ErrorSpec {
return &ErrorSpec{errno: errno, failure: failure}
}
// NewLibraryError constructs a new ErrorSpec representing a library
// error, i.e., an error returned by the Go standard library or by other
// dependecies written typicall in Go (e.g., quic-go).
func NewLibraryError(failure string) *ErrorSpec {
return &ErrorSpec{failure: failure}
}
// IsSystemError returns whether this ErrorSpec describes a system
// error, i.e., an error returned by a syscall.
func (es *ErrorSpec) IsSystemError() bool {
return es.errno != ""
}
// Specs contains all the error specs.
var Specs = []ErrorSpec{{
Errno: "ECANCELED",
Failure: "OperationCanceled",
}, {
Errno: "ECONNREFUSED",
Failure: "ConnectionRefused",
}, {
Errno: "ECONNRESET",
Failure: "ConnectionReset",
}, {
Errno: "EHOSTUNREACH",
Failure: "HostUnreachable",
}, {
Errno: "ETIMEDOUT",
Failure: "TimedOut",
}, {
Errno: "EAFNOSUPPORT",
Failure: "AddressFamilyNotSupported",
}, {
Errno: "EADDRINUSE",
Failure: "AddressInUse",
}, {
Errno: "EADDRNOTAVAIL",
Failure: "AddressNotAvailable",
}, {
Errno: "EISCONN",
Failure: "AlreadyConnected",
}, {
Errno: "EFAULT",
Failure: "BadAddress",
}, {
Errno: "EBADF",
Failure: "BadFileDescriptor",
}, {
Errno: "ECONNABORTED",
Failure: "ConnectionAborted",
}, {
Errno: "EALREADY",
Failure: "ConnectionAlreadyInProgress",
}, {
Errno: "EDESTADDRREQ",
Failure: "DestinationAddressRequired",
}, {
Errno: "EINTR",
Failure: "Interrupted",
}, {
Errno: "EINVAL",
Failure: "InvalidArgument",
}, {
Errno: "EMSGSIZE",
Failure: "MessageSize",
}, {
Errno: "ENETDOWN",
Failure: "NetworkDown",
}, {
Errno: "ENETRESET",
Failure: "NetworkReset",
}, {
Errno: "ENETUNREACH",
Failure: "NetworkUnreachable",
}, {
Errno: "ENOBUFS",
Failure: "NoBufferSpace",
}, {
Errno: "ENOPROTOOPT",
Failure: "NoProtocolOption",
}, {
Errno: "ENOTSOCK",
Failure: "NotASocket",
}, {
Errno: "ENOTCONN",
Failure: "NotConnected",
}, {
Errno: "EWOULDBLOCK",
Failure: "OperationWouldBlock",
}, {
Errno: "EACCES",
Failure: "PermissionDenied",
}, {
Errno: "EPROTONOSUPPORT",
Failure: "ProtocolNotSupported",
}, {
Errno: "EPROTOTYPE",
Failure: "WrongProtocolType",
}}
var Specs = []*ErrorSpec{
NewSystemError("ECANCELED", "operation_canceled"),
NewSystemError("ECONNREFUSED", "connection_refused"),
NewSystemError("ECONNRESET", "connection_reset"),
NewSystemError("EHOSTUNREACH", "host_unreachable"),
NewSystemError("ETIMEDOUT", "timed_out"),
NewSystemError("EAFNOSUPPORT", "address_family_not_supported"),
NewSystemError("EADDRINUSE", "address_in_use"),
NewSystemError("EADDRNOTAVAIL", "address_not_available"),
NewSystemError("EISCONN", "already_connected"),
NewSystemError("EFAULT", "bad_address"),
NewSystemError("EBADF", "bad_file_descriptor"),
NewSystemError("ECONNABORTED", "connection_aborted"),
NewSystemError("EALREADY", "connection_already_in_progress"),
NewSystemError("EDESTADDRREQ", "destination_address_required"),
NewSystemError("EINTR", "interrupted"),
NewSystemError("EINVAL", "invalid_argument"),
NewSystemError("EMSGSIZE", "message_size"),
NewSystemError("ENETDOWN", "network_down"),
NewSystemError("ENETRESET", "network_reset"),
NewSystemError("ENETUNREACH", "network_unreachable"),
NewSystemError("ENOBUFS", "no_buffer_space"),
NewSystemError("ENOPROTOOPT", "no_protocol_option"),
NewSystemError("ENOTSOCK", "not_a_socket"),
NewSystemError("ENOTCONN", "not_connected"),
NewSystemError("EWOULDBLOCK", "operation_would_block"),
NewSystemError("EACCES", "permission_denied"),
NewSystemError("EPROTONOSUPPORT", "protocol_not_supported"),
NewSystemError("EPROTOTYPE", "wrong_protocol_type"),
}
func fileCreate(filename string) *os.File {
filep, err := os.Create(filename)
@ -146,7 +129,8 @@ func writeSystemSpecificFile(kind string) {
filePrintf(filep, "import \"golang.org/x/sys/%s\"\n\n", kind)
fileWrite(filep, "const (\n")
for _, spec := range Specs {
filePrintf(filep, "\t%s = %s.%s\n", spec.Errno, kind, spec.Errno)
filePrintf(filep, "\t%s = %s.%s\n",
spec.AsErrnoName(), kind, spec.AsErrnoName())
}
fileWrite(filep, ")\n\n")
fileClose(filep)
@ -172,8 +156,9 @@ func writeGenericFile() {
fileWrite(filep, "// DO NOT derive from system call errors.\n")
fileWrite(filep, "const (\n")
for _, spec := range Specs {
filePrintf(filep, "\tFailure%s = \"%s\"\n", spec.Failure,
strcase.ToSnake(spec.Failure))
filePrintf(filep, "\t%s = \"%s\"\n",
spec.AsFailureVar(),
spec.AsFailureString())
}
fileWrite(filep, ")\n\n")
@ -190,10 +175,8 @@ func writeGenericFile() {
fileWrite(filep, "\t}\n")
fileWrite(filep, "\tswitch errno {\n")
for _, spec := range Specs {
if spec.Failure != "" {
filePrintf(filep, "\tcase %s:\n", spec.Errno)
filePrintf(filep, "\t\treturn Failure%s\n", spec.Failure)
}
filePrintf(filep, "\tcase %s:\n", spec.AsErrnoName())
filePrintf(filep, "\t\treturn %s\n", spec.AsFailureVar())
}
fileWrite(filep, "\t}\n")
fileWrite(filep, "\treturn \"\"\n")
@ -222,8 +205,10 @@ func writeGenericTestFile() {
fileWrite(filep, "\t}\n")
for _, spec := range Specs {
filePrintf(filep, "\tif v := toSyscallErr(%s); v != Failure%s {\n", spec.Errno, spec.Failure)
filePrintf(filep, "\t\tt.Fatalf(\"expected '%%s', got '%%s'\", Failure%s, v)\n", spec.Failure)
filePrintf(filep, "\tif v := toSyscallErr(%s); v != %s {\n",
spec.AsErrnoName(), spec.AsFailureVar())
filePrintf(filep, "\t\tt.Fatalf(\"expected '%%s', got '%%s'\", %s, v)\n",
spec.AsFailureVar())
fileWrite(filep, "\t}\n")
}

View File

@ -1,10 +1,10 @@
// Code generated by go generate; DO NOT EDIT.
// 2021-09-05 14:01:12.627844 +0200 CEST m=+0.441951210
// 2021-09-07 12:37:42.667616 +0200 CEST m=+1.355122543
// https://curl.haxx.se/ca/cacert.pem
package netxlite
//go:generate go run ./internal/generator/ "https://curl.haxx.se/ca/cacert.pem"
//go:generate go run ./internal/gencertifi/ "https://curl.haxx.se/ca/cacert.pem"
const pemcerts string = `
##

View File

@ -28,7 +28,7 @@ var tmpl = template.Must(template.New("").Parse(`// Code generated by go generat
package netxlite
//{{ .GoGenerate }} go run ./internal/generator/ "{{ .URL }}"
//{{ .GoGenerate }} go run ./internal/gencertifi/ "{{ .URL }}"
const pemcerts string = ` + "`" + `
{{ .Bundle }}