├── project ├── build.properties ├── plugins.sbt └── Versions.scala ├── version.sbt ├── docs └── src │ └── site │ ├── fonts │ ├── fira │ │ ├── eot │ │ │ ├── FiraMono-Bold.eot │ │ │ ├── FiraMono-Medium.eot │ │ │ └── FiraMono-Regular.eot │ │ ├── otf │ │ │ ├── FiraMono-Bold.otf │ │ │ ├── FiraMono-Medium.otf │ │ │ └── FiraMono-Regular.otf │ │ ├── ttf │ │ │ ├── FiraMono-Bold.ttf │ │ │ ├── FiraMono-Medium.ttf │ │ │ └── FiraMono-Regular.ttf │ │ ├── woff │ │ │ ├── FiraMono-Bold.woff │ │ │ ├── FiraMono-Medium.woff │ │ │ └── FiraMono-Regular.woff │ │ ├── woff2 │ │ │ ├── FiraMono-Bold.woff2 │ │ │ ├── FiraMono-Medium.woff2 │ │ │ └── FiraMono-Regular.woff2 │ │ └── fira.css │ └── fontawesome │ │ ├── FontAwesome.otf │ │ ├── fontawesome-webfont.eot │ │ ├── fontawesome-webfont.ttf │ │ ├── fontawesome-webfont.woff │ │ └── fontawesome-webfont.woff2 │ ├── _layouts │ ├── default.html │ ├── tutorial.html │ └── index.html │ ├── _config.yml │ └── _includes │ ├── tutorials.html │ └── header.html ├── .jvmopts ├── .gitignore ├── shapeless ├── core │ ├── shared │ │ └── src │ │ │ ├── main │ │ │ └── scala │ │ │ │ └── kantan │ │ │ │ └── codecs │ │ │ │ └── shapeless │ │ │ │ └── package.scala │ │ │ └── test │ │ │ └── scala │ │ │ └── kantan │ │ │ └── codecs │ │ │ └── shapeless │ │ │ ├── Instances.scala │ │ │ └── InstancesTests.scala │ └── jvm │ │ └── src │ │ └── test │ │ └── scala │ │ └── kantan │ │ └── codecs │ │ └── shapeless │ │ └── SerializationTests.scala └── laws │ └── shared │ └── src │ └── main │ └── scala │ └── kantan │ └── codecs │ └── shapeless │ └── laws │ ├── discipline │ ├── package.scala │ └── arbitrary.scala │ └── Or.scala ├── enumeratum ├── core │ ├── shared │ │ └── src │ │ │ ├── main │ │ │ └── scala │ │ │ │ └── kantan │ │ │ │ └── codecs │ │ │ │ └── enumeratum │ │ │ │ ├── package.scala │ │ │ │ └── values │ │ │ │ └── package.scala │ │ │ └── test │ │ │ └── scala │ │ │ └── kantan │ │ │ └── codecs │ │ │ └── enumeratum │ │ │ ├── EnumCodecTests.scala │ │ │ └── values │ │ │ ├── IntEnumCodecTests.scala │ │ │ ├── ByteEnumCodecTests.scala │ │ │ ├── CharEnumCodecTests.scala │ │ │ ├── LongEnumCodecTests.scala │ │ │ ├── ShortEnumCodecTests.scala │ │ │ └── StringEnumCodecTests.scala │ └── jvm │ │ └── src │ │ └── test │ │ └── scala │ │ └── kantan │ │ └── codecs │ │ └── enumeratum │ │ └── SerializationTests.scala └── laws │ └── shared │ └── src │ └── main │ └── scala │ └── kantan │ └── codecs │ └── enumeratum │ └── laws │ └── discipline │ └── package.scala ├── laws ├── js │ └── src │ │ └── main │ │ └── scala │ │ └── kantan │ │ └── codecs │ │ └── laws │ │ └── discipline │ │ ├── PlatformSpecificDisciplinePackage.scala │ │ └── ArbitraryInstances.scala ├── jvm │ └── src │ │ └── main │ │ └── scala │ │ └── kantan │ │ └── codecs │ │ └── laws │ │ ├── discipline │ │ ├── PlatformSpecificDisciplinePackage.scala │ │ └── SerializableTests.scala │ │ └── SerializableLaws.scala └── shared │ └── src │ └── main │ ├── scala │ └── kantan │ │ └── codecs │ │ └── laws │ │ ├── discipline │ │ ├── equality.scala │ │ ├── DisciplineSuite.scala │ │ ├── package.scala │ │ ├── StringEncoderTests.scala │ │ ├── StringCodecTests.scala │ │ ├── StringDecoderTests.scala │ │ ├── OptionalTests.scala │ │ ├── DisciplinePackage.scala │ │ └── EncoderTests.scala │ │ ├── OptionalLaws.scala │ │ ├── package.scala │ │ └── CodecValue.scala │ └── boilerplate │ └── kantan │ └── codecs │ └── laws │ └── discipline │ └── ArbitraryArities.template ├── cats ├── core │ └── shared │ │ └── src │ │ ├── main │ │ └── scala │ │ │ └── kantan │ │ │ └── codecs │ │ │ └── cats │ │ │ └── package.scala │ │ └── test │ │ └── scala │ │ └── kantan │ │ └── codecs │ │ └── cats │ │ ├── EncoderTests.scala │ │ ├── DecodeErrorTests.scala │ │ └── DecoderTests.scala └── laws │ └── shared │ └── src │ └── main │ └── scala │ └── kantan │ └── codecs │ └── cats │ └── laws │ └── discipline │ ├── package.scala │ ├── arbitrary.scala │ └── equality.scala ├── scalaz ├── core │ └── shared │ │ └── src │ │ ├── main │ │ └── scala │ │ │ └── kantan │ │ │ └── codecs │ │ │ └── scalaz │ │ │ └── package.scala │ │ └── test │ │ └── scala │ │ └── kantan │ │ └── codecs │ │ └── scalaz │ │ ├── EncoderTests.scala │ │ ├── DecodeErrorTests.scala │ │ ├── DecoderTests.scala │ │ ├── MaybeCodecTests.scala │ │ └── DisjunctionCodecTests.scala └── laws │ └── shared │ └── src │ └── main │ └── scala │ └── kantan │ └── codecs │ └── scalaz │ └── laws │ └── discipline │ ├── package.scala │ ├── ScalazDisciplineSuite.scala │ └── equality.scala ├── core ├── shared │ └── src │ │ ├── main │ │ ├── scala-2.13 │ │ │ └── kantan │ │ │ │ └── codecs │ │ │ │ ├── collection │ │ │ │ └── package.scala │ │ │ │ ├── VersionSpecificResultCompanion.scala │ │ │ │ └── resource │ │ │ │ └── VersionSpecificResourceIterator.scala │ │ ├── scala │ │ │ └── kantan │ │ │ │ └── codecs │ │ │ │ ├── export │ │ │ │ ├── Exported.scala │ │ │ │ ├── DerivedEncoder.scala │ │ │ │ ├── DerivedDecoder.scala │ │ │ │ └── package.scala │ │ │ │ ├── strings │ │ │ │ ├── StringResult.scala │ │ │ │ ├── StringEncoder.scala │ │ │ │ ├── DecodeError.scala │ │ │ │ ├── StringCodec.scala │ │ │ │ ├── package.scala │ │ │ │ └── StringDecoder.scala │ │ │ │ ├── resource │ │ │ │ ├── OpenResult.scala │ │ │ │ ├── ProcessResult.scala │ │ │ │ ├── InputResource.scala │ │ │ │ ├── OutputResource.scala │ │ │ │ ├── ReaderResource.scala │ │ │ │ ├── WriterResource.scala │ │ │ │ ├── CloseResult.scala │ │ │ │ ├── Closeable.scala │ │ │ │ └── ResourceError.scala │ │ │ │ ├── Optional.scala │ │ │ │ └── error │ │ │ │ └── Error.scala │ │ └── scala-2.12 │ │ │ └── kantan │ │ │ └── codecs │ │ │ ├── VersionSpecificResultCompanion.scala │ │ │ └── resource │ │ │ └── VersionSpecificResourceIterator.scala │ │ └── test │ │ ├── scala │ │ └── kantan │ │ │ └── codecs │ │ │ ├── DefaultOptionalTests.scala │ │ │ ├── strings │ │ │ ├── tagged.scala │ │ │ ├── IntCodecTests.scala │ │ │ ├── RegexCodecTests.scala │ │ │ ├── ByteCodecTests.scala │ │ │ ├── CharCodecTests.scala │ │ │ ├── LongCodecTests.scala │ │ │ ├── ErrorsTests.scala │ │ │ ├── FloatCodecTests.scala │ │ │ ├── ShortCodecTests.scala │ │ │ ├── BigIntCodecTests.scala │ │ │ ├── DoubleCodecTests.scala │ │ │ ├── UUIDCodecTests.scala │ │ │ ├── BooleanCodecTests.scala │ │ │ ├── PatternCodecTests.scala │ │ │ ├── StringCodecTests.scala │ │ │ ├── BigDecimalCodecTests.scala │ │ │ ├── OptionCodecTests.scala │ │ │ └── EitherCodecTests.scala │ │ │ ├── EncoderCompanionTests.scala │ │ │ ├── CodecCompanionTests.scala │ │ │ └── DecoderCompanionTests.scala │ │ ├── scala-2.12 │ │ └── kantan │ │ │ └── codecs │ │ │ └── resource │ │ │ └── VersionSpecificResourceIteratorTests.scala │ │ └── scala-2.13 │ │ └── kantan │ │ └── codecs │ │ └── resource │ │ └── VersionSpecificResourceIteratorTests.scala ├── js │ └── src │ │ └── main │ │ └── scala │ │ └── kantan │ │ └── codecs │ │ ├── resource │ │ └── PlatformSpecificInstances.scala │ │ └── strings │ │ └── PlatformSpecificInstances.scala └── jvm │ └── src │ ├── test │ ├── scala │ │ └── kantan │ │ │ └── codecs │ │ │ ├── resource │ │ │ └── bom │ │ │ │ └── InMemoryBomWriter.scala │ │ │ ├── strings │ │ │ ├── URICodecTests.scala │ │ │ ├── URLCodecTests.scala │ │ │ ├── FileCodecTests.scala │ │ │ ├── PathCodecTests.scala │ │ │ ├── AccessModeCodecTests.scala │ │ │ └── DateCodecTests.scala │ │ │ └── collection │ │ │ └── SerializationTests.scala │ ├── scala-2.12 │ │ └── kantan │ │ │ └── codecs │ │ │ └── collection │ │ │ └── VersionSpecificSerializationTests.scala │ └── scala-2.13 │ │ └── kantan │ │ └── codecs │ │ └── collection │ │ └── VersionSpecificSerializationTests.scala │ └── main │ └── scala │ └── kantan │ └── codecs │ └── resource │ ├── bom │ ├── BomWriter.scala │ ├── ByteOrderMark.scala │ └── package.scala │ └── PlatformSpecificInstances.scala ├── libra ├── laws │ └── src │ │ └── main │ │ └── scala │ │ └── kantan │ │ └── codecs │ │ └── libra │ │ └── laws │ │ └── discipline │ │ ├── package.scala │ │ └── arbitrary.scala └── core │ └── src │ └── main │ └── scala │ └── kantan │ └── codecs │ └── libra │ ├── package.scala │ └── LibraInstances.scala ├── java8 ├── laws │ └── src │ │ └── main │ │ └── scala │ │ └── kantan │ │ └── codecs │ │ └── strings │ │ └── java8 │ │ └── laws │ │ └── discipline │ │ └── package.scala └── core │ └── src │ ├── test │ └── scala │ │ └── kantan │ │ └── codecs │ │ └── strings │ │ └── java8 │ │ ├── codec.scala │ │ ├── InstantCodecTests.scala │ │ ├── LocalDateCodecTests.scala │ │ ├── LocalTimeCodecTests.scala │ │ ├── LocalDateTimeCodecTests.scala │ │ ├── ZonedDateTimeCodecTests.scala │ │ └── OffsetDateTimeCodecTests.scala │ └── main │ └── scala │ └── kantan │ └── codecs │ └── strings │ └── java8 │ └── FormatLiteral.scala ├── refined ├── laws │ └── shared │ │ └── src │ │ └── main │ │ └── scala │ │ └── kantan │ │ └── codecs │ │ └── refined │ │ └── laws │ │ └── discipline │ │ ├── package.scala │ │ └── arbitrary.scala └── core │ ├── shared │ └── src │ │ ├── main │ │ └── scala │ │ │ └── kantan │ │ │ └── codecs │ │ │ └── refined │ │ │ ├── package.scala │ │ │ └── RefinedInstances.scala │ │ └── test │ │ └── scala │ │ └── kantan │ │ └── codecs │ │ └── refined │ │ └── RefinedCodecTests.scala │ └── jvm │ └── src │ └── test │ └── scala │ └── kantan │ └── codecs │ └── refined │ └── SerializationTests.scala ├── .github └── workflows │ └── cli.yml └── README.md /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=1.10.7 2 | -------------------------------------------------------------------------------- /version.sbt: -------------------------------------------------------------------------------- 1 | ThisBuild / version := "0.6.1-SNAPSHOT" 2 | -------------------------------------------------------------------------------- /docs/src/site/fonts/fira/eot/FiraMono-Bold.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nrinaudo/kantan.codecs/HEAD/docs/src/site/fonts/fira/eot/FiraMono-Bold.eot -------------------------------------------------------------------------------- /docs/src/site/fonts/fira/otf/FiraMono-Bold.otf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nrinaudo/kantan.codecs/HEAD/docs/src/site/fonts/fira/otf/FiraMono-Bold.otf -------------------------------------------------------------------------------- /docs/src/site/fonts/fira/ttf/FiraMono-Bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nrinaudo/kantan.codecs/HEAD/docs/src/site/fonts/fira/ttf/FiraMono-Bold.ttf -------------------------------------------------------------------------------- /docs/src/site/fonts/fontawesome/FontAwesome.otf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nrinaudo/kantan.codecs/HEAD/docs/src/site/fonts/fontawesome/FontAwesome.otf -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("com.nrinaudo" % "kantan.sbt-kantan" % "2.9.0") 2 | addSbtPlugin("com.nrinaudo" % "kantan.sbt-scalajs" % "2.9.0") 3 | -------------------------------------------------------------------------------- /docs/src/site/fonts/fira/eot/FiraMono-Medium.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nrinaudo/kantan.codecs/HEAD/docs/src/site/fonts/fira/eot/FiraMono-Medium.eot -------------------------------------------------------------------------------- /docs/src/site/fonts/fira/eot/FiraMono-Regular.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nrinaudo/kantan.codecs/HEAD/docs/src/site/fonts/fira/eot/FiraMono-Regular.eot -------------------------------------------------------------------------------- /docs/src/site/fonts/fira/otf/FiraMono-Medium.otf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nrinaudo/kantan.codecs/HEAD/docs/src/site/fonts/fira/otf/FiraMono-Medium.otf -------------------------------------------------------------------------------- /docs/src/site/fonts/fira/otf/FiraMono-Regular.otf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nrinaudo/kantan.codecs/HEAD/docs/src/site/fonts/fira/otf/FiraMono-Regular.otf -------------------------------------------------------------------------------- /docs/src/site/fonts/fira/ttf/FiraMono-Medium.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nrinaudo/kantan.codecs/HEAD/docs/src/site/fonts/fira/ttf/FiraMono-Medium.ttf -------------------------------------------------------------------------------- /docs/src/site/fonts/fira/ttf/FiraMono-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nrinaudo/kantan.codecs/HEAD/docs/src/site/fonts/fira/ttf/FiraMono-Regular.ttf -------------------------------------------------------------------------------- /docs/src/site/fonts/fira/woff/FiraMono-Bold.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nrinaudo/kantan.codecs/HEAD/docs/src/site/fonts/fira/woff/FiraMono-Bold.woff -------------------------------------------------------------------------------- /docs/src/site/fonts/fira/woff/FiraMono-Medium.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nrinaudo/kantan.codecs/HEAD/docs/src/site/fonts/fira/woff/FiraMono-Medium.woff -------------------------------------------------------------------------------- /docs/src/site/fonts/fira/woff2/FiraMono-Bold.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nrinaudo/kantan.codecs/HEAD/docs/src/site/fonts/fira/woff2/FiraMono-Bold.woff2 -------------------------------------------------------------------------------- /docs/src/site/fonts/fira/woff/FiraMono-Regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nrinaudo/kantan.codecs/HEAD/docs/src/site/fonts/fira/woff/FiraMono-Regular.woff -------------------------------------------------------------------------------- /docs/src/site/fonts/fira/woff2/FiraMono-Medium.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nrinaudo/kantan.codecs/HEAD/docs/src/site/fonts/fira/woff2/FiraMono-Medium.woff2 -------------------------------------------------------------------------------- /docs/src/site/fonts/fira/woff2/FiraMono-Regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nrinaudo/kantan.codecs/HEAD/docs/src/site/fonts/fira/woff2/FiraMono-Regular.woff2 -------------------------------------------------------------------------------- /docs/src/site/fonts/fontawesome/fontawesome-webfont.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nrinaudo/kantan.codecs/HEAD/docs/src/site/fonts/fontawesome/fontawesome-webfont.eot -------------------------------------------------------------------------------- /docs/src/site/fonts/fontawesome/fontawesome-webfont.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nrinaudo/kantan.codecs/HEAD/docs/src/site/fonts/fontawesome/fontawesome-webfont.ttf -------------------------------------------------------------------------------- /docs/src/site/fonts/fontawesome/fontawesome-webfont.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nrinaudo/kantan.codecs/HEAD/docs/src/site/fonts/fontawesome/fontawesome-webfont.woff -------------------------------------------------------------------------------- /docs/src/site/fonts/fontawesome/fontawesome-webfont.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nrinaudo/kantan.codecs/HEAD/docs/src/site/fonts/fontawesome/fontawesome-webfont.woff2 -------------------------------------------------------------------------------- /docs/src/site/_layouts/default.html: -------------------------------------------------------------------------------- 1 | {% include header.html %} 2 |
3 | {% if page.title %}

{{ page.title }}

{% endif %} 4 | {{ content }} 5 |
6 | 7 | 8 | -------------------------------------------------------------------------------- /docs/src/site/_config.yml: -------------------------------------------------------------------------------- 1 | name: kantan.codecs 2 | markdown: kramdown 3 | highlighter: rouge 4 | baseurl: /kantan.codecs 5 | 6 | kramdown: 7 | input: GFM 8 | hard_wrap: false 9 | 10 | collections: 11 | tut: 12 | output: true 13 | -------------------------------------------------------------------------------- /.jvmopts: -------------------------------------------------------------------------------- 1 | # Same JVM opts as cats 2 | 3 | # see https://weblogs.java.net/blog/kcpeppe/archive/2013/12/11/case-study-jvm-hotspot-flags 4 | -Dfile.encoding=UTF8 5 | -Xms1G 6 | -Xmx13G 7 | -XX:ReservedCodeCacheSize=250M 8 | -XX:+TieredCompilation 9 | -XX:-UseGCOverheadLimit 10 | -------------------------------------------------------------------------------- /docs/src/site/_layouts/tutorial.html: -------------------------------------------------------------------------------- 1 | {% include header.html %} 2 |
3 | {% if page.title %}

