Pārlūkot izejas kodu

Fix linter errors

Mariano Cano 11 mēneši atpakaļ
vecāks
revīzija
091b6a15b3
4 mainītis faili ar 32 papildinājumiem un 1 dzēšanām
  1. 1 1
      acme/challenge.go
  2. 4 0
      acme/linker.go
  3. 13 0
      internal/cast/cast.go
  4. 14 0
      internal/cast/cast_test.go

+ 1 - 1
acme/challenge.go

@@ -231,7 +231,7 @@ func tlsAlert(err error) uint8 {
 	if errors.As(err, &opErr) {
 		v := reflect.ValueOf(opErr.Err)
 		if v.Kind() == reflect.Uint8 {
-			return uint8(v.Uint())
+			return cast.Uint8(v.Uint())
 		}
 	}
 	return 0

+ 4 - 0
acme/linker.go

@@ -84,12 +84,16 @@ func GetUnescapedPathSuffix(typ LinkType, provisionerName string, inputs ...stri
 	case NewNonceLinkType, NewAccountLinkType, NewOrderLinkType, NewAuthzLinkType, DirectoryLinkType, KeyChangeLinkType, RevokeCertLinkType:
 		return fmt.Sprintf("/%s/%s", provisionerName, typ)
 	case AccountLinkType, OrderLinkType, AuthzLinkType, CertificateLinkType:
+		//nolint:gosec // operating on internally defined inputs
 		return fmt.Sprintf("/%s/%s/%s", provisionerName, typ, inputs[0])
 	case ChallengeLinkType:
+		//nolint:gosec // operating on internally defined inputs
 		return fmt.Sprintf("/%s/%s/%s/%s", provisionerName, typ, inputs[0], inputs[1])
 	case OrdersByAccountLinkType:
+		//nolint:gosec // operating on internally defined inputs
 		return fmt.Sprintf("/%s/%s/%s/orders", provisionerName, AccountLinkType, inputs[0])
 	case FinalizeLinkType:
+		//nolint:gosec // operating on internally defined inputs
 		return fmt.Sprintf("/%s/%s/%s/finalize", provisionerName, OrderLinkType, inputs[0])
 	default:
 		return ""

+ 13 - 0
internal/cast/cast.go

@@ -93,3 +93,16 @@ func Uint16(x int) uint16 {
 
 	return u16
 }
+
+func SafeUint8[T number](x T) (uint8, error) {
+	return safecast.Convert[uint8](x)
+}
+
+func Uint8[T number](x T) uint8 {
+	u8, err := SafeUint8(x)
+	if err != nil {
+		panic(err)
+	}
+
+	return u8
+}

+ 14 - 0
internal/cast/cast_test.go

@@ -78,3 +78,17 @@ func TestUint16PanicsOnNegativeValue(t *testing.T) {
 func TestUint16PanicsOnLargeValue(t *testing.T) {
 	require.Panics(t, func() { Uint16(math.MaxUint16 + 1) })
 }
+
+func TestUint8ConvertsValues(t *testing.T) {
+	require.Equal(t, uint8(0), Uint8(0))
+	require.Equal(t, uint8(math.MaxUint8), Uint8(math.MaxUint8))
+	require.Equal(t, uint8(42), Uint8(42))
+}
+
+func TestUint8PanicsOnNegativeValue(t *testing.T) {
+	require.Panics(t, func() { Uint8(-1) })
+}
+
+func TestUint8PanicsOnLargeValue(t *testing.T) {
+	require.Panics(t, func() { Uint8(math.MaxUint8 + 1) })
+}