Skip to content

Commit

Permalink
Move builders to package ...encoding.base* (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
05nelsonm authored Jun 21, 2023
1 parent 74ebb02 commit 640bd82
Show file tree
Hide file tree
Showing 22 changed files with 1,351 additions and 131 deletions.
29 changes: 29 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# MIGRATION

## Preamble

`2.0.0` release contained a breaking API change which broke binary compatibility.
This was attributed to an issue with Java 9 `Module`s, for which JPMS disallows
split packages. The `encoding-base16`, `encoding-base32` and `encoding-base64`
dependencies were the cause of the issue because their builder classes and functions
were located in the same package named `io.matthewnelson.encoding.builders`. Those
classes and functions were subsequently deprecated in release `1.2.3` so consumers
could gracefully update before upgrading to `2.0.0` where they have been removed.

For more details, see [[#124]][124].

## Migration guide for 1.x.x -> 2.0.0

- Update dependency to `1.2.3`
- Migration method 1:
- Use your IDE or editor to search your project for the following
```
import io.matthewnelson.encoding.builders
```
- Replace `.builders` with the new package location (either `base16`, `base32`, or `base64`)
- Migration method 2:
- Use the provided `ReplaceWith` functionality of the `@Deprecated` notice
to update to the new builder class/function package locations.
- Update dependency to `2.0.0`

[124]: https://github.com/05nelsonm/encoding/issues/124
18 changes: 18 additions & 0 deletions library/encoding-base16/api/encoding-base16.api
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ public final class io/matthewnelson/encoding/base16/Base16$Config : io/matthewne
public synthetic fun <init> (ZBZLkotlin/jvm/internal/DefaultConstructorMarker;)V
}

public final class io/matthewnelson/encoding/base16/Base16ConfigBuilder {
public field encodeToLowercase Z
public field isLenient Z
public field lineBreakInterval B
public fun <init> ()V
public fun <init> (Lio/matthewnelson/encoding/base16/Base16$Config;)V
public final fun build ()Lio/matthewnelson/encoding/base16/Base16$Config;
public final fun strict ()Lio/matthewnelson/encoding/base16/Base16ConfigBuilder;
}

public final class io/matthewnelson/encoding/base16/BuildersKt {
public static final fun Base16 ()Lio/matthewnelson/encoding/base16/Base16;
public static final fun Base16 (Lio/matthewnelson/encoding/base16/Base16$Config;Lkotlin/jvm/functions/Function1;)Lio/matthewnelson/encoding/base16/Base16;
public static final fun Base16 (Lkotlin/jvm/functions/Function1;)Lio/matthewnelson/encoding/base16/Base16;
public static final fun Base16 (Z)Lio/matthewnelson/encoding/base16/Base16;
public static synthetic fun Base16$default (ZILjava/lang/Object;)Lio/matthewnelson/encoding/base16/Base16;
}

public final class io/matthewnelson/encoding/builders/Base16ConfigBuilder {
public field encodeToLowercase Z
public field isLenient Z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package io.matthewnelson.component.encoding.base16

import io.matthewnelson.encoding.builders.Base16
import io.matthewnelson.encoding.base16.Base16
import io.matthewnelson.encoding.core.Decoder.Companion.decodeToByteArrayOrNull
import io.matthewnelson.encoding.core.Encoder.Companion.encodeToByteArray
import io.matthewnelson.encoding.core.Encoder.Companion.encodeToCharArray
Expand All @@ -28,7 +28,7 @@ import io.matthewnelson.encoding.core.Encoder.Companion.encodeToString
replaceWith = ReplaceWith(
expression = "this.decodeToByteArrayOrNull(Base16())",
imports = [
"io.matthewnelson.encoding.builders.Base16",
"io.matthewnelson.encoding.base16.Base16",
"io.matthewnelson.encoding.core.Decoder.Companion.decodeToByteArrayOrNull",
],
),
Expand All @@ -44,7 +44,7 @@ public inline fun String.decodeBase16ToArray(): ByteArray? {
replaceWith = ReplaceWith(
expression = "this.decodeToByteArrayOrNull(Base16())",
imports = [
"io.matthewnelson.encoding.builders.Base16",
"io.matthewnelson.encoding.base16.Base16",
"io.matthewnelson.encoding.core.Decoder.Companion.decodeToByteArrayOrNull",
],
),
Expand All @@ -59,7 +59,7 @@ public fun CharArray.decodeBase16ToArray(): ByteArray? {
replaceWith = ReplaceWith(
expression = "this.encodeToString(Base16())",
imports = [
"io.matthewnelson.encoding.builders.Base16",
"io.matthewnelson.encoding.base16.Base16",
"io.matthewnelson.encoding.core.Encoder.Companion.encodeToString",
],
),
Expand All @@ -75,7 +75,7 @@ public inline fun ByteArray.encodeBase16(): String {
replaceWith = ReplaceWith(
expression = "this.encodeToCharArray(Base16())",
imports = [
"io.matthewnelson.encoding.builders.Base16",
"io.matthewnelson.encoding.base16.Base16",
"io.matthewnelson.encoding.core.Encoder.Companion.encodeToCharArray",
],
),
Expand All @@ -91,7 +91,7 @@ public inline fun ByteArray.encodeBase16ToCharArray(): CharArray {
ReplaceWith(
expression = "this.encodeToByteArray(Base16())",
imports = [
"io.matthewnelson.encoding.builders.Base16",
"io.matthewnelson.encoding.base16.Base16",
"io.matthewnelson.encoding.core.Encoder.Companion.encodeToByteArray",
],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package io.matthewnelson.encoding.base16

import io.matthewnelson.encoding.builders.Base16
import io.matthewnelson.encoding.builders.Base16ConfigBuilder
import io.matthewnelson.encoding.core.*
import io.matthewnelson.encoding.core.util.DecoderInput
import io.matthewnelson.encoding.core.util.FeedBuffer
Expand Down Expand Up @@ -49,7 +47,7 @@ import kotlin.jvm.JvmSynthetic
* val decoded = encoded.decodeToByteArray(Base16).decodeToString()
* assertEquals(text, decoded)
*
* @see [io.matthewnelson.encoding.builders.Base16]
* @see [io.matthewnelson.encoding.base16.Base16]
* @see [Base16.Config]
* @see [Base16.CHARS_UPPER]
* @see [Base16.CHARS_LOWER]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Copyright (c) 2023 Matthew Nelson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
@file:Suppress("SpellCheckingInspection")

package io.matthewnelson.encoding.base16

import io.matthewnelson.encoding.base16.Base16.Config
import io.matthewnelson.encoding.core.EncodingException
import kotlin.jvm.JvmField
import kotlin.jvm.JvmOverloads

/**
* Creates a configured [Base16] encoder/decoder.
*
* @param [config] inherit settings from.
* @see [Base16ConfigBuilder]
* */
public fun Base16(
config: Config?,
block: Base16ConfigBuilder.() -> Unit
): Base16 {
val builder = Base16ConfigBuilder(config)
block.invoke(builder)
return Base16(builder.build())
}

/**
* Creates a configured [Base16] encoder/decoder.
*
* @see [Base16ConfigBuilder]
* */
public fun Base16(
block: Base16ConfigBuilder.() -> Unit
): Base16 {
return Base16(config = null, block)
}

/**
* Creates a configured [Base16] encoder/decoder
* using the default settings.
*
* @param [strict] If true, configures the encoder/decoder
* to be in strict accordance with RFC 4648.
* @see [Base16ConfigBuilder]
* */
@JvmOverloads
public fun Base16(strict: Boolean = false): Base16 = Base16 { if (strict) strict() }


/**
* Builder for creating a [Base16.Config].
*
* @see [strict]
* @see [io.matthewnelson.encoding.base16.Base16]
* */
public class Base16ConfigBuilder {

public constructor()
public constructor(config: Config?): this() {
if (config == null) return
isLenient = config.isLenient ?: true
lineBreakInterval = config.lineBreakInterval
encodeToLowercase = config.encodeToLowercase
}

/**
* If true, spaces and new lines ('\n', '\r', ' ', '\t')
* will be skipped over when decoding (against RFC 4648).
*
* If false, an [EncodingException] will be thrown if
* those characters are encountered when decoding.
* */
@JvmField
public var isLenient: Boolean = true

/**
* For every [lineBreakInterval] of encoded data, a
* line break will be output.
*
* Will **ONLY** output line breaks if [isLenient] is
* set to **true**.
*
* e.g.
*
* isLenient = true
* lineBreakInterval = 0
* // 48656C6C6F20576F726C6421
*
* isLenient = true
* lineBreakInterval = 16
* // 48656C6C6F20576F
* // 726C6421
*
* isLenient = false
* lineBreakInterval = 16
* // 48656C6C6F20576F726C6421
*
* Enable by setting to a value between 1 and 127, and
* setting [isLenient] to true.
*
* A great value is 64
* */
@JvmField
public var lineBreakInterval: Byte = 0

/**
* If true, will output lowercase characters when
* encoding (against RFC 4648).
*
* If false, will output uppercase characters when
* encoding.
* */
@JvmField
public var encodeToLowercase: Boolean = false

/**
* A shortcut for configuring things to be in strict
* adherence with RFC 4648.
* */
public fun strict(): Base16ConfigBuilder {
isLenient = false
lineBreakInterval = 0
encodeToLowercase = false
return this
}

/**
* Builds a [Base16.Config] for the provided settings.
* */
public fun build(): Config = Config.from(this)
}
Loading

0 comments on commit 640bd82

Please sign in to comment.