{{ page.title }}

{% endif %} 4 | {{ content }} 5 | 6 |
7 | Other tutorials: 8 | {% include tutorials.html %} 9 |
10 | 11 | 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # IDE and other personal preferences go to a global gitignore or .git/info/exclude 2 | 3 | # Scala ecosystem 4 | .bloop 5 | .bsp 6 | .metals 7 | project/boot/ 8 | project/plugins/project/ 9 | target/ 10 | 11 | # Project 12 | ## generated files 13 | .scalafix.conf 14 | .scalafmt.conf 15 | -------------------------------------------------------------------------------- /docs/src/site/_layouts/index.html: -------------------------------------------------------------------------------- 1 | {% include header.html %} 2 |
3 | {% if page.title %}

{{ page.title }}

{% endif %} 4 | {{ content }} 5 | 6 | 7 |

Tutorials

8 | 9 | The following tutorials are available: 10 | {% include tutorials.html %} 11 |
12 | 13 | 14 | -------------------------------------------------------------------------------- /docs/src/site/_includes/tutorials.html: -------------------------------------------------------------------------------- 1 | 15 | -------------------------------------------------------------------------------- /project/Versions.scala: -------------------------------------------------------------------------------- 1 | object Versions { 2 | val cats = "2.12.0" 3 | val collectionCompat = "2.12.0" 4 | val commonsIo = "2.18.0" 5 | val disciplineScalatest = "2.3.0" 6 | val enumeratum = "1.7.5" 7 | val enumeratumScalacheck = "1.7.5" 8 | val imp = "0.5.0" 9 | val libra = "0.7.0" 10 | val refined = "0.11.3" 11 | val scalacheck = "1.18.1" 12 | val scalacheckShapeless = "1.3.2" 13 | val scalatest = "3.2.19" 14 | val scalaz = "7.3.8" 15 | val shapeless = "2.3.12" 16 | } 17 | -------------------------------------------------------------------------------- /docs/src/site/fonts/fira/fira.css: -------------------------------------------------------------------------------- 1 | @font-face{ 2 | font-family: 'Fira Mono'; 3 | src: url('eot/FiraMono-Regular.eot'); 4 | src: local('Fira Mono'), 5 | url('eot/FiraMono-Regular.eot') format('embedded-opentype'), 6 | url('woff/FiraMono-Regular.woff') format('woff'), 7 | url('ttf/FiraMono-Regular.ttf') format('truetype'); 8 | font-weight: 400; 9 | font-style: normal; 10 | } 11 | 12 | @font-face{ 13 | font-family: 'Fira Mono'; 14 | src: url('eot/FiraMono-Bold.eot'); 15 | src: local('Fira Mono Bold'), 16 | url('eot/FiraMono-Bold.eot') format('embedded-opentype'), 17 | url('woff/FiraMono-Bold.woff') format('woff'), 18 | url('ttf/FiraMono-Bold.ttf') format('truetype'); 19 | font-weight: 600; 20 | font-style: normal; 21 | } 22 | -------------------------------------------------------------------------------- /shapeless/core/shared/src/main/scala/kantan/codecs/shapeless/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs 18 | 19 | package object shapeless extends ShapelessInstances 20 | -------------------------------------------------------------------------------- /enumeratum/core/shared/src/main/scala/kantan/codecs/enumeratum/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs 18 | 19 | package object enumeratum extends DecoderInstances with EncoderInstances 20 | -------------------------------------------------------------------------------- /laws/js/src/main/scala/kantan/codecs/laws/discipline/PlatformSpecificDisciplinePackage.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.laws.discipline 18 | 19 | trait PlatformSpecificDisciplinePackage 20 | -------------------------------------------------------------------------------- /cats/core/shared/src/main/scala/kantan/codecs/cats/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs 18 | 19 | package object cats extends CommonInstances with DecoderInstances with EncoderInstances 20 | -------------------------------------------------------------------------------- /scalaz/core/shared/src/main/scala/kantan/codecs/scalaz/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs 18 | 19 | package object scalaz extends DecoderInstances with EncoderInstances with CommonInstances 20 | -------------------------------------------------------------------------------- /core/shared/src/main/scala-2.13/kantan/codecs/collection/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs 18 | 19 | package object collection { 20 | type Factory[A, C] = scala.collection.Factory[A, C] 21 | } 22 | -------------------------------------------------------------------------------- /enumeratum/core/shared/src/main/scala/kantan/codecs/enumeratum/values/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.enumeratum 18 | 19 | package object values extends DecoderInstances with EncoderInstances 20 | -------------------------------------------------------------------------------- /libra/laws/src/main/scala/kantan/codecs/libra/laws/discipline/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.libra.laws 18 | 19 | package object discipline extends kantan.codecs.laws.discipline.DisciplinePackage 20 | -------------------------------------------------------------------------------- /cats/laws/shared/src/main/scala/kantan/codecs/cats/laws/discipline/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.cats.laws 18 | 19 | package object discipline extends kantan.codecs.laws.discipline.DisciplinePackage 20 | -------------------------------------------------------------------------------- /core/shared/src/main/scala/kantan/codecs/export/Exported.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.`export` 18 | 19 | /** Wraps derived type class instances. */ 20 | final case class Exported[A](value: A) extends AnyVal 21 | -------------------------------------------------------------------------------- /core/js/src/main/scala/kantan/codecs/resource/PlatformSpecificInstances.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.resource 18 | 19 | /** Scala.js specific instances of [[Resource]]. */ 20 | trait PlatformSpecificInstances 21 | -------------------------------------------------------------------------------- /scalaz/laws/shared/src/main/scala/kantan/codecs/scalaz/laws/discipline/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.scalaz.laws 18 | 19 | package object discipline extends kantan.codecs.laws.discipline.DisciplinePackage 20 | -------------------------------------------------------------------------------- /java8/laws/src/main/scala/kantan/codecs/strings/java8/laws/discipline/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings.java8.laws 18 | 19 | package object discipline extends kantan.codecs.laws.discipline.DisciplinePackage 20 | -------------------------------------------------------------------------------- /refined/laws/shared/src/main/scala/kantan/codecs/refined/laws/discipline/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.refined.laws 18 | 19 | package object discipline extends kantan.codecs.laws.discipline.DisciplinePackage 20 | -------------------------------------------------------------------------------- /shapeless/laws/shared/src/main/scala/kantan/codecs/shapeless/laws/discipline/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.shapeless.laws 18 | 19 | package object discipline extends kantan.codecs.laws.discipline.DisciplinePackage 20 | -------------------------------------------------------------------------------- /core/shared/src/main/scala/kantan/codecs/strings/StringResult.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.ResultCompanion 20 | 21 | object StringResult extends ResultCompanion.WithError[DecodeError] 22 | -------------------------------------------------------------------------------- /enumeratum/laws/shared/src/main/scala/kantan/codecs/enumeratum/laws/discipline/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.enumeratum.laws 18 | 19 | package object discipline extends kantan.codecs.laws.discipline.DisciplinePackage 20 | -------------------------------------------------------------------------------- /core/shared/src/main/scala/kantan/codecs/resource/OpenResult.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.resource 18 | 19 | import kantan.codecs.ResultCompanion 20 | 21 | object OpenResult extends ResultCompanion.WithError[ResourceError.OpenError] 22 | -------------------------------------------------------------------------------- /core/shared/src/main/scala/kantan/codecs/resource/ProcessResult.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.resource 18 | 19 | import kantan.codecs.ResultCompanion 20 | 21 | object ProcessResult extends ResultCompanion.WithError[ResourceError.ProcessError] 22 | -------------------------------------------------------------------------------- /libra/core/src/main/scala/kantan/codecs/libra/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs 18 | 19 | /** Provides codecs for [[https://to-ithaca.github.io/libra/]] types. */ 20 | package object libra extends DecoderInstances with EncoderInstances 21 | -------------------------------------------------------------------------------- /core/shared/src/main/scala/kantan/codecs/strings/StringEncoder.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.EncoderCompanion 20 | 21 | object StringEncoder extends EncoderCompanion[String, codecs.type] with PlatformSpecificEncoders 22 | -------------------------------------------------------------------------------- /refined/core/shared/src/main/scala/kantan/codecs/refined/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs 18 | 19 | /** Provides codecs for [[https://github.com/fthomas/refined refined]] types. */ 20 | package object refined extends DecoderInstances with EncoderInstances 21 | -------------------------------------------------------------------------------- /cats/laws/shared/src/main/scala/kantan/codecs/cats/laws/discipline/arbitrary.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.cats.laws.discipline 18 | 19 | object arbitrary extends ArbitraryInstances 20 | 21 | trait ArbitraryInstances extends kantan.codecs.laws.discipline.ArbitraryInstances 22 | -------------------------------------------------------------------------------- /shapeless/laws/shared/src/main/scala/kantan/codecs/shapeless/laws/Or.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.shapeless.laws 18 | 19 | sealed trait Or[+A, +B] extends Product with Serializable 20 | final case class Left[A](a: A) extends Or[A, Nothing] 21 | final case class Right[B](b: B) extends Or[Nothing, B] 22 | -------------------------------------------------------------------------------- /laws/jvm/src/main/scala/kantan/codecs/laws/discipline/PlatformSpecificDisciplinePackage.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.laws.discipline 18 | 19 | trait PlatformSpecificDisciplinePackage { 20 | type SerializableTests[A] = kantan.codecs.laws.discipline.SerializableTests[A] 21 | val SerializableTests = kantan.codecs.laws.discipline.SerializableTests 22 | } 23 | -------------------------------------------------------------------------------- /core/shared/src/main/scala/kantan/codecs/export/DerivedEncoder.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.`export` 18 | 19 | import kantan.codecs.Encoder 20 | 21 | /** Provides construction methods for [[kantan.codecs.export.DerivedEncoder]]. */ 22 | object DerivedEncoder { 23 | def from[E, D, T](f: D => E): DerivedEncoder[E, D, T] = 24 | Exported(Encoder.from(f)) 25 | } 26 | -------------------------------------------------------------------------------- /core/shared/src/main/scala/kantan/codecs/export/DerivedDecoder.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.`export` 18 | 19 | import kantan.codecs.Decoder 20 | 21 | /** Provides construction methods for [[kantan.codecs.export.DerivedDecoder]]. */ 22 | object DerivedDecoder { 23 | def from[E, D, F, T](f: E => Either[F, D]): DerivedDecoder[E, D, F, T] = 24 | Exported(Decoder.from(f)) 25 | } 26 | -------------------------------------------------------------------------------- /core/shared/src/main/scala/kantan/codecs/strings/DecodeError.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.error.Error 20 | import kantan.codecs.error.ErrorCompanion 21 | 22 | sealed case class DecodeError(message: String) extends Error(message) 23 | 24 | object DecodeError extends ErrorCompanion[DecodeError]("an error occurred while decoding data")(s => new DecodeError(s)) 25 | -------------------------------------------------------------------------------- /core/shared/src/main/scala/kantan/codecs/resource/InputResource.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.resource 18 | 19 | /** Provides instance summoning methods for [[InputResource]]. */ 20 | object InputResource { 21 | 22 | /** Summons an implicit instance of [[InputResource]] if one is found in scope, fails compilation otherwise. */ 23 | def apply[A](implicit ev: InputResource[A]): InputResource[A] = 24 | ev 25 | } 26 | -------------------------------------------------------------------------------- /core/shared/src/main/scala/kantan/codecs/resource/OutputResource.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.resource 18 | 19 | /** Provides instance summoning methods for [[OutputResource]]. */ 20 | object OutputResource { 21 | 22 | /** Summons an implicit instance of [[OutputResource]] if one is found in scope, fails compilation otherwise. */ 23 | def apply[A](implicit ev: OutputResource[A]): OutputResource[A] = 24 | ev 25 | } 26 | -------------------------------------------------------------------------------- /core/shared/src/main/scala/kantan/codecs/resource/ReaderResource.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.resource 18 | 19 | /** Provides instance summoning methods for [[ReaderResource]]. */ 20 | object ReaderResource { 21 | 22 | /** Summons an implicit instance of [[ReaderResource]] if one is found in scope, fails compilation otherwise. */ 23 | def apply[A](implicit ev: ReaderResource[A]): ReaderResource[A] = 24 | ev 25 | } 26 | -------------------------------------------------------------------------------- /core/shared/src/main/scala/kantan/codecs/resource/WriterResource.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.resource 18 | 19 | /** Provides instance summoning methods for [[WriterResource]]. */ 20 | object WriterResource { 21 | 22 | /** Summons an implicit instance of [[WriterResource]] if one is found in scope, fails compilation otherwise. */ 23 | def apply[A](implicit ev: WriterResource[A]): WriterResource[A] = 24 | ev 25 | } 26 | -------------------------------------------------------------------------------- /core/shared/src/main/scala/kantan/codecs/resource/CloseResult.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.resource 18 | 19 | import kantan.codecs.resource.ResourceError.CloseError 20 | 21 | object CloseResult { 22 | val success: CloseResult = Right(()) 23 | def failure(error: CloseError): CloseResult = 24 | Left(error) 25 | def apply[U](c: => U): CloseResult = 26 | CloseError.safe { 27 | c 28 | () 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /laws/js/src/main/scala/kantan/codecs/laws/discipline/ArbitraryInstances.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.laws.discipline 18 | 19 | import org.scalacheck.Gen 20 | 21 | import java.io.EOFException 22 | import java.io.IOException 23 | 24 | trait ArbitraryInstances extends CommonArbitraryInstances { 25 | 26 | override val genIoException: Gen[IOException] = 27 | Gen.oneOf(genUnsupportedEncoding, Gen.const(new EOFException)) 28 | 29 | } 30 | -------------------------------------------------------------------------------- /laws/shared/src/main/scala/kantan/codecs/laws/discipline/equality.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.laws.discipline 18 | 19 | import org.scalacheck.Arbitrary 20 | 21 | object equality { 22 | def eq[A, B: Arbitrary](a1: B => A, a2: B => A)(f: (A, A) => Boolean): Boolean = { 23 | val samples = List.fill(100)(Arbitrary.arbitrary[B].sample).collect { case Some(a) => 24 | a 25 | } 26 | samples.forall(b => f(a1(b), a2(b))) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /shapeless/laws/shared/src/main/scala/kantan/codecs/shapeless/laws/discipline/arbitrary.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.shapeless.laws.discipline 18 | 19 | import org.scalacheck.derive._ 20 | 21 | object arbitrary extends ArbitraryInstances with kantan.codecs.laws.discipline.ArbitraryInstances 22 | 23 | trait ArbitraryInstances 24 | extends SingletonInstances with HListInstances with CoproductInstances with DerivedInstances with FieldTypeInstances 25 | -------------------------------------------------------------------------------- /core/jvm/src/test/scala/kantan/codecs/resource/bom/InMemoryBomWriter.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.resource.bom 18 | 19 | import java.io.ByteArrayOutputStream 20 | import scala.io.Codec 21 | 22 | object InMemoryBomWriter { 23 | def write(str: String, codec: Codec): Array[Byte] = { 24 | val bytes = new ByteArrayOutputStream() 25 | val out = BomWriter(bytes, codec) 26 | out.write(str) 27 | out.close() 28 | bytes.toByteArray 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /laws/shared/src/main/scala/kantan/codecs/laws/discipline/DisciplineSuite.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.laws.discipline 18 | 19 | import org.scalatest.funsuite.AnyFunSuite 20 | import org.scalatest.matchers.should.Matchers 21 | import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks 22 | import org.typelevel.discipline.scalatest.FunSuiteDiscipline 23 | 24 | trait DisciplineSuite extends AnyFunSuite with ScalaCheckPropertyChecks with FunSuiteDiscipline with Matchers 25 | -------------------------------------------------------------------------------- /core/js/src/main/scala/kantan/codecs/strings/PlatformSpecificInstances.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | /** Scala.js-specific codec instances. */ 20 | trait PlatformSpecificInstances 21 | 22 | /** Scala.js-specific [[StringDecoder decoders]]. */ 23 | trait PlatformSpecificDecoders 24 | 25 | /** Scala.js-specific [[StringEncoder encoders]]. */ 26 | trait PlatformSpecificEncoders 27 | 28 | /** Scala.js-specific [[StringCodec codecs]]. */ 29 | trait PlatformSpecificCodecs 30 | -------------------------------------------------------------------------------- /laws/shared/src/main/scala/kantan/codecs/laws/discipline/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.laws 18 | 19 | import kantan.codecs.strings.DecodeError 20 | import kantan.codecs.strings.codecs 21 | 22 | package object discipline { 23 | 24 | type StringEncoderTests[A] = EncoderTests[String, A, codecs.type] 25 | type StringDecoderTests[A] = DecoderTests[String, A, DecodeError, codecs.type] 26 | type StringCodecTests[A] = CodecTests[String, A, DecodeError, codecs.type] 27 | 28 | } 29 | -------------------------------------------------------------------------------- /laws/shared/src/main/scala/kantan/codecs/laws/OptionalLaws.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.laws 18 | 19 | import kantan.codecs.Optional 20 | 21 | trait OptionalLaws[A] { 22 | def optional: Optional[A] 23 | 24 | def emptyUniqueness(a: A): Boolean = 25 | a == optional.empty || !optional.isEmpty(a) 26 | } 27 | 28 | object OptionalLaws { 29 | implicit def apply[A: Optional]: OptionalLaws[A] = 30 | new OptionalLaws[A] { 31 | override val optional = Optional[A] 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /core/shared/src/test/scala/kantan/codecs/DefaultOptionalTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs 18 | 19 | import kantan.codecs.laws.discipline.DisciplineSuite 20 | import kantan.codecs.laws.discipline.OptionalTests 21 | 22 | class DefaultOptionalTests extends DisciplineSuite { 23 | checkAll("Optional[Option[Int]]", OptionalTests[Option[Int]].optional) 24 | checkAll("Optional[Seq[Int]]", OptionalTests[Seq[Int]].optional) 25 | checkAll("Optional[String]", OptionalTests[String].optional) 26 | } 27 | -------------------------------------------------------------------------------- /laws/shared/src/main/scala/kantan/codecs/laws/discipline/StringEncoderTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.laws.discipline 18 | 19 | import kantan.codecs.laws.LegalString 20 | import kantan.codecs.laws.StringEncoderLaws 21 | import kantan.codecs.strings.codecs 22 | import org.scalacheck.Arbitrary 23 | 24 | object StringEncoderTests { 25 | 26 | def apply[D: Arbitrary](implicit l: StringEncoderLaws[D], al: Arbitrary[LegalString[D]]): StringEncoderTests[D] = 27 | EncoderTests[String, D, codecs.type] 28 | } 29 | -------------------------------------------------------------------------------- /cats/core/shared/src/test/scala/kantan/codecs/cats/EncoderTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.cats 18 | 19 | import cats.laws.discipline.ContravariantTests 20 | import kantan.codecs.cats.laws.discipline.DisciplineSuite 21 | import kantan.codecs.cats.laws.discipline.arbitrary._ 22 | import kantan.codecs.cats.laws.discipline.equality._ 23 | import kantan.codecs.strings.StringEncoder 24 | 25 | class EncoderTests extends DisciplineSuite { 26 | checkAll("StringEncoder", ContravariantTests[StringEncoder].contravariant[Int, Int, Int]) 27 | } 28 | -------------------------------------------------------------------------------- /core/jvm/src/test/scala-2.12/kantan/codecs/collection/VersionSpecificSerializationTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.collection 18 | 19 | import kantan.codecs.laws.discipline.DisciplineSuite 20 | import kantan.codecs.laws.discipline.SerializableTests 21 | 22 | class VersionSpecificSerializationTests extends DisciplineSuite { 23 | checkAll("Factory[StreamFactoryTests]", SerializableTests[Factory[Int, Stream[Int]]].serializable) 24 | checkAll("Factory[TraversableFactoryTests]", SerializableTests[Factory[Int, Traversable[Int]]].serializable) 25 | } 26 | -------------------------------------------------------------------------------- /core/jvm/src/test/scala-2.13/kantan/codecs/collection/VersionSpecificSerializationTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.collection 18 | 19 | import kantan.codecs.laws.discipline.DisciplineSuite 20 | import kantan.codecs.laws.discipline.SerializableTests 21 | 22 | class VersionSpecificSerializationTests extends DisciplineSuite { 23 | checkAll("Factory[LazyListFactoryTests]", SerializableTests[Factory[Int, LazyList[Int]]].serializable) 24 | checkAll("Factory[IterableFactoryTests]", SerializableTests[Factory[Int, Iterable[Int]]].serializable) 25 | } 26 | -------------------------------------------------------------------------------- /scalaz/core/shared/src/test/scala/kantan/codecs/scalaz/EncoderTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.scalaz 18 | 19 | import kantan.codecs.scalaz.laws.discipline.ScalazDisciplineSuite 20 | import kantan.codecs.scalaz.laws.discipline.arbitrary._ 21 | import kantan.codecs.scalaz.laws.discipline.equality._ 22 | import kantan.codecs.strings.StringEncoder 23 | import scalaz.scalacheck.ScalazProperties.contravariant 24 | import scalaz.std.string._ 25 | 26 | class EncoderTests extends ScalazDisciplineSuite { 27 | checkAll("StringEncoder", contravariant.laws[StringEncoder]) 28 | } 29 | -------------------------------------------------------------------------------- /core/shared/src/main/scala/kantan/codecs/strings/StringCodec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.CodecCompanion 20 | 21 | /** Provides instance creation methods. 22 | * 23 | * No instance summoning method is provided - this is by design. Developers should never work with codecs, but with 24 | * instances of [[StringEncoder]] and [[StringDecoder]] instead. Codecs are merely meant as a declaration convenience. 25 | * 26 | * Default instances are defined in [[codecs]]. 27 | */ 28 | object StringCodec extends CodecCompanion[String, DecodeError, codecs.type] with PlatformSpecificCodecs 29 | -------------------------------------------------------------------------------- /core/jvm/src/main/scala/kantan/codecs/resource/bom/BomWriter.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.resource.bom 18 | 19 | import java.io.OutputStream 20 | import java.io.OutputStreamWriter 21 | import java.io.Writer 22 | import scala.io.Codec 23 | 24 | object BomWriter { 25 | 26 | /** Opens a `Writer` on the specified `OutputStream`, writing a BOM if the specified codec has one. */ 27 | def apply(out: OutputStream, codec: Codec): Writer = 28 | new OutputStreamWriter( 29 | ByteOrderMark.findFor(codec.charSet).fold(out) { bom => 30 | out.write(bom.bytes) 31 | out 32 | }, 33 | codec.charSet 34 | ) 35 | } 36 | -------------------------------------------------------------------------------- /core/shared/src/test/scala/kantan/codecs/strings/tagged.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.Codec 20 | import kantan.codecs.Decoder 21 | import kantan.codecs.Encoder 22 | 23 | object tagged { 24 | 25 | implicit def decoder[D: StringDecoder]: Decoder[String, D, DecodeError, tagged.type] = 26 | StringDecoder[D].tag[tagged.type] 27 | 28 | implicit def encoder[D: StringEncoder]: Encoder[String, D, tagged.type] = 29 | StringEncoder[D].tag[tagged.type] 30 | 31 | def codec[D: StringCodec]: Codec[String, D, DecodeError, tagged.type] = 32 | implicitly[StringCodec[D]].tag[tagged.type] 33 | 34 | } 35 | -------------------------------------------------------------------------------- /laws/shared/src/main/scala/kantan/codecs/laws/discipline/StringCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.laws.discipline 18 | 19 | import kantan.codecs.laws.LegalString 20 | import kantan.codecs.laws.StringCodecLaws 21 | import kantan.codecs.laws.discipline.arbitrary._ 22 | import kantan.codecs.strings.DecodeError 23 | import kantan.codecs.strings.codecs 24 | import org.scalacheck.Arbitrary 25 | import org.scalacheck.Cogen 26 | 27 | object StringCodecTests { 28 | 29 | def apply[D: Arbitrary: Cogen](implicit l: StringCodecLaws[D], al: Arbitrary[LegalString[D]]): StringCodecTests[D] = 30 | CodecTests[String, D, DecodeError, codecs.type] 31 | 32 | } 33 | -------------------------------------------------------------------------------- /docs/src/site/_includes/header.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {{ site.name }}{% if page.title %} - {{ page.title }}{% endif %} 6 | 7 | 8 | 9 | 10 | 11 | 14 | 15 | 16 | 24 | -------------------------------------------------------------------------------- /core/shared/src/test/scala/kantan/codecs/EncoderCompanionTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs 18 | 19 | import org.scalatest.funsuite.AnyFunSuite 20 | import org.scalatest.matchers.should.Matchers 21 | import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks 22 | 23 | class EncoderCompanionTests extends AnyFunSuite with ScalaCheckPropertyChecks with Matchers { 24 | object codec 25 | object Companion extends EncoderCompanion[String, codec.type] 26 | 27 | test("EncoderCompanion.from should be equivalent to Encoder.from") { 28 | forAll { (f: Int => String, i: Int) => 29 | Encoder.from(f).encode(i) should be(Companion.from(f).encode(i)) 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /enumeratum/core/jvm/src/test/scala/kantan/codecs/enumeratum/SerializationTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.enumeratum 18 | 19 | import kantan.codecs.enumeratum.laws.discipline.Enumerated 20 | import kantan.codecs.laws.discipline.DisciplineSuite 21 | import kantan.codecs.laws.discipline.SerializableTests 22 | import kantan.codecs.strings.StringDecoder 23 | import kantan.codecs.strings.StringEncoder 24 | 25 | class SerializationTests extends DisciplineSuite { 26 | 27 | checkAll("StringDecoder[Enumerated]", SerializableTests[StringDecoder[Enumerated]].serializable) 28 | checkAll("StringEncoder[Enumerated]", SerializableTests[StringEncoder[Enumerated]].serializable) 29 | 30 | } 31 | -------------------------------------------------------------------------------- /cats/core/shared/src/test/scala/kantan/codecs/cats/DecodeErrorTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.cats 18 | 19 | import cats.Show 20 | import cats.kernel.laws.discipline.EqTests 21 | import kantan.codecs.laws.discipline.DisciplineSuite 22 | import kantan.codecs.laws.discipline.arbitrary._ 23 | import kantan.codecs.strings.DecodeError 24 | 25 | class DecodeErrorTests extends DisciplineSuite { 26 | 27 | checkAll("DecodeError", EqTests[DecodeError].eqv) 28 | 29 | test("Show[DecodeError] should yield a string containing the error message") { 30 | forAll { (error: DecodeError) => 31 | Show[DecodeError].show(error) should include(error.message) 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /laws/shared/src/main/scala/kantan/codecs/laws/discipline/StringDecoderTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.laws.discipline 18 | 19 | import kantan.codecs.laws.LegalString 20 | import kantan.codecs.laws.StringDecoderLaws 21 | import kantan.codecs.laws.discipline.arbitrary._ 22 | import kantan.codecs.strings.DecodeError 23 | import kantan.codecs.strings.codecs 24 | import org.scalacheck.Arbitrary 25 | import org.scalacheck.Cogen 26 | 27 | object StringDecoderTests { 28 | 29 | def apply[D: Arbitrary: Cogen](implicit 30 | l: StringDecoderLaws[D], 31 | al: Arbitrary[LegalString[D]] 32 | ): StringDecoderTests[D] = 33 | DecoderTests[String, D, DecodeError, codecs.type] 34 | 35 | } 36 | -------------------------------------------------------------------------------- /.github/workflows/cli.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | workflow_dispatch: 4 | push: 5 | pull_request: 6 | 7 | jobs: 8 | pre_job: 9 | name: Skip Duplicate Actions 10 | runs-on: ubuntu-latest 11 | outputs: 12 | should_skip: ${{ steps.skip_check.outputs.should_skip }} 13 | steps: 14 | - id: skip_check 15 | uses: fkirc/skip-duplicate-actions@v5 16 | with: 17 | skip_after_successful_duplicate: 'true' 18 | 19 | test: 20 | needs: pre_job 21 | if: needs.pre_job.outputs.should_skip != 'true' 22 | strategy: 23 | fail-fast: false 24 | matrix: 25 | os : [ubuntu-latest] 26 | scala: [2.12.20, 2.13.15] 27 | platform: [jvm, js] 28 | java : [8] 29 | runs-on: ${{ matrix.os }} 30 | steps: 31 | - name: Checkout 32 | uses: actions/checkout@v4 33 | 34 | - name: Setup 35 | uses: coursier/setup-action@v1 36 | with: 37 | jvm: "adopt:${{ matrix.java }}" 38 | apps: sbt 39 | 40 | - name: Cache 41 | uses: coursier/cache-action@v6 42 | 43 | - name: Test JVM 44 | if: matrix.platform == 'jvm' 45 | run: sbt ++${{ matrix.scala }} validateJVM 46 | 47 | - name: Test JS 48 | if: matrix.platform == 'js' 49 | run: sbt ++${{ matrix.scala }} validateJS 50 | -------------------------------------------------------------------------------- /libra/core/src/main/scala/kantan/codecs/libra/LibraInstances.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.libra 18 | 19 | import kantan.codecs.Decoder 20 | import kantan.codecs.Encoder 21 | import libra.Quantity 22 | import shapeless.HList 23 | 24 | trait DecoderInstances { 25 | 26 | implicit def libraQuantityDecoder[E, D, F, T, X <: HList](implicit 27 | decoder: Decoder[E, D, F, T] 28 | ): Decoder[E, Quantity[D, X], F, T] = 29 | decoder.map(Quantity.apply) 30 | 31 | } 32 | 33 | trait EncoderInstances { 34 | 35 | implicit def libraQuantyEncoder[E, D, T, X <: HList](implicit 36 | encoder: Encoder[E, D, T] 37 | ): Encoder[E, Quantity[D, X], T] = 38 | encoder.contramap(_.value) 39 | 40 | } 41 | -------------------------------------------------------------------------------- /laws/jvm/src/main/scala/kantan/codecs/laws/discipline/SerializableTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.laws.discipline 18 | 19 | import kantan.codecs.laws.SerializableLaws 20 | import org.scalacheck.Prop 21 | import org.typelevel.discipline.Laws 22 | 23 | trait SerializableTests[A] extends Laws { 24 | def laws: SerializableLaws[A] 25 | 26 | def serializable: RuleSet = 27 | new DefaultRuleSet( 28 | "serializable", 29 | None, 30 | "serialize" -> Prop(laws.serializable()) 31 | ) 32 | } 33 | 34 | object SerializableTests { 35 | def apply[A](implicit l: SerializableLaws[A]): SerializableTests[A] = 36 | new SerializableTests[A] { 37 | override def laws = 38 | l 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /scalaz/core/shared/src/test/scala/kantan/codecs/scalaz/DecodeErrorTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.scalaz 18 | 19 | import kantan.codecs.scalaz.laws.discipline.ScalazDisciplineSuite 20 | import kantan.codecs.scalaz.laws.discipline.arbitrary._ 21 | import kantan.codecs.strings.DecodeError 22 | import scalaz.Show 23 | import scalaz.scalacheck.ScalazProperties.{equal => equ} 24 | 25 | class DecodeErrorTests extends ScalazDisciplineSuite { 26 | 27 | checkAll("DecodeError", equ.laws[DecodeError]) 28 | 29 | test("Show[DecodeError] should yield a string containing the error message") { 30 | forAll { (error: DecodeError) => 31 | Show[DecodeError].shows(error) should include(error.message) 32 | } 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /scalaz/core/shared/src/test/scala/kantan/codecs/scalaz/DecoderTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.scalaz 18 | 19 | import kantan.codecs.scalaz.laws.discipline.ScalazDisciplineSuite 20 | import kantan.codecs.scalaz.laws.discipline.arbitrary._ 21 | import kantan.codecs.scalaz.laws.discipline.equality._ 22 | import kantan.codecs.strings.DecodeError 23 | import kantan.codecs.strings.StringDecoder 24 | import scalaz.scalacheck.ScalazProperties.monadError 25 | import scalaz.scalacheck.ScalazProperties.plus 26 | import scalaz.std.anyVal._ 27 | 28 | class DecoderTests extends ScalazDisciplineSuite { 29 | checkAll("StringDecoder", monadError.laws[StringDecoder, DecodeError]) 30 | checkAll("StringDecoder", plus.laws[StringDecoder]) 31 | } 32 | -------------------------------------------------------------------------------- /shapeless/core/jvm/src/test/scala/kantan/codecs/shapeless/SerializationTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.shapeless 18 | 19 | import kantan.codecs.shapeless.Instances._ 20 | import kantan.codecs.shapeless.laws.Or 21 | import kantan.codecs.shapeless.laws.discipline.DisciplineSuite 22 | import kantan.codecs.shapeless.laws.discipline.SerializableTests 23 | import kantan.codecs.strings.StringDecoder 24 | import kantan.codecs.strings.StringEncoder 25 | 26 | class SerializationTests extends DisciplineSuite { 27 | checkAll("StringDecoder[Int Or Boolean]", SerializableTests[StringDecoder[Int Or Boolean]].serializable) 28 | checkAll("StringEncoder[Int Or Boolean]", SerializableTests[StringEncoder[Int Or Boolean]].serializable) 29 | 30 | } 31 | -------------------------------------------------------------------------------- /refined/core/jvm/src/test/scala/kantan/codecs/refined/SerializationTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.refined 18 | 19 | import eu.timepit.refined.api.Refined 20 | import eu.timepit.refined.numeric.Positive 21 | import kantan.codecs.laws.discipline.DisciplineSuite 22 | import kantan.codecs.laws.discipline.SerializableTests 23 | import kantan.codecs.strings.StringDecoder 24 | import kantan.codecs.strings.StringEncoder 25 | 26 | class SerializationTests extends DisciplineSuite { 27 | 28 | checkAll("StringDecoder[Int Refined Positive]", SerializableTests[StringDecoder[Int Refined Positive]].serializable) 29 | checkAll("StringEncoder[Int Refined Positive]", SerializableTests[StringEncoder[Int Refined Positive]].serializable) 30 | 31 | } 32 | -------------------------------------------------------------------------------- /java8/core/src/test/scala/kantan/codecs/strings/java8/codec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings.java8 18 | 19 | import kantan.codecs.laws.CodecValue.{IllegalValue, LegalValue} 20 | import org.scalacheck.Arbitrary 21 | 22 | object codec { 23 | 24 | implicit def arbLegalValue[D](implicit 25 | arb: Arbitrary[LegalValue[String, D, kantan.codecs.strings.codecs.type]] 26 | ): Arbitrary[LegalValue[String, D, codec.type]] = 27 | Arbitrary(arb.arbitrary.map(_.tag[codec.type])) 28 | 29 | implicit def arbIllegalValue[D](implicit 30 | arb: Arbitrary[IllegalValue[String, D, kantan.codecs.strings.codecs.type]] 31 | ): Arbitrary[IllegalValue[String, D, codec.type]] = 32 | Arbitrary(arb.arbitrary.map(_.tag[codec.type])) 33 | 34 | } 35 | -------------------------------------------------------------------------------- /core/shared/src/main/scala/kantan/codecs/resource/Closeable.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.resource 18 | 19 | /** Type class for all types that can be closed. */ 20 | trait Closeable[A] { 21 | 22 | /** Closes the specified value. */ 23 | def close(a: A): CloseResult 24 | } 25 | 26 | object Closeable { 27 | def apply[A](implicit ev: Closeable[A]): Closeable[A] = 28 | ev 29 | 30 | def from[A](f: A => CloseResult): Closeable[A] = 31 | new Closeable[A] { 32 | override def close(a: A) = 33 | f(a) 34 | } 35 | 36 | /** Instance for any type that extends `AutoCloseable`. */ 37 | implicit def autoCloseable[A <: AutoCloseable]: Closeable[A] = 38 | Closeable.from { c => 39 | CloseResult(c.close()) 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /laws/shared/src/main/scala/kantan/codecs/laws/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs 18 | 19 | import kantan.codecs.laws.CodecValue.IllegalValue 20 | import kantan.codecs.laws.CodecValue.LegalValue 21 | import kantan.codecs.strings.DecodeError 22 | import kantan.codecs.strings.codecs 23 | 24 | package object laws { 25 | 26 | type StringValue[A] = CodecValue[String, A, codecs.type] 27 | type LegalString[A] = LegalValue[String, A, codecs.type] 28 | type IllegalString[A] = IllegalValue[String, A, codecs.type] 29 | 30 | type StringEncoderLaws[A] = EncoderLaws[String, A, codecs.type] 31 | type StringDecoderLaws[A] = DecoderLaws[String, A, DecodeError, codecs.type] 32 | type StringCodecLaws[A] = CodecLaws[String, A, DecodeError, codecs.type] 33 | 34 | } 35 | -------------------------------------------------------------------------------- /scalaz/core/shared/src/test/scala/kantan/codecs/scalaz/MaybeCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.scalaz 18 | 19 | import kantan.codecs.laws.discipline.DisciplineSuite 20 | import kantan.codecs.laws.discipline.StringCodecTests 21 | import kantan.codecs.laws.discipline.StringDecoderTests 22 | import kantan.codecs.laws.discipline.StringEncoderTests 23 | import kantan.codecs.scalaz.laws.discipline.arbitrary._ 24 | import scalaz.Maybe 25 | 26 | class MaybeCodecTests extends DisciplineSuite { 27 | 28 | checkAll("StringDecoder[Maybe[Int]]", StringDecoderTests[Maybe[Int]].decoder[Int, Int]) 29 | checkAll("StringEncoder[Maybe[Int]]", StringEncoderTests[Maybe[Int]].encoder[Int, Int]) 30 | checkAll("StringCodec[Maybe[Int]]", StringCodecTests[Maybe[Int]].codec[Int, Int]) 31 | 32 | } 33 | -------------------------------------------------------------------------------- /core/shared/src/main/scala-2.12/kantan/codecs/VersionSpecificResultCompanion.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs 18 | 19 | import scala.collection.generic.CanBuildFrom 20 | import scala.collection.mutable 21 | 22 | object VersionSpecificResultCompanion { 23 | trait Simple[F] { 24 | 25 | /** Turns a collection of results into a result of a collection. */ 26 | @inline def sequence[S, M[X] <: TraversableOnce[X]](rs: M[Either[F, S]])(implicit 27 | cbf: CanBuildFrom[M[Either[F, S]], S, M[S]] 28 | ): Either[F, M[S]] = 29 | rs.foldLeft(Right(cbf(rs)): Either[F, mutable.Builder[S, M[S]]]) { (builder, res) => 30 | for { 31 | b <- builder.right 32 | r <- res.right 33 | } yield b += r 34 | }.right 35 | .map(_.result()) 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /core/shared/src/main/scala-2.13/kantan/codecs/VersionSpecificResultCompanion.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs 18 | 19 | import scala.collection.BuildFrom 20 | import scala.collection.mutable 21 | 22 | object VersionSpecificResultCompanion { 23 | trait Simple[F] { 24 | 25 | /** Turns a collection of results into a result of a collection. */ 26 | @inline def sequence[S, M[X] <: IterableOnce[X]](rs: M[Either[F, S]])(implicit 27 | bf: BuildFrom[M[Either[F, S]], S, M[S]] 28 | ): Either[F, M[S]] = 29 | rs.iterator 30 | .foldLeft(Right(bf.newBuilder(rs)): Either[F, mutable.Builder[S, M[S]]]) { (builder, res) => 31 | for { 32 | b <- builder 33 | r <- res 34 | } yield b += r 35 | } 36 | .map(_.result()) 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /laws/shared/src/main/scala/kantan/codecs/laws/discipline/OptionalTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.laws.discipline 18 | 19 | import kantan.codecs.laws.OptionalLaws 20 | import org.scalacheck.Arbitrary 21 | import org.scalacheck.Prop.forAll 22 | import org.typelevel.discipline.Laws 23 | 24 | trait OptionalTests[A] extends Laws { 25 | def laws: OptionalLaws[A] 26 | 27 | implicit def arbA: Arbitrary[A] 28 | 29 | def optional: RuleSet = 30 | new SimpleRuleSet("optional", "empty uniqueness" -> forAll(laws.emptyUniqueness _)) 31 | } 32 | 33 | object OptionalTests { 34 | def apply[A: Arbitrary: OptionalLaws]: OptionalTests[A] = 35 | new OptionalTests[A] { 36 | override val laws = implicitly[OptionalLaws[A]] 37 | override val arbA = implicitly[Arbitrary[A]] 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /scalaz/laws/shared/src/main/scala/kantan/codecs/scalaz/laws/discipline/ScalazDisciplineSuite.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.scalaz.laws.discipline 18 | 19 | import org.scalacheck.Properties 20 | import org.scalatestplus.scalacheck.Checkers 21 | 22 | class ScalazDisciplineSuite extends DisciplineSuite { 23 | // The i bit is a dirty hack to work around the fact that some scalaz properties have duplicated identifiers, which 24 | // causes scalatest to refuse to even consider working. 25 | @SuppressWarnings(Array("org.wartremover.warts.Var")) 26 | def checkAll(name: String, props: Properties): Unit = { 27 | var i = 0 28 | for((id, prop) <- props.properties) { 29 | i = i + 1 30 | test(s"$name[${i.toString}].$id") { 31 | Checkers.check(prop) 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /scalaz/core/shared/src/test/scala/kantan/codecs/scalaz/DisjunctionCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.scalaz 18 | 19 | import kantan.codecs.laws.discipline.DisciplineSuite 20 | import kantan.codecs.laws.discipline.StringCodecTests 21 | import kantan.codecs.laws.discipline.StringDecoderTests 22 | import kantan.codecs.laws.discipline.StringEncoderTests 23 | import kantan.codecs.scalaz.laws.discipline.arbitrary._ 24 | import scalaz.\/ 25 | 26 | class DisjunctionCodecTests extends DisciplineSuite { 27 | 28 | checkAll("StringDecoder[Int \\/ Boolean]", StringDecoderTests[Int \/ Boolean].decoder[Int, Int]) 29 | checkAll("StringEncoder[Int \\/ Boolean]", StringEncoderTests[Int \/ Boolean].encoder[Int, Int]) 30 | checkAll("StringCodec[Int \\/ Boolean]", StringCodecTests[Int \/ Boolean].codec[Int, Int]) 31 | 32 | } 33 | -------------------------------------------------------------------------------- /enumeratum/core/shared/src/test/scala/kantan/codecs/enumeratum/EnumCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.enumeratum 18 | 19 | import kantan.codecs.enumeratum.laws.discipline.Enumerated 20 | import kantan.codecs.enumeratum.laws.discipline.arbitrary._ 21 | import kantan.codecs.laws.discipline.DisciplineSuite 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | 26 | class EnumCodecTests extends DisciplineSuite { 27 | 28 | checkAll("StringDecoder[Enumerated]", StringDecoderTests[Enumerated].decoder[Int, Int]) 29 | checkAll("StringEncoder[Enumerated]", StringEncoderTests[Enumerated].encoder[Int, Int]) 30 | checkAll("StringCodec[Enumerated]", StringCodecTests[Enumerated].codec[Int, Int]) 31 | 32 | } 33 | -------------------------------------------------------------------------------- /shapeless/core/shared/src/test/scala/kantan/codecs/shapeless/Instances.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.shapeless 18 | 19 | import kantan.codecs.strings.StringDecoder 20 | import kantan.codecs.strings.StringEncoder 21 | import shapeless.:: 22 | import shapeless.HNil 23 | 24 | object Instances { 25 | 26 | // - HList / Coproduct instances ------------------------------------------------------------------------------------- 27 | // ------------------------------------------------------------------------------------------------------------------- 28 | 29 | implicit def hlistEncoder[A: StringEncoder]: StringEncoder[A :: HNil] = 30 | StringEncoder.from { case h :: _ => 31 | StringEncoder[A].encode(h) 32 | } 33 | 34 | implicit def hlistDecoder[A: StringDecoder]: StringDecoder[A :: HNil] = 35 | StringDecoder[A].map(h => h :: HNil) 36 | } 37 | -------------------------------------------------------------------------------- /enumeratum/core/shared/src/test/scala/kantan/codecs/enumeratum/values/IntEnumCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.enumeratum.values 18 | 19 | import kantan.codecs.enumeratum.laws.discipline.EnumeratedInt 20 | import kantan.codecs.enumeratum.laws.discipline.arbitrary._ 21 | import kantan.codecs.laws.discipline.DisciplineSuite 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | 26 | class IntEnumCodecTests extends DisciplineSuite { 27 | 28 | checkAll("StringDecoder[EnumeratedInt]", StringDecoderTests[EnumeratedInt].decoder[Int, Int]) 29 | checkAll("StringEncoder[EnumeratedInt]", StringEncoderTests[EnumeratedInt].encoder[Int, Int]) 30 | checkAll("StringCodec[EnumeratedInt]", StringCodecTests[EnumeratedInt].codec[Int, Int]) 31 | 32 | } 33 | -------------------------------------------------------------------------------- /enumeratum/core/shared/src/test/scala/kantan/codecs/enumeratum/values/ByteEnumCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.enumeratum.values 18 | 19 | import kantan.codecs.enumeratum.laws.discipline.EnumeratedByte 20 | import kantan.codecs.enumeratum.laws.discipline.arbitrary._ 21 | import kantan.codecs.laws.discipline.DisciplineSuite 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | 26 | class ByteEnumCodecTests extends DisciplineSuite { 27 | 28 | checkAll("StringDecoder[EnumeratedByte]", StringDecoderTests[EnumeratedByte].decoder[Int, Int]) 29 | checkAll("StringEncoder[EnumeratedByte]", StringEncoderTests[EnumeratedByte].encoder[Int, Int]) 30 | checkAll("StringCodec[EnumeratedByte]", StringCodecTests[EnumeratedByte].codec[Int, Int]) 31 | 32 | } 33 | -------------------------------------------------------------------------------- /enumeratum/core/shared/src/test/scala/kantan/codecs/enumeratum/values/CharEnumCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.enumeratum.values 18 | 19 | import kantan.codecs.enumeratum.laws.discipline.EnumeratedChar 20 | import kantan.codecs.enumeratum.laws.discipline.arbitrary._ 21 | import kantan.codecs.laws.discipline.DisciplineSuite 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | 26 | class CharEnumCodecTests extends DisciplineSuite { 27 | 28 | checkAll("StringDecoder[EnumeratedChar]", StringDecoderTests[EnumeratedChar].decoder[Int, Int]) 29 | checkAll("StringEncoder[EnumeratedChar]", StringEncoderTests[EnumeratedChar].encoder[Int, Int]) 30 | checkAll("StringCodec[EnumeratedChar]", StringCodecTests[EnumeratedChar].codec[Int, Int]) 31 | 32 | } 33 | -------------------------------------------------------------------------------- /enumeratum/core/shared/src/test/scala/kantan/codecs/enumeratum/values/LongEnumCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.enumeratum.values 18 | 19 | import kantan.codecs.enumeratum.laws.discipline.EnumeratedLong 20 | import kantan.codecs.enumeratum.laws.discipline.arbitrary._ 21 | import kantan.codecs.laws.discipline.DisciplineSuite 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | 26 | class LongEnumCodecTests extends DisciplineSuite { 27 | 28 | checkAll("StringDecoder[EnumeratedLong]", StringDecoderTests[EnumeratedLong].decoder[Int, Int]) 29 | checkAll("StringEncoder[EnumeratedLong]", StringEncoderTests[EnumeratedLong].encoder[Int, Int]) 30 | checkAll("StringCodec[EnumeratedLong]", StringCodecTests[EnumeratedLong].codec[Int, Int]) 31 | 32 | } 33 | -------------------------------------------------------------------------------- /enumeratum/core/shared/src/test/scala/kantan/codecs/enumeratum/values/ShortEnumCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.enumeratum.values 18 | 19 | import kantan.codecs.enumeratum.laws.discipline.EnumeratedShort 20 | import kantan.codecs.enumeratum.laws.discipline.arbitrary._ 21 | import kantan.codecs.laws.discipline.DisciplineSuite 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | 26 | class ShortEnumCodecTests extends DisciplineSuite { 27 | 28 | checkAll("StringDecoder[EnumeratedShort]", StringDecoderTests[EnumeratedShort].decoder[Int, Int]) 29 | checkAll("StringEncoder[EnumeratedShort]", StringEncoderTests[EnumeratedShort].encoder[Int, Int]) 30 | checkAll("StringCodec[EnumeratedShort]", StringCodecTests[EnumeratedShort].codec[Int, Int]) 31 | 32 | } 33 | -------------------------------------------------------------------------------- /enumeratum/core/shared/src/test/scala/kantan/codecs/enumeratum/values/StringEnumCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.enumeratum.values 18 | 19 | import kantan.codecs.enumeratum.laws.discipline.EnumeratedString 20 | import kantan.codecs.enumeratum.laws.discipline.arbitrary._ 21 | import kantan.codecs.laws.discipline.DisciplineSuite 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | 26 | class StringEnumCodecTests extends DisciplineSuite { 27 | 28 | checkAll("StringDecoder[EnumeratedString]", StringDecoderTests[EnumeratedString].decoder[Int, Int]) 29 | checkAll("StringEncoder[EnumeratedString]", StringEncoderTests[EnumeratedString].encoder[Int, Int]) 30 | checkAll("StringCodec[EnumeratedString]", StringCodecTests[EnumeratedString].codec[Int, Int]) 31 | 32 | } 33 | -------------------------------------------------------------------------------- /refined/core/shared/src/test/scala/kantan/codecs/refined/RefinedCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.refined 18 | 19 | import eu.timepit.refined.api.Refined 20 | import eu.timepit.refined.numeric.Positive 21 | import kantan.codecs.laws.discipline.DisciplineSuite 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | import kantan.codecs.refined.laws.discipline.arbitrary._ 26 | 27 | class RefinedCodecTests extends DisciplineSuite { 28 | 29 | checkAll("StringDecoder[Int Refined Positive]", StringDecoderTests[Int Refined Positive].decoder[Int, Int]) 30 | checkAll("StringEncoder[Int Refined Positive]", StringEncoderTests[Int Refined Positive].encoder[Int, Int]) 31 | checkAll("StringCodec[Int Refined Positive]", StringCodecTests[Int Refined Positive].codec[Int, Int]) 32 | 33 | } 34 | -------------------------------------------------------------------------------- /core/jvm/src/main/scala/kantan/codecs/resource/bom/ByteOrderMark.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.resource.bom 18 | 19 | import java.nio.charset.Charset 20 | 21 | private[bom] sealed abstract class ByteOrderMark(val charset: Charset) { 22 | val bytes: Array[Byte] = "\uFEFF".getBytes(charset) 23 | } 24 | 25 | private[bom] object ByteOrderMark { 26 | object Utf8 extends ByteOrderMark(Charset.forName("UTF-8")) 27 | object Utf16BE extends ByteOrderMark(Charset.forName("UTF-16BE")) 28 | object Utf16LE extends ByteOrderMark(Charset.forName("UTF-16LE")) 29 | object Utf32BE extends ByteOrderMark(Charset.forName("UTF-32BE")) 30 | object Utf32LE extends ByteOrderMark(Charset.forName("UTF-32LE")) 31 | 32 | def findFor(charset: Charset): Option[ByteOrderMark] = 33 | values.find(_.charset.name() == charset.name()) 34 | 35 | val values: List[ByteOrderMark] = List(Utf8, Utf16BE, Utf16LE, Utf32BE, Utf32LE) 36 | } 37 | -------------------------------------------------------------------------------- /core/jvm/src/main/scala/kantan/codecs/resource/bom/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.resource 18 | 19 | import scala.io.Codec 20 | 21 | /** Adds Byte Order Mark support to resources. 22 | * 23 | * By importing this package, the default behaviour of kantan libraries will be changed to: * add a BOM when writing 24 | * textual data (if one exists for the output charset). * attempt to find a BOM when reading textual data and, if one 25 | * is found, use the corresponding charset (potentially overriding whatever charset was specified "in-code"). 26 | * 27 | * Those behaviours are not enabled by default as they're very Microsoft-specific. 28 | */ 29 | package object bom { 30 | implicit def writerFromStream[A: OutputResource](implicit codec: Codec): WriterResource[A] = 31 | OutputResource[A].map(BomWriter(_, codec)) 32 | 33 | implicit def readerFromStream[A: InputResource](implicit codec: Codec): ReaderResource[A] = 34 | InputResource[A].map(BomReader(_, codec)) 35 | } 36 | -------------------------------------------------------------------------------- /core/shared/src/test/scala/kantan/codecs/CodecCompanionTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs 18 | 19 | import org.scalatest.funsuite.AnyFunSuite 20 | import org.scalatest.matchers.should.Matchers 21 | import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks 22 | 23 | class CodecCompanionTests extends AnyFunSuite with ScalaCheckPropertyChecks with Matchers { 24 | object codec 25 | object Companion extends CodecCompanion[String, String, codec.type] 26 | 27 | test("CodecCompanion.from should be equivalent to Codec.from (for encoding)") { 28 | forAll { (f: String => Either[String, Int], g: Int => String, i: Int) => 29 | Codec.from(f)(g).encode(i) should be(Companion.from(f)(g).encode(i)) 30 | } 31 | } 32 | 33 | test("CodecCompanion.from should be equivalent to Codec.from (for decoding)") { 34 | forAll { (f: String => Either[String, Int], g: Int => String, str: String) => 35 | Codec.from(f)(g).decode(str) should be(Companion.from(f)(g).decode(str)) 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /core/shared/src/test/scala/kantan/codecs/strings/IntCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.DecoderTests 20 | import kantan.codecs.laws.discipline.DisciplineSuite 21 | import kantan.codecs.laws.discipline.EncoderTests 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | import kantan.codecs.laws.discipline.arbitrary._ 26 | 27 | class IntCodecTests extends DisciplineSuite { 28 | 29 | checkAll("StringDecoder[Int]", StringDecoderTests[Int].decoder[Int, Int]) 30 | checkAll("StringEncoder[Int]", StringEncoderTests[Int].encoder[Int, Int]) 31 | checkAll("StringCodec[Int]", StringCodecTests[Int].codec[Int, Int]) 32 | 33 | checkAll("TaggedDecoder[Int]", DecoderTests[String, Int, DecodeError, tagged.type].decoder[Int, Int]) 34 | checkAll("TaggedEncoder[Int]", EncoderTests[String, Int, tagged.type].encoder[Int, Int]) 35 | 36 | } 37 | -------------------------------------------------------------------------------- /core/shared/src/test/scala/kantan/codecs/strings/RegexCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.DisciplineSuite 20 | 21 | // TODO: this is currently disabled because of Java's messed up Pattern.equals implementation. 22 | class RegexCodecTests extends DisciplineSuite /*{ 23 | checkAll("StringDecoder[Regex]", StringDecoderTests[Regex].bijectiveDecoder[Int, Int]) 24 | checkAll("StringDecoder[Regex]", SerializableTests[StringDecoder[Regex]].serializable) 25 | 26 | checkAll("StringEncoder[Regex]", StringEncoderTests[Regex].encoder[Int, Int]) 27 | checkAll("StringEncoder[Regex]", SerializableTests[StringEncoder[Regex]].serializable) 28 | 29 | checkAll("StringCodec[Regex]", StringCodecTests[Regex].bijectiveCodec[Int, Int]) 30 | 31 | checkAll("TaggedDecoder[Regex]", DecoderTests[String, Regex, DecodeError, tagged.type].bijectiveDecoder[Int, Int]) 32 | checkAll("TaggedEncoder[Regex]", EncoderTests[String, Regex, tagged.type].encoder[Int, Int]) 33 | 34 | } 35 | */ 36 | -------------------------------------------------------------------------------- /core/shared/src/test/scala/kantan/codecs/strings/ByteCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.DecoderTests 20 | import kantan.codecs.laws.discipline.DisciplineSuite 21 | import kantan.codecs.laws.discipline.EncoderTests 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | import kantan.codecs.laws.discipline.arbitrary._ 26 | 27 | class ByteCodecTests extends DisciplineSuite { 28 | 29 | checkAll("StringDecoder[Byte]", StringDecoderTests[Byte].decoder[Int, Int]) 30 | checkAll("StringEncoder[Byte]", StringEncoderTests[Byte].encoder[Int, Int]) 31 | checkAll("StringCodec[Byte]", StringCodecTests[Byte].codec[Int, Int]) 32 | 33 | checkAll("TaggedDecoder[Byte]", DecoderTests[String, Byte, DecodeError, tagged.type].decoder[Int, Int]) 34 | checkAll("TaggedEncoder[Byte]", EncoderTests[String, Byte, tagged.type].encoder[Int, Int]) 35 | 36 | } 37 | -------------------------------------------------------------------------------- /core/shared/src/test/scala/kantan/codecs/strings/CharCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.DecoderTests 20 | import kantan.codecs.laws.discipline.DisciplineSuite 21 | import kantan.codecs.laws.discipline.EncoderTests 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | import kantan.codecs.laws.discipline.arbitrary._ 26 | 27 | class CharCodecTests extends DisciplineSuite { 28 | 29 | checkAll("StringDecoder[Char]", StringDecoderTests[Char].decoder[Int, Int]) 30 | checkAll("StringEncoder[Char]", StringEncoderTests[Char].encoder[Int, Int]) 31 | checkAll("StringCodec[Char]", StringCodecTests[Char].codec[Int, Int]) 32 | 33 | checkAll("TaggedDecoder[Char]", DecoderTests[String, Char, DecodeError, tagged.type].decoder[Int, Int]) 34 | checkAll("TaggedEncoder[Char]", EncoderTests[String, Char, tagged.type].encoder[Int, Int]) 35 | 36 | } 37 | -------------------------------------------------------------------------------- /core/shared/src/test/scala/kantan/codecs/strings/LongCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.DecoderTests 20 | import kantan.codecs.laws.discipline.DisciplineSuite 21 | import kantan.codecs.laws.discipline.EncoderTests 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | import kantan.codecs.laws.discipline.arbitrary._ 26 | 27 | class LongCodecTests extends DisciplineSuite { 28 | 29 | checkAll("StringDecoder[Long]", StringDecoderTests[Long].decoder[Int, Int]) 30 | checkAll("StringEncoder[Long]", StringEncoderTests[Long].encoder[Int, Int]) 31 | checkAll("StringCodec[Long]", StringCodecTests[Long].codec[Int, Int]) 32 | 33 | checkAll("TaggedDecoder[Long]", DecoderTests[String, Long, DecodeError, tagged.type].decoder[Int, Int]) 34 | checkAll("TaggedEncoder[Long]", EncoderTests[String, Long, tagged.type].encoder[Int, Int]) 35 | 36 | } 37 | -------------------------------------------------------------------------------- /core/shared/src/test/scala/kantan/codecs/strings/ErrorsTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.arbitrary._ 20 | import org.scalatest.funsuite.AnyFunSuite 21 | import org.scalatest.matchers.should.Matchers 22 | import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks 23 | 24 | class ErrorsTests extends AnyFunSuite with ScalaCheckPropertyChecks with Matchers { 25 | test("DecodeErrors should be equal if the underlying exceptions are the same") { 26 | forAll { (e1: DecodeError, e2: Exception) => 27 | (e1, e2) match { 28 | case (DecodeError(t1), DecodeError(t2)) => (e1 == e2) should be(t1 == t2) 29 | case _ => e1 should not be e2 30 | } 31 | } 32 | } 33 | 34 | test("DecodeErrors should have identical hashCodes if the underlying exceptions are the same") { 35 | forAll { (e1: DecodeError, e2: DecodeError) => 36 | (e1.hashCode() == e2.hashCode()) should be(e1.message == e2.getMessage) 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /core/shared/src/test/scala/kantan/codecs/strings/FloatCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.DecoderTests 20 | import kantan.codecs.laws.discipline.DisciplineSuite 21 | import kantan.codecs.laws.discipline.EncoderTests 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | import kantan.codecs.laws.discipline.arbitrary._ 26 | 27 | class FloatCodecTests extends DisciplineSuite { 28 | 29 | checkAll("StringDecoder[Float]", StringDecoderTests[Float].decoder[Int, Int]) 30 | checkAll("StringEncoder[Float]", StringEncoderTests[Float].encoder[Int, Int]) 31 | checkAll("StringCodec[Float]", StringCodecTests[Float].codec[Int, Int]) 32 | 33 | checkAll("TaggedDecoder[Float]", DecoderTests[String, Float, DecodeError, tagged.type].decoder[Int, Int]) 34 | checkAll("TaggedEncoder[Float]", EncoderTests[String, Float, tagged.type].encoder[Int, Int]) 35 | 36 | } 37 | -------------------------------------------------------------------------------- /core/shared/src/test/scala/kantan/codecs/strings/ShortCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.DecoderTests 20 | import kantan.codecs.laws.discipline.DisciplineSuite 21 | import kantan.codecs.laws.discipline.EncoderTests 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | import kantan.codecs.laws.discipline.arbitrary._ 26 | 27 | class ShortCodecTests extends DisciplineSuite { 28 | 29 | checkAll("StringDecoder[Short]", StringDecoderTests[Short].decoder[Int, Int]) 30 | checkAll("StringEncoder[Short]", StringEncoderTests[Short].encoder[Int, Int]) 31 | checkAll("StringCodec[Short]", StringCodecTests[Short].codec[Int, Int]) 32 | 33 | checkAll("TaggedDecoder[Short]", DecoderTests[String, Short, DecodeError, tagged.type].decoder[Int, Int]) 34 | checkAll("TaggedEncoder[Short]", EncoderTests[String, Short, tagged.type].encoder[Int, Int]) 35 | 36 | } 37 | -------------------------------------------------------------------------------- /core/jvm/src/test/scala/kantan/codecs/strings/URICodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.DecoderTests 20 | import kantan.codecs.laws.discipline.DisciplineSuite 21 | import kantan.codecs.laws.discipline.EncoderTests 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | import kantan.codecs.laws.discipline.arbitrary._ 26 | 27 | import java.net.URI 28 | 29 | class URICodecTests extends DisciplineSuite { 30 | 31 | checkAll("StringDecoder[URI]", StringDecoderTests[URI].decoder[Int, Int]) 32 | checkAll("StringEncoder[URI]", StringEncoderTests[URI].encoder[Int, Int]) 33 | checkAll("StringCodec[URI]", StringCodecTests[URI].codec[Int, Int]) 34 | 35 | checkAll("TaggedDecoder[URI]", DecoderTests[String, URI, DecodeError, tagged.type].decoder[Int, Int]) 36 | checkAll("TaggedEncoder[URI]", EncoderTests[String, URI, tagged.type].encoder[Int, Int]) 37 | 38 | } 39 | -------------------------------------------------------------------------------- /core/jvm/src/test/scala/kantan/codecs/strings/URLCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.DecoderTests 20 | import kantan.codecs.laws.discipline.DisciplineSuite 21 | import kantan.codecs.laws.discipline.EncoderTests 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | import kantan.codecs.laws.discipline.arbitrary._ 26 | 27 | import java.net.URL 28 | 29 | class URLCodecTests extends DisciplineSuite { 30 | 31 | checkAll("StringDecoder[URL]", StringDecoderTests[URL].decoder[Int, Int]) 32 | checkAll("StringEncoder[URL]", StringEncoderTests[URL].encoder[Int, Int]) 33 | checkAll("StringCodec[URL]", StringCodecTests[URL].codec[Int, Int]) 34 | 35 | checkAll("TaggedDecoder[URL]", DecoderTests[String, URL, DecodeError, tagged.type].decoder[Int, Int]) 36 | checkAll("TaggedEncoder[URL]", EncoderTests[String, URL, tagged.type].encoder[Int, Int]) 37 | 38 | } 39 | -------------------------------------------------------------------------------- /core/shared/src/test/scala/kantan/codecs/strings/BigIntCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.DecoderTests 20 | import kantan.codecs.laws.discipline.DisciplineSuite 21 | import kantan.codecs.laws.discipline.EncoderTests 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | import kantan.codecs.laws.discipline.arbitrary._ 26 | 27 | class BigIntCodecTests extends DisciplineSuite { 28 | 29 | checkAll("StringDecoder[BigInt]", StringDecoderTests[BigInt].decoder[Int, Int]) 30 | checkAll("StringEncoder[BigInt]", StringEncoderTests[BigInt].encoder[Int, Int]) 31 | checkAll("StringCodec[BigInt]", StringCodecTests[BigInt].codec[Int, Int]) 32 | 33 | checkAll("TaggedDecoder[BigInt]", DecoderTests[String, BigInt, DecodeError, tagged.type].decoder[Int, Int]) 34 | checkAll("TaggedEncoder[BigInt]", EncoderTests[String, BigInt, tagged.type].encoder[Int, Int]) 35 | 36 | } 37 | -------------------------------------------------------------------------------- /core/shared/src/test/scala/kantan/codecs/strings/DoubleCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.DecoderTests 20 | import kantan.codecs.laws.discipline.DisciplineSuite 21 | import kantan.codecs.laws.discipline.EncoderTests 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | import kantan.codecs.laws.discipline.arbitrary._ 26 | 27 | class DoubleCodecTests extends DisciplineSuite { 28 | 29 | checkAll("StringDecoder[Double]", StringDecoderTests[Double].decoder[Int, Int]) 30 | checkAll("StringEncoder[Double]", StringEncoderTests[Double].encoder[Int, Int]) 31 | checkAll("StringCodec[Double]", StringCodecTests[Double].codec[Int, Int]) 32 | 33 | checkAll("TaggedDecoder[Double]", DecoderTests[String, Double, DecodeError, tagged.type].decoder[Int, Int]) 34 | checkAll("TaggedEncoder[Double]", EncoderTests[String, Double, tagged.type].encoder[Int, Int]) 35 | 36 | } 37 | -------------------------------------------------------------------------------- /core/shared/src/test/scala/kantan/codecs/strings/UUIDCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.DecoderTests 20 | import kantan.codecs.laws.discipline.DisciplineSuite 21 | import kantan.codecs.laws.discipline.EncoderTests 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | import kantan.codecs.laws.discipline.arbitrary._ 26 | 27 | import java.util.UUID 28 | 29 | class UUIDCodecTests extends DisciplineSuite { 30 | 31 | checkAll("StringDecoder[UUID]", StringDecoderTests[UUID].decoder[Int, Int]) 32 | checkAll("StringEncoder[UUID]", StringEncoderTests[UUID].encoder[Int, Int]) 33 | checkAll("StringCodec[UUID]", StringCodecTests[UUID].codec[Int, Int]) 34 | 35 | checkAll("TaggedDecoder[UUID]", DecoderTests[String, UUID, DecodeError, tagged.type].decoder[Int, Int]) 36 | checkAll("TaggedEncoder[UUID]", EncoderTests[String, UUID, tagged.type].encoder[Int, Int]) 37 | 38 | } 39 | -------------------------------------------------------------------------------- /core/shared/src/main/scala-2.13/kantan/codecs/resource/VersionSpecificResourceIterator.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.resource 18 | 19 | import scala.collection.Factory 20 | import scala.collection.mutable.Buffer 21 | 22 | @SuppressWarnings(Array("org.wartremover.warts.MutableDataStructures")) 23 | trait VersionSpecificResourceIterator[+A] { self: ResourceIterator[A] => 24 | def to[F](factory: Factory[A, F]): F = 25 | foldLeft(factory.newBuilder)(_ += _).result() 26 | 27 | def toList: List[A] = 28 | to(List) 29 | def toBuffer[AA >: A]: Buffer[AA] = 30 | to(Buffer) 31 | def toIndexedSeq: IndexedSeq[A] = 32 | to(IndexedSeq) 33 | def toIterable: Iterable[A] = 34 | to(Iterable) 35 | def toSeq: Seq[A] = 36 | to(Seq) 37 | def toSet[AA >: A]: Set[AA] = 38 | to(Set) 39 | def toVector: Vector[A] = 40 | to(Vector) 41 | 42 | def iterator: Iterator[A] = 43 | new Iterator[A] { 44 | override def next(): A = 45 | self.next() 46 | override def hasNext: Boolean = 47 | self.hasNext 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /core/shared/src/test/scala/kantan/codecs/strings/BooleanCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.DecoderTests 20 | import kantan.codecs.laws.discipline.DisciplineSuite 21 | import kantan.codecs.laws.discipline.EncoderTests 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | import kantan.codecs.laws.discipline.arbitrary._ 26 | 27 | class BooleanCodecTests extends DisciplineSuite { 28 | 29 | checkAll("StringDecoder[Boolean]", StringDecoderTests[Boolean].decoder[Int, Int]) 30 | checkAll("StringEncoder[Boolean]", StringEncoderTests[Boolean].encoder[Int, Int]) 31 | checkAll("StringCodec[Boolean]", StringCodecTests[Boolean].codec[Int, Int]) 32 | 33 | checkAll("TaggedDecoder[Boolean]", DecoderTests[String, Boolean, DecodeError, tagged.type].decoder[Int, Int]) 34 | checkAll("TaggedEncoder[Boolean]", EncoderTests[String, Boolean, tagged.type].encoder[Int, Int]) 35 | 36 | } 37 | -------------------------------------------------------------------------------- /core/shared/src/test/scala/kantan/codecs/strings/PatternCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.DisciplineSuite 20 | 21 | // TODO: this is currently disabled because of Java's messed up Pattern.equals implementation. 22 | class PatternCodecTests extends DisciplineSuite /*{ 23 | 24 | checkAll("StringDecoder[Pattern]", StringDecoderTests[Pattern].bijectiveDecoder[Int, Int]) 25 | checkAll("StringDecoder[Pattern]", SerializableTests[StringDecoder[Pattern]].serializable) 26 | 27 | checkAll("StringEncoder[Pattern]", StringEncoderTests[Pattern].encoder[Int, Int]) 28 | checkAll("StringEncoder[Pattern]", SerializableTests[StringEncoder[Pattern]].serializable) 29 | 30 | checkAll("StringCodec[Pattern]", StringCodecTests[Pattern].bijectiveCodec[Int, Int]) 31 | 32 | checkAll("TaggedDecoder[Pattern]", DecoderTests[String, Pattern, DecodeError, tagged.type].bijectiveDecoder[Int, Int]) 33 | checkAll("TaggedEncoder[Pattern]", EncoderTests[String, Pattern, tagged.type].encoder[Int, Int]) 34 | 35 | } 36 | */ 37 | -------------------------------------------------------------------------------- /core/jvm/src/test/scala/kantan/codecs/strings/FileCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.DecoderTests 20 | import kantan.codecs.laws.discipline.DisciplineSuite 21 | import kantan.codecs.laws.discipline.EncoderTests 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | import kantan.codecs.laws.discipline.arbitrary._ 26 | 27 | import java.io.File 28 | 29 | class FileCodecTests extends DisciplineSuite { 30 | 31 | checkAll("StringDecoder[File]", StringDecoderTests[File].bijectiveDecoder[Int, Int]) 32 | checkAll("StringEncoder[File]", StringEncoderTests[File].encoder[Int, Int]) 33 | checkAll("StringCodec[File]", StringCodecTests[File].bijectiveCodec[Int, Int]) 34 | 35 | checkAll("TaggedDecoder[File]", DecoderTests[String, File, DecodeError, tagged.type].bijectiveDecoder[Int, Int]) 36 | checkAll("TaggedEncoder[File]", EncoderTests[String, File, tagged.type].encoder[Int, Int]) 37 | 38 | } 39 | -------------------------------------------------------------------------------- /core/jvm/src/test/scala/kantan/codecs/strings/PathCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.DecoderTests 20 | import kantan.codecs.laws.discipline.DisciplineSuite 21 | import kantan.codecs.laws.discipline.EncoderTests 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | import kantan.codecs.laws.discipline.arbitrary._ 26 | 27 | import java.nio.file.Path 28 | 29 | class PathCodecTests extends DisciplineSuite { 30 | 31 | checkAll("StringDecoder[Path]", StringDecoderTests[Path].bijectiveDecoder[Int, Int]) 32 | checkAll("StringEncoder[Path]", StringEncoderTests[Path].encoder[Int, Int]) 33 | checkAll("StringCodec[Path]", StringCodecTests[Path].bijectiveCodec[Int, Int]) 34 | 35 | checkAll("TaggedDecoder[Path]", DecoderTests[String, Path, DecodeError, tagged.type].bijectiveDecoder[Int, Int]) 36 | checkAll("TaggedEncoder[Path]", EncoderTests[String, Path, tagged.type].encoder[Int, Int]) 37 | 38 | } 39 | -------------------------------------------------------------------------------- /core/shared/src/test/scala/kantan/codecs/strings/StringCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.DecoderTests 20 | import kantan.codecs.laws.discipline.DisciplineSuite 21 | import kantan.codecs.laws.discipline.EncoderTests 22 | import kantan.codecs.laws.discipline.StringDecoderTests 23 | import kantan.codecs.laws.discipline.StringEncoderTests 24 | import kantan.codecs.laws.discipline.arbitrary._ 25 | import kantan.codecs.laws.discipline.{StringCodecTests => SCodecTests} 26 | 27 | class StringCodecTests extends DisciplineSuite { 28 | 29 | checkAll("StringDecoder[String]", StringDecoderTests[String].bijectiveDecoder[Int, Int]) 30 | checkAll("StringEncoder[String]", StringEncoderTests[String].encoder[Int, Int]) 31 | checkAll("StringCodec[String]", SCodecTests[String].bijectiveCodec[Int, Int]) 32 | 33 | checkAll("TaggedDecoder[String]", DecoderTests[String, String, DecodeError, tagged.type].bijectiveDecoder[Int, Int]) 34 | checkAll("TaggedEncoder[String]", EncoderTests[String, String, tagged.type].encoder[Int, Int]) 35 | 36 | } 37 | -------------------------------------------------------------------------------- /core/shared/src/test/scala/kantan/codecs/strings/BigDecimalCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.DecoderTests 20 | import kantan.codecs.laws.discipline.DisciplineSuite 21 | import kantan.codecs.laws.discipline.EncoderTests 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | import kantan.codecs.laws.discipline.arbitrary._ 26 | 27 | class BigDecimalCodecTests extends DisciplineSuite { 28 | 29 | checkAll("StringDecoder[BigDecimal]", StringDecoderTests[BigDecimal].decoder[Int, Int]) 30 | checkAll("StringEncoder[BigDecimal]", StringEncoderTests[BigDecimal].encoder[Int, Int]) 31 | checkAll("StringCodec[BigDecimal]", StringCodecTests[BigDecimal].codec[Int, Int]) 32 | 33 | checkAll("TaggedDecoder[BigDecimal]", DecoderTests[String, BigDecimal, DecodeError, tagged.type].decoder[Int, Int]) 34 | checkAll("TaggedEncoder[BigDecimal]", EncoderTests[String, BigDecimal, tagged.type].encoder[Int, Int]) 35 | 36 | } 37 | -------------------------------------------------------------------------------- /laws/jvm/src/main/scala/kantan/codecs/laws/SerializableLaws.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.laws 18 | 19 | import java.io.ByteArrayInputStream 20 | import java.io.ByteArrayOutputStream 21 | import java.io.ObjectInputStream 22 | import java.io.ObjectOutputStream 23 | import scala.util.Try 24 | 25 | /** Laws for serializable type class instances. */ 26 | trait SerializableLaws[A] { 27 | def value: A 28 | 29 | def serialize(o: Any): Array[Byte] = { 30 | val baos = new ByteArrayOutputStream 31 | val oos = new ObjectOutputStream(baos) 32 | 33 | oos.writeObject(o) 34 | oos.flush() 35 | baos.toByteArray 36 | } 37 | 38 | def deserialize(data: Array[Byte]): Any = { 39 | val bais = new ByteArrayInputStream(data) 40 | val ois = new ObjectInputStream(bais) 41 | ois.readObject 42 | } 43 | 44 | def serializable(): Boolean = 45 | Try(deserialize(serialize(value))).isSuccess 46 | } 47 | 48 | object SerializableLaws { 49 | implicit def apply[A](implicit a: A): SerializableLaws[A] = 50 | new SerializableLaws[A] { 51 | override def value = 52 | a 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /core/shared/src/main/scala/kantan/codecs/Optional.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs 18 | 19 | /** Type class that represents data types that have an "empty" value. 20 | * 21 | * The purpose of this type class is to allow automatic derivation of [[Decoder]] for decoded types that might not have 22 | * a value - `Option`, `List`... 23 | * 24 | * In theory, there should rarely be a need to interact directly with this type class, and one is usually better served 25 | * by obtaining the [[Decoder]] instance for `Option` and mapping on it. 26 | */ 27 | trait Optional[A] extends Serializable { 28 | def empty: A 29 | def isEmpty(a: A): Boolean = 30 | a == empty 31 | } 32 | 33 | object Optional { 34 | def apply[A](implicit ev: Optional[A]): Optional[A] = 35 | ev 36 | 37 | def apply[A](a: A): Optional[A] = 38 | new Optional[A] { 39 | override val empty = a 40 | } 41 | 42 | implicit val optString: Optional[String] = Optional("") 43 | implicit def optSeq[A]: Optional[Seq[A]] = 44 | Optional(Seq.empty[A]) 45 | implicit def optOption[A]: Optional[Option[A]] = 46 | Optional(Option.empty[A]) 47 | } 48 | -------------------------------------------------------------------------------- /java8/core/src/test/scala/kantan/codecs/strings/java8/InstantCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings.java8 18 | 19 | import kantan.codecs.laws.discipline.DisciplineSuite 20 | import kantan.codecs.laws.discipline.SerializableTests 21 | import kantan.codecs.laws.discipline.StringCodecTests 22 | import kantan.codecs.laws.discipline.StringDecoderTests 23 | import kantan.codecs.laws.discipline.StringEncoderTests 24 | import kantan.codecs.strings.StringDecoder 25 | import kantan.codecs.strings.StringEncoder 26 | import kantan.codecs.strings.java8.laws.discipline.arbitrary._ 27 | 28 | import java.time.Instant 29 | 30 | class InstantCodecTests extends DisciplineSuite { 31 | checkAll("StringDecoder[Instant]", StringDecoderTests[Instant].decoder[Int, Int]) 32 | checkAll("StringDecoder[Instant]", SerializableTests[StringDecoder[Instant]].serializable) 33 | 34 | checkAll("StringEncoder[Instant]", StringEncoderTests[Instant].encoder[Int, Int]) 35 | checkAll("StringEncoder[Instant]", SerializableTests[StringEncoder[Instant]].serializable) 36 | 37 | checkAll("StringCodec[Instant]", StringCodecTests[Instant].codec[Int, Int]) 38 | } 39 | -------------------------------------------------------------------------------- /core/shared/src/test/scala/kantan/codecs/strings/OptionCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.DecoderTests 20 | import kantan.codecs.laws.discipline.DisciplineSuite 21 | import kantan.codecs.laws.discipline.EncoderTests 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | import kantan.codecs.laws.discipline.arbitrary._ 26 | import kantan.codecs.strings.tagged._ 27 | 28 | class OptionCodecTests extends DisciplineSuite { 29 | 30 | checkAll("StringDecoder[Option[Int]]", StringDecoderTests[Option[Int]].decoder[Int, Int]) 31 | checkAll("StringEncoder[Option[Int]]", StringEncoderTests[Option[Int]].encoder[Int, Int]) 32 | checkAll("StringCodec[Option[Int]]", StringCodecTests[Option[Int]].codec[Int, Int]) 33 | 34 | checkAll("TaggedDecoder[Option[Int]]", DecoderTests[String, Option[Int], DecodeError, tagged.type].decoder[Int, Int]) 35 | checkAll("TaggedEncoder[Option[Int]]", EncoderTests[String, Option[Int], tagged.type].encoder[Int, Int]) 36 | 37 | } 38 | -------------------------------------------------------------------------------- /refined/laws/shared/src/main/scala/kantan/codecs/refined/laws/discipline/arbitrary.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.refined.laws.discipline 18 | 19 | import eu.timepit.refined.api.Refined 20 | import eu.timepit.refined.numeric.Positive 21 | import eu.timepit.refined.scalacheck._ 22 | import kantan.codecs.laws.CodecValue 23 | import kantan.codecs.laws.IllegalString 24 | import kantan.codecs.laws.LegalString 25 | import kantan.codecs.laws.discipline.arbitrary._ 26 | import org.scalacheck.Arbitrary 27 | 28 | object arbitrary extends ArbitraryInstances with kantan.codecs.laws.discipline.ArbitraryInstances 29 | 30 | trait ArbitraryInstances 31 | extends BooleanInstances with CharInstances with GenericInstances with NumericInstances with RefTypeInstances 32 | with StringInstances { 33 | 34 | implicit val arbLegalPositiveIntString: Arbitrary[LegalString[Int Refined Positive]] = 35 | arbLegalValue(_.toString) 36 | // format: off 37 | implicit val arbIllegalPositiveIntString: Arbitrary[IllegalString[Int Refined Positive]] = 38 | Arbitrary(Arbitrary.arbitrary[Int].map(i => CodecValue.IllegalValue((-math.abs(i)).toString))) 39 | // format: on 40 | } 41 | -------------------------------------------------------------------------------- /core/shared/src/main/scala/kantan/codecs/export/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs 18 | 19 | /** Provides a simple mechanism to insert derived type class instances in the implicit resolution mechanism. 20 | * 21 | * By default, when imported, such instances have the highest possible priority, which is typically problematic with 22 | * shapeless instance derivation: bespoke instances for `Option` or `Either`, for example, find themselves shadowed by 23 | * the generically derived ones. 24 | * 25 | * If such instances are of type [[kantan.codecs.export.DerivedDecoder]] / [[kantan.codecs.export.DerivedEncoder]] 26 | * rather than [[Decoder]] / [[Encoder]], however, they'll find themselves with a lower precedence and only be used if 27 | * no other, more specific instance is found. 28 | */ 29 | package object `export` { 30 | 31 | /** [[Decoder]] instance that should only be used when no specific one is found. */ 32 | type DerivedDecoder[E, D, F, T] = Exported[Decoder[E, D, F, T]] 33 | 34 | /** [[Encoder]] instance that should only be used when no specific one is found. */ 35 | type DerivedEncoder[E, D, T] = Exported[Encoder[E, D, T]] 36 | } 37 | -------------------------------------------------------------------------------- /core/jvm/src/test/scala/kantan/codecs/collection/SerializationTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.collection 18 | 19 | import kantan.codecs.laws.discipline.SerializableTests 20 | 21 | import scala.collection.immutable.Queue 22 | import scala.collection.immutable.TreeSet 23 | 24 | class SerializationTests extends VersionSpecificSerializationTests { 25 | checkAll("Factory[TreeSet]", SerializableTests[Factory[Int, TreeSet[Int]]].serializable) 26 | checkAll("Factory[QueueFactoryTests]", SerializableTests[Factory[Int, Queue[Int]]].serializable) 27 | checkAll("Factory[ListFactoryTests]", SerializableTests[Factory[Int, List[Int]]].serializable) 28 | checkAll("Factory[VectorFactoryTests]", SerializableTests[Factory[Int, Vector[Int]]].serializable) 29 | checkAll("Factory[IndexedSeqFactoryTests]", SerializableTests[Factory[Int, IndexedSeq[Int]]].serializable) 30 | checkAll("Factory[SeqFactoryTests]", SerializableTests[Factory[Int, Seq[Int]]].serializable) 31 | checkAll("Factory[SetFactoryTests]", SerializableTests[Factory[Int, Set[Int]]].serializable) 32 | checkAll("Factory[ArrayFactoryTests]", SerializableTests[Factory[Int, Array[Int]]].serializable) 33 | 34 | } 35 | -------------------------------------------------------------------------------- /java8/core/src/test/scala/kantan/codecs/strings/java8/LocalDateCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings.java8 18 | 19 | import kantan.codecs.laws.discipline.DisciplineSuite 20 | import kantan.codecs.laws.discipline.SerializableTests 21 | import kantan.codecs.laws.discipline.StringCodecTests 22 | import kantan.codecs.laws.discipline.StringDecoderTests 23 | import kantan.codecs.laws.discipline.StringEncoderTests 24 | import kantan.codecs.strings.StringDecoder 25 | import kantan.codecs.strings.StringEncoder 26 | import kantan.codecs.strings.java8.laws.discipline.arbitrary._ 27 | 28 | import java.time.LocalDate 29 | 30 | class LocalDateCodecTests extends DisciplineSuite { 31 | 32 | checkAll("StringDecoder[LocalDate]", StringDecoderTests[LocalDate].decoder[Int, Int]) 33 | checkAll("StringDecoder[LocalDate]", SerializableTests[StringDecoder[LocalDate]].serializable) 34 | 35 | checkAll("StringEncoder[LocalDate]", StringEncoderTests[LocalDate].encoder[Int, Int]) 36 | checkAll("StringEncoder[LocalDate]", SerializableTests[StringEncoder[LocalDate]].serializable) 37 | 38 | checkAll("StringCodec[LocalDate]", StringCodecTests[LocalDate].codec[Int, Int]) 39 | 40 | } 41 | -------------------------------------------------------------------------------- /java8/core/src/test/scala/kantan/codecs/strings/java8/LocalTimeCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings.java8 18 | 19 | import kantan.codecs.laws.discipline.DisciplineSuite 20 | import kantan.codecs.laws.discipline.SerializableTests 21 | import kantan.codecs.laws.discipline.StringCodecTests 22 | import kantan.codecs.laws.discipline.StringDecoderTests 23 | import kantan.codecs.laws.discipline.StringEncoderTests 24 | import kantan.codecs.strings.StringDecoder 25 | import kantan.codecs.strings.StringEncoder 26 | import kantan.codecs.strings.java8.laws.discipline.arbitrary._ 27 | 28 | import java.time.LocalTime 29 | 30 | class LocalTimeCodecTests extends DisciplineSuite { 31 | 32 | checkAll("StringDecoder[LocalTime]", StringDecoderTests[LocalTime].decoder[Int, Int]) 33 | checkAll("StringDecoder[LocalTime]", SerializableTests[StringDecoder[LocalTime]].serializable) 34 | 35 | checkAll("StringEncoder[LocalTime]", StringEncoderTests[LocalTime].encoder[Int, Int]) 36 | checkAll("StringEncoder[LocalTime]", SerializableTests[StringEncoder[LocalTime]].serializable) 37 | 38 | checkAll("StringCodec[LocalTime]", StringCodecTests[LocalTime].codec[Int, Int]) 39 | 40 | } 41 | -------------------------------------------------------------------------------- /core/jvm/src/test/scala/kantan/codecs/strings/AccessModeCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.DecoderTests 20 | import kantan.codecs.laws.discipline.DisciplineSuite 21 | import kantan.codecs.laws.discipline.EncoderTests 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | import kantan.codecs.laws.discipline.arbitrary._ 26 | 27 | import java.nio.file.AccessMode 28 | 29 | class AccessModeCodecTests extends DisciplineSuite { 30 | 31 | checkAll("StringDecoder[AccessMode]", StringDecoderTests[AccessMode].bijectiveDecoder[Int, Int]) 32 | checkAll("StringEncoder[AccessMode]", StringEncoderTests[AccessMode].encoder[Int, Int]) 33 | checkAll("StringCodec[AccessMode]", StringCodecTests[AccessMode].bijectiveCodec[Int, Int]) 34 | 35 | checkAll( 36 | "TaggedDecoder[AccessMode]", 37 | DecoderTests[String, AccessMode, DecodeError, tagged.type].bijectiveDecoder[Int, Int] 38 | ) 39 | checkAll("TaggedEncoder[AccessMode]", EncoderTests[String, AccessMode, tagged.type].encoder[Int, Int]) 40 | 41 | } 42 | -------------------------------------------------------------------------------- /core/shared/src/main/scala/kantan/codecs/strings/package.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs 18 | 19 | /** Defines codecs for encoding to and decoding from strings. 20 | * 21 | * These codecs are not necessary meant to use directly, but more as part of larger ones. 22 | * [[https://github.com/nrinaudo/kantan.csv kantan.csv]], for example, works with CSV files but delegates the act of 23 | * encoding to or decoding from a CSV cell to string codecs. 24 | * 25 | * Default instances can be found in [[kantan.codecs.strings.codecs]]. 26 | */ 27 | package object strings { 28 | 29 | /** Type of values values returned when attempting to decode a `String`. */ 30 | type StringResult[A] = Either[DecodeError, A] 31 | 32 | /** [[Encoder]] for strings. 33 | * 34 | * Default instances can be found in [[codecs]]. 35 | */ 36 | type StringEncoder[A] = Encoder[String, A, codecs.type] 37 | 38 | /** [[Decoder]] for strings. 39 | * 40 | * Default instances can be found in [[codecs]]. 41 | */ 42 | type StringDecoder[A] = Decoder[String, A, DecodeError, codecs.type] 43 | 44 | /** [[Codec]] for strings. */ 45 | type StringCodec[A] = Codec[String, A, DecodeError, codecs.type] 46 | } 47 | -------------------------------------------------------------------------------- /java8/core/src/test/scala/kantan/codecs/strings/java8/LocalDateTimeCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings.java8 18 | 19 | import kantan.codecs.laws.discipline.DisciplineSuite 20 | import kantan.codecs.laws.discipline.SerializableTests 21 | import kantan.codecs.laws.discipline.StringCodecTests 22 | import kantan.codecs.laws.discipline.StringDecoderTests 23 | import kantan.codecs.laws.discipline.StringEncoderTests 24 | import kantan.codecs.strings.StringDecoder 25 | import kantan.codecs.strings.StringEncoder 26 | import kantan.codecs.strings.java8.laws.discipline.arbitrary._ 27 | 28 | import java.time.LocalDateTime 29 | 30 | class LocalDateTimeCodecTests extends DisciplineSuite { 31 | 32 | checkAll("StringDecoder[LocalDateTime]", StringDecoderTests[LocalDateTime].decoder[Int, Int]) 33 | checkAll("StringDecoder[LocalDateTime]", SerializableTests[StringDecoder[LocalDateTime]].serializable) 34 | 35 | checkAll("StringEncoder[LocalDateTime]", StringEncoderTests[LocalDateTime].encoder[Int, Int]) 36 | checkAll("StringEncoder[LocalDateTime]", SerializableTests[StringEncoder[LocalDateTime]].serializable) 37 | 38 | checkAll("StringCodec[LocalDateTime]", StringCodecTests[LocalDateTime].codec[Int, Int]) 39 | 40 | } 41 | -------------------------------------------------------------------------------- /java8/core/src/test/scala/kantan/codecs/strings/java8/ZonedDateTimeCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings.java8 18 | 19 | import kantan.codecs.laws.discipline.DisciplineSuite 20 | import kantan.codecs.laws.discipline.SerializableTests 21 | import kantan.codecs.laws.discipline.StringCodecTests 22 | import kantan.codecs.laws.discipline.StringDecoderTests 23 | import kantan.codecs.laws.discipline.StringEncoderTests 24 | import kantan.codecs.strings.StringDecoder 25 | import kantan.codecs.strings.StringEncoder 26 | import kantan.codecs.strings.java8.laws.discipline.arbitrary._ 27 | 28 | import java.time.ZonedDateTime 29 | 30 | class ZonedDateTimeCodecTests extends DisciplineSuite { 31 | 32 | checkAll("StringDecoder[ZonedDateTime]", StringDecoderTests[ZonedDateTime].decoder[Int, Int]) 33 | checkAll("StringDecoder[ZonedDateTime]", SerializableTests[StringDecoder[ZonedDateTime]].serializable) 34 | 35 | checkAll("StringEncoder[ZonedDateTime]", StringEncoderTests[ZonedDateTime].encoder[Int, Int]) 36 | checkAll("StringEncoder[ZonedDateTime]", SerializableTests[StringEncoder[ZonedDateTime]].serializable) 37 | 38 | checkAll("StringCodec[ZonedDateTime]", StringCodecTests[ZonedDateTime].codec[Int, Int]) 39 | 40 | } 41 | -------------------------------------------------------------------------------- /refined/core/shared/src/main/scala/kantan/codecs/refined/RefinedInstances.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.refined 18 | 19 | import eu.timepit.refined.api.RefType 20 | import eu.timepit.refined.api.Validate 21 | import kantan.codecs.Decoder 22 | import kantan.codecs.Encoder 23 | import kantan.codecs.error.IsError 24 | 25 | trait DecoderInstances { 26 | 27 | /** Provides a [[Decoder]] instance for any refined type such that the reference type has a [[Decoder]] instance. */ 28 | implicit final def refinedDecoder[E, D, F, T, P, R[_, _]](implicit 29 | decoder: Decoder[E, D, F, T], 30 | validate: Validate[D, P], 31 | refType: RefType[R], 32 | t: IsError[F] 33 | ): Decoder[E, R[D, P], F, T] = 34 | decoder.emap { d => 35 | refType.refine(d).left.map(err => t.fromMessage(s"Not acceptable: '$err'")) 36 | } 37 | 38 | } 39 | 40 | trait EncoderInstances { 41 | 42 | /** Provides an [[Encoder]] instance for any refined type such that the reference type has an [[Encoder]] instance. */ 43 | implicit final def refinedEncoder[E, D, T, P, R[_, _]](implicit 44 | encoder: Encoder[E, D, T], 45 | refType: RefType[R] 46 | ): Encoder[E, R[D, P], T] = 47 | encoder.contramap(refType.unwrap) 48 | 49 | } 50 | -------------------------------------------------------------------------------- /java8/core/src/test/scala/kantan/codecs/strings/java8/OffsetDateTimeCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings.java8 18 | 19 | import kantan.codecs.laws.discipline.DisciplineSuite 20 | import kantan.codecs.laws.discipline.SerializableTests 21 | import kantan.codecs.laws.discipline.StringCodecTests 22 | import kantan.codecs.laws.discipline.StringDecoderTests 23 | import kantan.codecs.laws.discipline.StringEncoderTests 24 | import kantan.codecs.strings.StringDecoder 25 | import kantan.codecs.strings.StringEncoder 26 | import kantan.codecs.strings.java8.laws.discipline.arbitrary._ 27 | 28 | import java.time.OffsetDateTime 29 | 30 | class OffsetDateTimeCodecTests extends DisciplineSuite { 31 | 32 | checkAll("StringDecoder[OffsetDateTime]", StringDecoderTests[OffsetDateTime].decoder[Int, Int]) 33 | checkAll("StringDecoder[OffsetDateTime]", SerializableTests[StringDecoder[OffsetDateTime]].serializable) 34 | 35 | checkAll("StringEncoder[OffsetDateTime]", StringEncoderTests[OffsetDateTime].encoder[Int, Int]) 36 | checkAll("StringEncoder[OffsetDateTime]", SerializableTests[StringEncoder[OffsetDateTime]].serializable) 37 | 38 | checkAll("StringCodec[OffsetDateTime]", StringCodecTests[OffsetDateTime].codec[Int, Int]) 39 | 40 | } 41 | -------------------------------------------------------------------------------- /cats/laws/shared/src/main/scala/kantan/codecs/cats/laws/discipline/equality.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.cats.laws.discipline 18 | 19 | import cats.Eq 20 | import cats.instances.either._ 21 | import kantan.codecs.Decoder 22 | import kantan.codecs.Encoder 23 | import org.scalacheck.Arbitrary 24 | 25 | object equality extends EqInstances 26 | 27 | trait EqInstances { 28 | 29 | // This is needed because cats 2.0.0 introduces the notion of ExhaustiveCheck to derive instances of Eq[A => B], and 30 | // I do not know how to provide an exhaustive list of string values. 31 | implicit def eqFunction[A: Arbitrary, B: Eq]: Eq[A => B] = 32 | new Eq[A => B] { 33 | def eqv(x: A => B, y: A => B): Boolean = { 34 | val samples = List.fill(50)(Arbitrary.arbitrary[A].sample).collect { 35 | case Some(a) => a 36 | case None => sys.error("Could not generate arbitrary values to compare two functions") 37 | } 38 | samples.forall(a => Eq[B].eqv(x(a), y(a))) 39 | } 40 | } 41 | 42 | implicit def decoderEq[E: Arbitrary, D: Eq, F: Eq, T]: Eq[Decoder[E, D, F, T]] = 43 | Eq.by(_.decode _) 44 | 45 | implicit def encoderEq[E: Eq, D: Arbitrary, T]: Eq[Encoder[E, D, T]] = 46 | Eq.by(_.encode _) 47 | 48 | } 49 | -------------------------------------------------------------------------------- /core/shared/src/main/scala/kantan/codecs/resource/ResourceError.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.resource 18 | 19 | import kantan.codecs.error.Error 20 | import kantan.codecs.error.ErrorCompanion 21 | 22 | /** Errors that can occur while working with a [[Resource]]. */ 23 | sealed abstract class ResourceError(message: String) extends Error(message) 24 | 25 | object ResourceError { 26 | 27 | /** Errors that occur specifically while opening resources. */ 28 | final case class OpenError(message: String) extends ResourceError(message) 29 | 30 | object OpenError extends ErrorCompanion("an unspecified error occurred while opening a resource")(new OpenError(_)) 31 | 32 | /** Errors that occur specifically while processing resources. */ 33 | final case class ProcessError(message: String) extends ResourceError(message) 34 | 35 | object ProcessError 36 | extends ErrorCompanion("an unspecified error occurred while processing a resource")(new ProcessError(_)) 37 | 38 | /** Errors that occur specifically while closing resources. */ 39 | final case class CloseError(message: String) extends ResourceError(message) 40 | 41 | object CloseError extends ErrorCompanion("an unspecified error occurred while closing a resource")(new CloseError(_)) 42 | } 43 | -------------------------------------------------------------------------------- /scalaz/laws/shared/src/main/scala/kantan/codecs/scalaz/laws/discipline/equality.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.scalaz.laws.discipline 18 | 19 | import kantan.codecs.Decoder 20 | import kantan.codecs.Encoder 21 | import org.scalacheck.Arbitrary 22 | import scalaz.Equal 23 | 24 | object equality extends EqualInstances 25 | 26 | trait EqualInstances { 27 | 28 | implicit def decoderEqual[E: Arbitrary, D: Equal, F: Equal, T]: Equal[Decoder[E, D, F, T]] = 29 | new Equal[Decoder[E, D, F, T]] { 30 | override def equal(a1: Decoder[E, D, F, T], a2: Decoder[E, D, F, T]) = 31 | kantan.codecs.laws.discipline.equality.eq(a1.decode, a2.decode) { 32 | case (Left(f1), Left(f2)) => implicitly[Equal[F]].equal(f1, f2) 33 | case (Right(d1), Right(d2)) => implicitly[Equal[D]].equal(d1, d2) 34 | case _ => false 35 | } 36 | } 37 | 38 | implicit def encoderEqual[E: Equal, D: Arbitrary, T]: Equal[Encoder[E, D, T]] = 39 | new Equal[Encoder[E, D, T]] { 40 | override def equal(a1: Encoder[E, D, T], a2: Encoder[E, D, T]) = 41 | kantan.codecs.laws.discipline.equality.eq(a1.encode, a2.encode) { (e1, e2) => 42 | implicitly[Equal[E]].equal(e1, e2) 43 | } 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /core/jvm/src/test/scala/kantan/codecs/strings/DateCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.DecoderTests 20 | import kantan.codecs.laws.discipline.DisciplineSuite 21 | import kantan.codecs.laws.discipline.EncoderTests 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | import kantan.codecs.laws.discipline.arbitrary._ 26 | 27 | import java.text.SimpleDateFormat 28 | import java.util.Date 29 | import java.util.Locale 30 | 31 | class DateCodecTests extends DisciplineSuite { 32 | 33 | implicit val codec: StringCodec[Date] = 34 | StringCodec.dateCodec(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz", Locale.ENGLISH)) 35 | 36 | checkAll("StringDecoder[Date]", StringDecoderTests[Date].decoder[Int, Int]) 37 | checkAll("StringEncoder[Date]", StringEncoderTests[Date].encoder[Int, Int]) 38 | checkAll("StringCodec[Date]", StringCodecTests[Date].codec[Int, Int]) 39 | 40 | checkAll("TaggedDecoder[Date]", DecoderTests[String, Date, DecodeError, tagged.type].decoder[Int, Int]) 41 | checkAll("TaggedEncoder[Date]", EncoderTests[String, Date, tagged.type].encoder[Int, Int]) 42 | 43 | } 44 | -------------------------------------------------------------------------------- /cats/core/shared/src/test/scala/kantan/codecs/cats/DecoderTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.cats 18 | 19 | import cats.Eq 20 | import cats.data.EitherT 21 | import cats.laws.discipline.MonadErrorTests 22 | import cats.laws.discipline.SemigroupKTests 23 | import cats.laws.discipline.SemigroupalTests.Isomorphisms 24 | import kantan.codecs.cats.laws.discipline.DisciplineSuite 25 | import kantan.codecs.cats.laws.discipline.arbitrary._ 26 | import kantan.codecs.cats.laws.discipline.equality._ 27 | import kantan.codecs.strings.DecodeError 28 | import kantan.codecs.strings.StringDecoder 29 | 30 | class DecoderTests extends DisciplineSuite { 31 | 32 | // For some reason, these are not derived automatically. I *think* it's to do with StringDecoder being a type alias 33 | // for a type with many holes, but this is slightly beyond me. 34 | implicit val eqEitherT: Eq[EitherT[StringDecoder, DecodeError, Int]] = 35 | EitherT.catsDataEqForEitherT[StringDecoder, DecodeError, Int] 36 | 37 | implicit val iso: Isomorphisms[StringDecoder] = 38 | Isomorphisms.invariant[StringDecoder] 39 | 40 | checkAll("StringDecoder", SemigroupKTests[StringDecoder].semigroupK[Int]) 41 | checkAll("StringDecoder", MonadErrorTests[StringDecoder, DecodeError].monadError[Int, Int, Int]) 42 | 43 | } 44 | -------------------------------------------------------------------------------- /core/shared/src/test/scala/kantan/codecs/strings/EitherCodecTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.laws.discipline.DecoderTests 20 | import kantan.codecs.laws.discipline.DisciplineSuite 21 | import kantan.codecs.laws.discipline.EncoderTests 22 | import kantan.codecs.laws.discipline.StringCodecTests 23 | import kantan.codecs.laws.discipline.StringDecoderTests 24 | import kantan.codecs.laws.discipline.StringEncoderTests 25 | import kantan.codecs.laws.discipline.arbitrary._ 26 | import kantan.codecs.strings.tagged._ 27 | 28 | class EitherCodecTests extends DisciplineSuite { 29 | 30 | checkAll("StringDecoder[Either[Int, Boolean]]", StringDecoderTests[Either[Int, Boolean]].decoder[Int, Int]) 31 | checkAll("StringEncoder[Either[Int, Boolean]]", StringEncoderTests[Either[Int, Boolean]].encoder[Int, Int]) 32 | checkAll("StringCodec[Either[Int, Boolean]]", StringCodecTests[Either[Int, Boolean]].codec[Int, Int]) 33 | 34 | checkAll( 35 | "TaggedDecoder[Either[Int, Boolean]]", 36 | DecoderTests[String, Either[Int, Boolean], DecodeError, tagged.type].decoder[Int, Int] 37 | ) 38 | checkAll( 39 | "TaggedEncoder[Either[Int, Boolean]]", 40 | EncoderTests[String, Either[Int, Boolean], tagged.type].encoder[Int, Int] 41 | ) 42 | 43 | } 44 | -------------------------------------------------------------------------------- /core/shared/src/test/scala-2.12/kantan/codecs/resource/VersionSpecificResourceIteratorTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.resource 18 | 19 | trait VersionSpecificResourceIteratorTests { self: ResourceIteratorTests => 20 | test("seq should behave as expected") { 21 | forAll { (is: List[Int]) => 22 | ResourceIterator(is: _*).seq.toList should be(is) 23 | } 24 | } 25 | 26 | test("to[List] should behave as expected") { 27 | forAll { (is: List[Int]) => 28 | ResourceIterator(is: _*).to[List] should be(is) 29 | } 30 | } 31 | 32 | test("toTraversable should behave as expected") { 33 | forAll { (is: List[Int]) => 34 | ResourceIterator(is: _*).toTraversable should be(is.toTraversable) 35 | } 36 | } 37 | 38 | test("toIterator should behave as expected") { 39 | forAll { (is: List[Int]) => 40 | ResourceIterator(is: _*).toIterator.sameElements(is.toIterator) should be(true) 41 | } 42 | } 43 | 44 | test("toStream should behave as expected") { 45 | forAll { (is: List[Int]) => 46 | ResourceIterator(is: _*).toStream should be(is.toStream) 47 | } 48 | } 49 | 50 | test("toArray should behave as expected") { 51 | forAll { (is: List[Int]) => 52 | ResourceIterator(is: _*).toArray should be(is) 53 | } 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /laws/shared/src/main/scala/kantan/codecs/laws/discipline/DisciplinePackage.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.laws.discipline 18 | 19 | import kantan.codecs.strings.DecodeError 20 | import kantan.codecs.strings.codecs 21 | 22 | trait DisciplinePackage extends PlatformSpecificDisciplinePackage { 23 | 24 | type CodecTests[E, D, F, T] = kantan.codecs.laws.discipline.CodecTests[E, D, F, T] 25 | val CodecTests = kantan.codecs.laws.discipline.CodecTests 26 | 27 | type DecoderTests[E, D, F, T] = kantan.codecs.laws.discipline.DecoderTests[E, D, F, T] 28 | val DecoderTests = kantan.codecs.laws.discipline.DecoderTests 29 | 30 | type EncoderTests[E, D, T] = kantan.codecs.laws.discipline.EncoderTests[E, D, T] 31 | val EncoderTests = kantan.codecs.laws.discipline.EncoderTests 32 | 33 | type StringEncoderTests[A] = EncoderTests[String, A, codecs.type] 34 | val StringEncoderTests = kantan.codecs.laws.discipline.StringEncoderTests 35 | 36 | type StringDecoderTests[A] = DecoderTests[String, A, DecodeError, codecs.type] 37 | val StringDecoderTests = kantan.codecs.laws.discipline.StringDecoderTests 38 | 39 | type StringCodecTests[A] = CodecTests[String, A, DecodeError, codecs.type] 40 | val StringCodecTests = kantan.codecs.laws.discipline.StringCodecTests 41 | 42 | type DisciplineSuite = kantan.codecs.laws.discipline.DisciplineSuite 43 | 44 | } 45 | -------------------------------------------------------------------------------- /libra/laws/src/main/scala/kantan/codecs/libra/laws/discipline/arbitrary.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.libra.laws.discipline 18 | 19 | import kantan.codecs.laws.CodecValue.IllegalValue 20 | import kantan.codecs.laws.CodecValue.LegalValue 21 | import kantan.codecs.laws.IllegalString 22 | import kantan.codecs.laws.LegalString 23 | import kantan.codecs.strings.StringEncoder 24 | import libra.Quantity 25 | import org.scalacheck.Arbitrary 26 | import org.scalacheck.Arbitrary.{arbitrary => arb} 27 | import org.scalacheck.Cogen 28 | import shapeless.HList 29 | 30 | object arbitrary extends ArbitraryInstances with kantan.codecs.laws.discipline.ArbitraryInstances 31 | 32 | trait ArbitraryInstances { 33 | 34 | implicit def arbQuantity[A: Arbitrary, D <: HList]: Arbitrary[Quantity[A, D]] = 35 | Arbitrary(arb[A].map(Quantity(_))) 36 | 37 | implicit def cogenQuantity[A: Cogen, D <: HList]: Cogen[Quantity[A, D]] = 38 | Cogen[A].contramap(_.value) 39 | 40 | implicit def arbLegalQuantity[A: Arbitrary: StringEncoder, D <: HList]: Arbitrary[LegalString[Quantity[A, D]]] = 41 | Arbitrary(arb[A].map(a => LegalValue(StringEncoder[A].encode(a), Quantity(a)))) 42 | 43 | implicit def arbIllegalQuantity[A, D <: HList](implicit 44 | ai: Arbitrary[IllegalString[A]] 45 | ): Arbitrary[IllegalString[Quantity[A, D]]] = 46 | Arbitrary(ai.arbitrary.map(i => IllegalValue(i.encoded))) 47 | 48 | } 49 | -------------------------------------------------------------------------------- /core/jvm/src/main/scala/kantan/codecs/resource/PlatformSpecificInstances.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.resource 18 | 19 | import java.io.CharArrayReader 20 | import java.io.File 21 | import java.io.InputStream 22 | import java.io.OutputStream 23 | import java.io.Reader 24 | import java.net.URI 25 | import java.net.URL 26 | import java.nio.file.Files 27 | import java.nio.file.Path 28 | 29 | /** JVM specific instances of [[Resource]]. */ 30 | trait PlatformSpecificInstances { 31 | 32 | implicit val pathInputResource: InputResource[Path] = 33 | InputResource[InputStream].econtramap(p => OpenResult(Files.newInputStream(p))) 34 | 35 | implicit val pathOutputResource: OutputResource[Path] = 36 | OutputResource[OutputStream].econtramap(p => OpenResult(Files.newOutputStream(p))) 37 | 38 | implicit val fileInputResource: InputResource[File] = InputResource[Path].contramap(_.toPath) 39 | 40 | implicit val fileOutputResource: OutputResource[File] = OutputResource[Path].contramap(_.toPath) 41 | 42 | implicit val charsReaderResource: ReaderResource[Array[Char]] = 43 | ReaderResource[Reader].contramap(cs => new CharArrayReader(cs)) 44 | 45 | implicit val urlInputResource: InputResource[URL] = 46 | InputResource[InputStream].econtramap(u => OpenResult(u.openStream())) 47 | 48 | implicit val uriInputResource: InputResource[URI] = InputResource[URL].contramap(_.toURL) 49 | 50 | } 51 | -------------------------------------------------------------------------------- /laws/shared/src/main/boilerplate/kantan/codecs/laws/discipline/ArbitraryArities.template: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.laws.discipline 18 | 19 | import kantan.codecs.laws.CodecValue.{IllegalValue, LegalValue} 20 | import org.scalacheck.{Arbitrary, Gen} 21 | 22 | trait ArbitraryArities { 23 | [#def genLegalWith1[[#E1#], [#D1#], EE, DD, T](decoded: ([#D1#]) => DD)(encoded: ([#E1#]) => EE)(implicit [#arb1: Arbitrary[LegalValue[E1, D1, T]]#]): Gen[LegalValue[EE, DD, T]] = 24 | for { 25 | [#l1 <- arb1.arbitrary# 26 | ] 27 | } yield LegalValue(encoded([#l1.encoded#]), decoded([#l1.decoded#])) 28 | 29 | def genIllegalWith1[[#E1#], [#D1#], EE, DD, T](encoded: ([#E1#]) => EE)(implicit [#arb1: Arbitrary[IllegalValue[E1, D1, T]]#]): Gen[IllegalValue[EE, DD, T]] = 30 | for { 31 | [#l1 <- arb1.arbitrary# 32 | ] 33 | } yield IllegalValue(encoded([#l1.encoded#]))# 34 | 35 | ] 36 | 37 | 38 | [#def genLegalTuple1[[#E1#], [#D1#], T](implicit [#arbD1: Arbitrary[LegalValue[E1, D1, T]]#]): Gen[LegalValue[Tuple1[[#E1#]], Tuple1[[#D1#]], T]] = 39 | genLegalWith1(([#d1: D1#]) => Tuple1([#d1#]))(([#e1: E1#]) => Tuple1([#e1#])) 40 | 41 | def genIllegalTuple1[[#E1#], [#D1#], T](implicit [#arbD1: Arbitrary[IllegalValue[E1, D1, T]]#]): Gen[IllegalValue[Tuple1[[#E1#]], Tuple1[[#D1#]], T]] = 42 | genIllegalWith1(([#e1: E1#]) => Tuple1([#e1#]))([#arbD1#])# 43 | 44 | ] 45 | } 46 | -------------------------------------------------------------------------------- /shapeless/core/shared/src/test/scala/kantan/codecs/shapeless/InstancesTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.shapeless 18 | 19 | import kantan.codecs.shapeless.Instances._ 20 | import kantan.codecs.shapeless.laws.Or 21 | import kantan.codecs.shapeless.laws.discipline.DisciplineSuite 22 | import kantan.codecs.shapeless.laws.discipline.StringCodecTests 23 | import kantan.codecs.shapeless.laws.discipline.StringDecoderTests 24 | import kantan.codecs.shapeless.laws.discipline.StringEncoderTests 25 | import kantan.codecs.shapeless.laws.discipline.arbitrary._ 26 | import kantan.codecs.strings.StringEncoder 27 | import shapeless.CNil 28 | 29 | @SuppressWarnings(Array("org.wartremover.warts.Null")) 30 | class InstancesTests extends DisciplineSuite { 31 | 32 | // - Tests ----------------------------------------------------------------------------------------------------------- 33 | // ------------------------------------------------------------------------------------------------------------------- 34 | checkAll("StringDecoder[Int Or Boolean]", StringDecoderTests[Int Or Boolean].decoder[Int, Int]) 35 | checkAll("StringEncoder[Int Or Boolean]", StringEncoderTests[Int Or Boolean].encoder[Int, Int]) 36 | checkAll("StringCodec[Int Or Boolean]", StringCodecTests[Int Or Boolean].codec[Int, Int]) 37 | 38 | test("Encoder[?, CNil, ?] should fail") { 39 | intercept[IllegalStateException](StringEncoder[CNil].encode(null)) 40 | () 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /core/shared/src/test/scala-2.13/kantan/codecs/resource/VersionSpecificResourceIteratorTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.resource 18 | 19 | trait VersionSpecificResourceIteratorTests { self: ResourceIteratorTests => 20 | test("to(List) should behave as expected") { 21 | forAll { (is: List[Int]) => 22 | ResourceIterator(is: _*).to(List) should be(is) 23 | } 24 | } 25 | 26 | test("iterator should behave as expected") { 27 | forAll { (is: List[Int]) => 28 | ResourceIterator(is: _*).iterator.sameElements(is.iterator) should be(true) 29 | } 30 | } 31 | 32 | test("zipWithIndex should behave as expected") { 33 | forAll { (is: List[Int]) => 34 | ResourceIterator(is: _*).zipWithIndex.toList should be(is.zipWithIndex) 35 | } 36 | } 37 | 38 | test("scanLeft should behave as expected") { 39 | forAll { (is: List[String], z: Int, f: (Int, String) => Int) => 40 | ResourceIterator(is: _*).scanLeft(z)(f).toList should be(is.scanLeft(z)(f)) 41 | } 42 | } 43 | 44 | test("slice should behave as expected") { 45 | forAll { (is: List[Int], from: Int, to: Int) => 46 | ResourceIterator(is: _*).slice(from, to).toList should be(is.slice(from, to)) 47 | } 48 | } 49 | 50 | // Regression test for a rare (?!) random property failure. 51 | test("slice should behave properly when until is MinValue") { 52 | ResourceIterator(1, 2, 3).slice(1, Int.MinValue).hasNext should be(false) 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /java8/core/src/main/scala/kantan/codecs/strings/java8/FormatLiteral.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings.java8 18 | 19 | import scala.reflect.macros.blackbox.Context 20 | 21 | final class FormatLiteral(val sc: StringContext) extends AnyVal { 22 | def fmt(args: Any*): Format = 23 | macro FormatLiteral.fmtImpl 24 | } 25 | 26 | // Relatively distatefull trick to get rid of spurious warnings. 27 | trait FormatLiteralMacro { 28 | def fmtImpl(c: Context)(args: c.Expr[Any]*): c.Expr[Format] 29 | } 30 | 31 | object FormatLiteral extends FormatLiteralMacro { 32 | override def fmtImpl(c: Context)(args: c.Expr[Any]*): c.Expr[Format] = { 33 | import c.universe._ 34 | 35 | c.prefix.tree match { 36 | case Apply(_, List(Apply(_, List(lit @ Literal(Constant(str: String)))))) => 37 | Format.from(str) match { 38 | case Left(_) => c.abort(c.enclosingPosition, s"Illegal format: '$str'") 39 | case Right(_) => 40 | reify { 41 | val spliced = c.Expr[String](lit).splice 42 | 43 | Format 44 | .from(spliced) 45 | .getOrElse(sys.error(s"Illegal format: '$spliced'")) 46 | } 47 | } 48 | 49 | case _ => 50 | c.abort(c.enclosingPosition, "fmt can only be used on string literals") 51 | } 52 | } 53 | } 54 | 55 | trait ToFormatLiteral { 56 | implicit def toFormatLiteral(sc: StringContext): FormatLiteral = 57 | new FormatLiteral(sc) 58 | } 59 | -------------------------------------------------------------------------------- /laws/shared/src/main/scala/kantan/codecs/laws/discipline/EncoderTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.laws.discipline 18 | 19 | import kantan.codecs.laws.CodecValue.LegalValue 20 | import kantan.codecs.laws.EncoderLaws 21 | import org.scalacheck.Arbitrary 22 | import org.scalacheck.Cogen 23 | import org.scalacheck.Prop.forAll 24 | import org.typelevel.discipline.Laws 25 | 26 | trait EncoderTests[E, D, T] extends Laws { 27 | def laws: EncoderLaws[E, D, T] 28 | 29 | implicit def arbLegal: Arbitrary[LegalValue[E, D, T]] 30 | implicit val arbD: Arbitrary[D] 31 | implicit val cogenE: Cogen[E] 32 | 33 | def encoder[A: Arbitrary: Cogen, B: Arbitrary: Cogen]: RuleSet = 34 | new SimpleRuleSet( 35 | "core", 36 | "encode" -> forAll(laws.encode _), 37 | "contramap identity" -> forAll(laws.contramapIdentity _), 38 | "contramap composition" -> forAll(laws.contramapComposition[A, B] _), 39 | "mapEncoded identity" -> forAll(laws.mapEncodedIdentity _), 40 | "mapEncoded composition" -> forAll(laws.mapEncodedComposition[A, B] _) 41 | ) 42 | } 43 | 44 | object EncoderTests { 45 | def apply[E, D: Arbitrary, T](implicit 46 | l: EncoderLaws[E, D, T], 47 | al: Arbitrary[LegalValue[E, D, T]], 48 | ce: Cogen[E] 49 | ): EncoderTests[E, D, T] = 50 | new EncoderTests[E, D, T] { 51 | override val laws = l 52 | override val arbLegal = al 53 | override val cogenE = ce 54 | override val arbD = implicitly[Arbitrary[D]] 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kantan.codecs 2 | 3 | [![Build Status](https://github.com/nrinaudo/kantan.codecs/actions/workflows/cli.yml/badge.svg?branch=master)](https://github.com/nrinaudo/kantan.codecs/actions/workflows/cli.yml?branch=master) 4 | [![kantan.codecs Scala version support](https://index.scala-lang.org/nrinaudo/kantan.codecs/kantan.codecs/latest.svg)](https://index.scala-lang.org/nrinaudo/kantan.codecs/kantan.codecs) 5 | [![Join the chat at https://gitter.im/nrinaudo/kantan.codecs](https://img.shields.io/badge/gitter-join%20chat-52c435.svg)](https://gitter.im/nrinaudo/kantan.codecs) 6 | [![Scala Steward badge](https://img.shields.io/badge/Scala_Steward-helping-blue.svg?style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAQCAMAAAARSr4IAAAAVFBMVEUAAACHjojlOy5NWlrKzcYRKjGFjIbp293YycuLa3pYY2LSqql4f3pCUFTgSjNodYRmcXUsPD/NTTbjRS+2jomhgnzNc223cGvZS0HaSD0XLjbaSjElhIr+AAAAAXRSTlMAQObYZgAAAHlJREFUCNdNyosOwyAIhWHAQS1Vt7a77/3fcxxdmv0xwmckutAR1nkm4ggbyEcg/wWmlGLDAA3oL50xi6fk5ffZ3E2E3QfZDCcCN2YtbEWZt+Drc6u6rlqv7Uk0LdKqqr5rk2UCRXOk0vmQKGfc94nOJyQjouF9H/wCc9gECEYfONoAAAAASUVORK5CYII=)](https://scala-steward.org) 7 | 8 | ## Overview and warning 9 | Existing and planned kantan libraries are heavily encoding and decoding oriented - their main purpose is to turn 10 | raw data and decode it into useful types, or vice-versa. [kantan.xpath](https://github.com/nrinaudo/kantan.xpath), for 11 | instance, is all about turning the results of XPath expressions into types that can be more easily manipulated than 12 | strings or nodes. 13 | 14 | Since all these libraries share the same underlying purpose, it's only natural they should also share a lot of data 15 | structures, or at least *shapes* of data structures. Both [kantan.csv](https://github.com/nrinaudo/kantan.csv) and 16 | [kantan.xpath](https://github.com/nrinaudo/kantan.xpath), for example, define a `DecodeResult` type which is essentially 17 | the same. 18 | 19 | kantan.codecs tries to unify these types and provide generic laws and tests for them, which both reduces code 20 | duplication and provides a common vocabulary for all kantan libraries. 21 | 22 | It really isn't meant to be used directly and is more of a support library for more directly useful ones. 23 | -------------------------------------------------------------------------------- /core/shared/src/test/scala/kantan/codecs/DecoderCompanionTests.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs 18 | 19 | import kantan.codecs.error.IsError 20 | import org.scalatest.funsuite.AnyFunSuite 21 | import org.scalatest.matchers.should.Matchers 22 | import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks 23 | 24 | class DecoderCompanionTests extends AnyFunSuite with ScalaCheckPropertyChecks with Matchers { 25 | object codec 26 | object Companion extends DecoderCompanion[String, String, codec.type] 27 | 28 | implicit val stringIsError: IsError[String] = IsError[Exception].map(_.getMessage) 29 | 30 | type Dec = String => Either[String, Int] 31 | 32 | test("DecoderCompanion.from should be equivalent to Decoder.from") { 33 | forAll { (f: Dec, str: String) => 34 | Decoder.from(f).decode(str) should be(Companion.from(f).decode(str)) 35 | } 36 | } 37 | 38 | test("DecoderCompanion.oneOf should be equivalent to Decoder.oneOf for non-empty lists") { 39 | forAll { (h: Dec, t: List[Dec], str: String) => 40 | val cDec = Companion.oneOf((h :: t).map(Companion.from[Int]): _*) 41 | val dDec = Decoder.oneOf((h :: t).map(Decoder.from[String, Int, String, codec.type]): _*) 42 | 43 | cDec.decode(str) should be(dDec.decode(str)) 44 | } 45 | } 46 | 47 | test("DecoderCompanion.oneOf should be equivalent to Decoder.oneOf for empty lists") { 48 | val cDec = Companion.oneOf() 49 | val dDec = Decoder.oneOf[String, Int, String, codec.type]() 50 | 51 | forAll { (str: String) => 52 | cDec.decode(str) should be(dDec.decode(str)) 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /core/shared/src/main/scala/kantan/codecs/strings/StringDecoder.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.strings 18 | 19 | import kantan.codecs.DecoderCompanion 20 | 21 | /** Provides instance creation and summing methods for [[StringDecoder]]. 22 | * 23 | * Default [[StringDecoder]] instances are provided in [[codecs]]. 24 | */ 25 | object StringDecoder extends DecoderCompanion[String, DecodeError, codecs.type] with PlatformSpecificDecoders { 26 | 27 | /** Creates a safe decoding function from the specified unsafe one. 28 | * 29 | * This method expects the specified decoding function to be able to fail by throwing exceptions. These will be 30 | * caught and wrapped in [[DecodeError]]. 31 | * 32 | * This is typically used in conjunction with [StringDecoder.from], when creating instances for types that are not 33 | * isomorphic to `String`. 34 | * 35 | * @example 36 | * {{{ 37 | * scala> val decoder = StringDecoder.makeSafe("Int")(_.toInt) 38 | * 39 | * scala> decoder("1") 40 | * res1: StringResult[Int] = Right(1) 41 | * 42 | * scala> decoder("foobar") 43 | * res2: StringResult[Int] = Left(DecodeError: 'foobar' is not a valid Int) 44 | * }}} 45 | * 46 | * @param typeName 47 | * name of the decoded type (used in error messages). 48 | * @param f 49 | * decoding function. 50 | * @tparam D 51 | * decoded type. 52 | */ 53 | def makeSafe[D](typeName: String)(f: String => D): String => StringResult[D] = 54 | s => StringResult(f(s)).left.map(t => DecodeError(s"'$s' is not a valid $typeName", t)) 55 | 56 | } 57 | -------------------------------------------------------------------------------- /core/shared/src/main/scala-2.12/kantan/codecs/resource/VersionSpecificResourceIterator.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.resource 18 | 19 | import kantan.codecs.collection.Factory 20 | 21 | import scala.annotation.unchecked.{uncheckedVariance => uV} 22 | import scala.collection.mutable.Buffer 23 | import scala.reflect.ClassTag 24 | 25 | @SuppressWarnings( 26 | Array( 27 | "org.wartremover.warts.Var", 28 | "org.wartremover.warts.Throw", 29 | "org.wartremover.warts.While", 30 | "org.wartremover.warts.Null" 31 | ) 32 | ) 33 | trait VersionSpecificResourceIterator[+A] { self: ResourceIterator[A] => 34 | 35 | def to[F[_]](implicit factory: Factory[A @uV, F[A @uV]]): F[A @uV] = 36 | foldLeft(factory.newBuilder)(_ += _).result 37 | 38 | def toList: List[A] = 39 | to[List] 40 | def toArray[AA >: A](implicit ct: ClassTag[AA]): Array[AA] = 41 | toIterator.toArray 42 | def toBuffer[AA >: A]: Buffer[AA] = 43 | toIterator.toBuffer 44 | def toIndexedSeq: IndexedSeq[A] = 45 | to[IndexedSeq] 46 | def toIterable: Iterable[A] = 47 | to[Iterable] 48 | def toSeq: Seq[A] = 49 | to[Seq] 50 | def seq: Seq[A] = 51 | to[Seq] 52 | def toSet[AA >: A]: Set[AA] = 53 | toIterator.toSet 54 | def toVector: Vector[A] = 55 | to[Vector] 56 | def toTraversable: Traversable[A] = 57 | to[Traversable] 58 | def toStream: Stream[A] = 59 | if(hasNext) Stream.cons(next(), toStream) else Stream.empty 60 | def toIterator: Iterator[A] = 61 | new Iterator[A] { 62 | override def hasNext: Boolean = 63 | self.hasNext 64 | override def next(): A = 65 | self.next() 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /laws/shared/src/main/scala/kantan/codecs/laws/CodecValue.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.laws 18 | 19 | // TODO: investigate what type variance annotations can be usefully applied to CodecValue. 20 | sealed abstract class CodecValue[E, D, T] extends Product with Serializable { 21 | def encoded: E 22 | def mapEncoded[EE](f: E => EE): CodecValue[EE, D, T] 23 | def mapDecoded[DD](f: D => DD): CodecValue[E, DD, T] 24 | def tag[TT]: CodecValue[E, D, TT] 25 | 26 | def isLegal: Boolean 27 | def isIllegal: Boolean = 28 | !isLegal 29 | } 30 | 31 | object CodecValue { 32 | final case class LegalValue[E, D, T](encoded: E, decoded: D) extends CodecValue[E, D, T] { 33 | override def mapDecoded[DD](f: D => DD): LegalValue[E, DD, T] = 34 | LegalValue(encoded, f(decoded)) 35 | override def mapEncoded[EE](f: E => EE): LegalValue[EE, D, T] = 36 | LegalValue(f(encoded), decoded) 37 | @SuppressWarnings(Array("org.wartremover.warts.AsInstanceOf")) 38 | override def tag[TT]: LegalValue[E, D, TT] = 39 | this.asInstanceOf[LegalValue[E, D, TT]] 40 | override val isLegal = true 41 | } 42 | 43 | final case class IllegalValue[E, D, T](encoded: E) extends CodecValue[E, D, T] { 44 | override def mapDecoded[DD](f: D => DD): IllegalValue[E, DD, T] = 45 | IllegalValue(encoded) 46 | override def mapEncoded[EE](f: E => EE): IllegalValue[EE, D, T] = 47 | IllegalValue(f(encoded)) 48 | @SuppressWarnings(Array("org.wartremover.warts.AsInstanceOf")) 49 | override def tag[TT]: IllegalValue[E, D, TT] = 50 | this.asInstanceOf[IllegalValue[E, D, TT]] 51 | override val isLegal = false 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /core/shared/src/main/scala/kantan/codecs/error/Error.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2016 Nicolas Rinaudo 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package kantan.codecs.error 18 | 19 | /** Base class for errors 20 | * 21 | * This is specifically meant for errors represented as ADTs. The fact that it extends `Exception` is an unfortunate 22 | * side effect of the Scala stdlib's reliance on exceptions, as seen with `scala.concurrent.Future` and 23 | * `scala.util.Try`, for example. 24 | */ 25 | abstract class Error(message: String) extends Exception(message) with Product with Serializable { 26 | override final def toString: String = 27 | productPrefix + ": " + getMessage 28 | } 29 | 30 | /** Provides useful instance creation methods for errors that might be created as a result of Java exceptions. */ 31 | abstract class ErrorCompanion[T <: Error](defaultMsg: String)(f: String => T) extends Serializable { 32 | implicit val isError: IsError[T] = new IsError[T] { 33 | override def from(msg: String, cause: Throwable) = { 34 | val error = f(msg) 35 | error.initCause(cause) 36 | error 37 | } 38 | 39 | override def fromMessage(msg: String) = 40 | from(msg, new Exception(msg)) 41 | 42 | override def fromThrowable(cause: Throwable): T = 43 | from(Option(cause.getMessage).getOrElse(defaultMsg), cause) 44 | } 45 | 46 | /** Attempts to evaluate the specified argument, wrapping errors in a `T`. */ 47 | def safe[A](a: => A): Either[T, A] = 48 | isError.safe(a) 49 | 50 | def apply(msg: String, cause: Throwable): T = 51 | isError.from(msg, cause) 52 | def apply(cause: Throwable): T = 53 | isError.fromThrowable(cause) 54 | def apply(msg: String): T = 55 | isError.fromMessage(msg) 56 | } 57 | --------------------------------------------------------------------------------