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:
parent
fe3c90479d
commit
8b38ea7e98
|
@ -1,5 +1,5 @@
|
||||||
// Code generated by go generate; DO NOT EDIT.
|
// 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
|
package errorsx
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
// Code generated by go generate; DO NOT EDIT.
|
// 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
|
package errorsx
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
// Code generated by go generate; DO NOT EDIT.
|
// 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
|
package errorsx
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
// Code generated by go generate; DO NOT EDIT.
|
// 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
|
package errorsx
|
||||||
|
|
||||||
|
|
|
@ -12,99 +12,82 @@ import (
|
||||||
|
|
||||||
// ErrorSpec specifies the error we care about.
|
// ErrorSpec specifies the error we care about.
|
||||||
type ErrorSpec struct {
|
type ErrorSpec struct {
|
||||||
// Errno is the error name as an errno value (e.g., ECONNREFUSED).
|
// errno is the error name as an errno value (e.g., ECONNREFUSED).
|
||||||
Errno string
|
errno string
|
||||||
|
|
||||||
// Failure is the error name according to OONI (e.g., FailureConnectionRefused).
|
// failure is the error name according to OONI (e.g., FailureConnectionRefused).
|
||||||
Failure string
|
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.
|
// Specs contains all the error specs.
|
||||||
var Specs = []ErrorSpec{{
|
var Specs = []*ErrorSpec{
|
||||||
Errno: "ECANCELED",
|
NewSystemError("ECANCELED", "operation_canceled"),
|
||||||
Failure: "OperationCanceled",
|
NewSystemError("ECONNREFUSED", "connection_refused"),
|
||||||
}, {
|
NewSystemError("ECONNRESET", "connection_reset"),
|
||||||
Errno: "ECONNREFUSED",
|
NewSystemError("EHOSTUNREACH", "host_unreachable"),
|
||||||
Failure: "ConnectionRefused",
|
NewSystemError("ETIMEDOUT", "timed_out"),
|
||||||
}, {
|
NewSystemError("EAFNOSUPPORT", "address_family_not_supported"),
|
||||||
Errno: "ECONNRESET",
|
NewSystemError("EADDRINUSE", "address_in_use"),
|
||||||
Failure: "ConnectionReset",
|
NewSystemError("EADDRNOTAVAIL", "address_not_available"),
|
||||||
}, {
|
NewSystemError("EISCONN", "already_connected"),
|
||||||
Errno: "EHOSTUNREACH",
|
NewSystemError("EFAULT", "bad_address"),
|
||||||
Failure: "HostUnreachable",
|
NewSystemError("EBADF", "bad_file_descriptor"),
|
||||||
}, {
|
NewSystemError("ECONNABORTED", "connection_aborted"),
|
||||||
Errno: "ETIMEDOUT",
|
NewSystemError("EALREADY", "connection_already_in_progress"),
|
||||||
Failure: "TimedOut",
|
NewSystemError("EDESTADDRREQ", "destination_address_required"),
|
||||||
}, {
|
NewSystemError("EINTR", "interrupted"),
|
||||||
Errno: "EAFNOSUPPORT",
|
NewSystemError("EINVAL", "invalid_argument"),
|
||||||
Failure: "AddressFamilyNotSupported",
|
NewSystemError("EMSGSIZE", "message_size"),
|
||||||
}, {
|
NewSystemError("ENETDOWN", "network_down"),
|
||||||
Errno: "EADDRINUSE",
|
NewSystemError("ENETRESET", "network_reset"),
|
||||||
Failure: "AddressInUse",
|
NewSystemError("ENETUNREACH", "network_unreachable"),
|
||||||
}, {
|
NewSystemError("ENOBUFS", "no_buffer_space"),
|
||||||
Errno: "EADDRNOTAVAIL",
|
NewSystemError("ENOPROTOOPT", "no_protocol_option"),
|
||||||
Failure: "AddressNotAvailable",
|
NewSystemError("ENOTSOCK", "not_a_socket"),
|
||||||
}, {
|
NewSystemError("ENOTCONN", "not_connected"),
|
||||||
Errno: "EISCONN",
|
NewSystemError("EWOULDBLOCK", "operation_would_block"),
|
||||||
Failure: "AlreadyConnected",
|
NewSystemError("EACCES", "permission_denied"),
|
||||||
}, {
|
NewSystemError("EPROTONOSUPPORT", "protocol_not_supported"),
|
||||||
Errno: "EFAULT",
|
NewSystemError("EPROTOTYPE", "wrong_protocol_type"),
|
||||||
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",
|
|
||||||
}}
|
|
||||||
|
|
||||||
func fileCreate(filename string) *os.File {
|
func fileCreate(filename string) *os.File {
|
||||||
filep, err := os.Create(filename)
|
filep, err := os.Create(filename)
|
||||||
|
@ -146,7 +129,8 @@ func writeSystemSpecificFile(kind string) {
|
||||||
filePrintf(filep, "import \"golang.org/x/sys/%s\"\n\n", kind)
|
filePrintf(filep, "import \"golang.org/x/sys/%s\"\n\n", kind)
|
||||||
fileWrite(filep, "const (\n")
|
fileWrite(filep, "const (\n")
|
||||||
for _, spec := range Specs {
|
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")
|
fileWrite(filep, ")\n\n")
|
||||||
fileClose(filep)
|
fileClose(filep)
|
||||||
|
@ -172,8 +156,9 @@ func writeGenericFile() {
|
||||||
fileWrite(filep, "// DO NOT derive from system call errors.\n")
|
fileWrite(filep, "// DO NOT derive from system call errors.\n")
|
||||||
fileWrite(filep, "const (\n")
|
fileWrite(filep, "const (\n")
|
||||||
for _, spec := range Specs {
|
for _, spec := range Specs {
|
||||||
filePrintf(filep, "\tFailure%s = \"%s\"\n", spec.Failure,
|
filePrintf(filep, "\t%s = \"%s\"\n",
|
||||||
strcase.ToSnake(spec.Failure))
|
spec.AsFailureVar(),
|
||||||
|
spec.AsFailureString())
|
||||||
}
|
}
|
||||||
fileWrite(filep, ")\n\n")
|
fileWrite(filep, ")\n\n")
|
||||||
|
|
||||||
|
@ -190,10 +175,8 @@ func writeGenericFile() {
|
||||||
fileWrite(filep, "\t}\n")
|
fileWrite(filep, "\t}\n")
|
||||||
fileWrite(filep, "\tswitch errno {\n")
|
fileWrite(filep, "\tswitch errno {\n")
|
||||||
for _, spec := range Specs {
|
for _, spec := range Specs {
|
||||||
if spec.Failure != "" {
|
filePrintf(filep, "\tcase %s:\n", spec.AsErrnoName())
|
||||||
filePrintf(filep, "\tcase %s:\n", spec.Errno)
|
filePrintf(filep, "\t\treturn %s\n", spec.AsFailureVar())
|
||||||
filePrintf(filep, "\t\treturn Failure%s\n", spec.Failure)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
fileWrite(filep, "\t}\n")
|
fileWrite(filep, "\t}\n")
|
||||||
fileWrite(filep, "\treturn \"\"\n")
|
fileWrite(filep, "\treturn \"\"\n")
|
||||||
|
@ -222,8 +205,10 @@ func writeGenericTestFile() {
|
||||||
fileWrite(filep, "\t}\n")
|
fileWrite(filep, "\t}\n")
|
||||||
|
|
||||||
for _, spec := range Specs {
|
for _, spec := range Specs {
|
||||||
filePrintf(filep, "\tif v := toSyscallErr(%s); v != Failure%s {\n", spec.Errno, spec.Failure)
|
filePrintf(filep, "\tif v := toSyscallErr(%s); v != %s {\n",
|
||||||
filePrintf(filep, "\t\tt.Fatalf(\"expected '%%s', got '%%s'\", Failure%s, v)\n", spec.Failure)
|
spec.AsErrnoName(), spec.AsFailureVar())
|
||||||
|
filePrintf(filep, "\t\tt.Fatalf(\"expected '%%s', got '%%s'\", %s, v)\n",
|
||||||
|
spec.AsFailureVar())
|
||||||
fileWrite(filep, "\t}\n")
|
fileWrite(filep, "\t}\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
// Code generated by go generate; DO NOT EDIT.
|
// 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
|
// https://curl.haxx.se/ca/cacert.pem
|
||||||
|
|
||||||
package netxlite
|
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 = `
|
const pemcerts string = `
|
||||||
##
|
##
|
||||||
|
|
|
@ -28,7 +28,7 @@ var tmpl = template.Must(template.New("").Parse(`// Code generated by go generat
|
||||||
|
|
||||||
package netxlite
|
package netxlite
|
||||||
|
|
||||||
//{{ .GoGenerate }} go run ./internal/generator/ "{{ .URL }}"
|
//{{ .GoGenerate }} go run ./internal/gencertifi/ "{{ .URL }}"
|
||||||
|
|
||||||
const pemcerts string = ` + "`" + `
|
const pemcerts string = ` + "`" + `
|
||||||
{{ .Bundle }}
|
{{ .Bundle }}
|
Loading…
Reference in New Issue
Block a user