{"id":20592085,"url":"https://github.com/bennyhuo/kotlindeepcopy","last_synced_at":"2025-04-05T19:14:51.181Z","repository":{"id":57717759,"uuid":"156778105","full_name":"bennyhuo/KotlinDeepCopy","owner":"bennyhuo","description":"DeepCopy extensions for Kotlin  Data class. Provide Reflection, Apt, Ksp and Kcp implementations.","archived":false,"fork":false,"pushed_at":"2024-02-05T03:12:21.000Z","size":436,"stargazers_count":287,"open_issues_count":3,"forks_count":28,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-03-29T18:08:31.075Z","etag":null,"topics":["apt","dataclass","deepcopy","kcp","kotlin","ksp","reflection"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bennyhuo.png","metadata":{"files":{"readme":"ReadMe.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-11-08T22:37:30.000Z","updated_at":"2025-03-23T14:09:10.000Z","dependencies_parsed_at":"2023-11-18T15:09:09.103Z","dependency_job_id":"39b50a87-a733-4ad0-a779-78c61219e741","html_url":"https://github.com/bennyhuo/KotlinDeepCopy","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bennyhuo%2FKotlinDeepCopy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bennyhuo%2FKotlinDeepCopy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bennyhuo%2FKotlinDeepCopy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bennyhuo%2FKotlinDeepCopy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bennyhuo","download_url":"https://codeload.github.com/bennyhuo/KotlinDeepCopy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247386266,"owners_count":20930619,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apt","dataclass","deepcopy","kcp","kotlin","ksp","reflection"],"created_at":"2024-11-16T07:42:42.095Z","updated_at":"2025-04-05T19:14:51.153Z","avatar_url":"https://github.com/bennyhuo.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.bennyhuo.kotlin/deepcopy-reflect/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.bennyhuo.kotlin/deepcopy-reflect)\n\n\n# KotlinDeepCopy\n\nProvide an easy way to generate `DeepCopy` function for `data class`. DeepCopy only takes effect on the component members i.e. the members declared in the primary constructor.\n\n## Reflection\n\nUse Kotlin Reflection to provide an extension function for `DeepCopyable` so that any data class which implements `DeepCopyable` can simply call `deepCopy()` to copy itsself.\n\nSee the test code below: \n\n```kotlin\ndata class Speaker(val name: String, val age: Int): DeepCopyable\n\ndata class Talk(val name: String, val speaker: Speaker): DeepCopyable\n\nclass DeepCopyTest {\n    @Test\n    fun test() {\n        val talk = Talk(\"DataClass in Action\", Speaker(\"Benny Huo\", 30))\n        val newTalk = talk.deepCopy()\n        assert(talk == newTalk)\n        assert(talk !== newTalk)\n    }\n}\n```\n\n`talk` equals `newTalk` since the values of their members are equal while they do not ref to the same object.\n\n### How to Setup\n\nThis library has been deloyed to maven center. \n\n```gradle\nimplementation(\"com.bennyhuo.kotlin:deepcopy-reflect:\u003clatest-version\u003e\")\n```\n\n## Apt \u0026 Ksp\n\nIf you concern about the runtime efficiency, apt may be the solution. You can simply annotate the data class you want to deep copy with a `DeepCopy` annotation:\n\n```kotlin\n@DeepCopy\ndata class Speaker(val name: String, val age: Int)\n@DeepCopy\ndata class Talk(val name: String, val speaker: Speaker)\n```\n\nExtension function `deepCopy` will be generated according to the components:\n\n```kotlin\nfun Talk.deepCopy(name: String = this.name, speaker: Speaker = this.speaker): Talk = Talk(name, speaker.deepCopy())\n\nfun Speaker.deepCopy(\n    name: String = this.name,\n    age: Int = this.age,\n    company: Company = this.company\n): Speaker = Speaker(name, age, company.deepCopy()) \n```\n\nNotice that `deepCopy` is called recursively if the member type is also a `data class` annotated with a `DeepCopy` annotation. Hence, if you remove the annotation for `Speaker`, generated function would be like:\n\n```kotlin\nfun Talk.deepCopy(name: String = this.name, speaker: Speaker = this.speaker): Talk = Talk(name, speaker)\n\n//And no deepCopy for Speaker.\n```\n\n### How to Setup\n\nThe artifacts have been deployed to maven central repository. Set up your project by adding these lines:\n\n```gradle\nplugins {\n    id(\"com.google.devtools.ksp\") version \"1.9.20-1.0.14\" // ksp\n    id \"org.jetbrains.kotlin.kapt\" // kapt\n}\n...\n\ndependencies {\n    ksp(\"com.bennyhuo.kotlin:deepcopy-compiler-ksp:\u003clatest-version\u003e\")) // ksp\n    kapt(\"com.bennyhuo.kotlin:deepcopy-compiler-kapt:\u003clatest-version\u003e\") // kapt\n    implementation(\"com.bennyhuo.kotlin:deepcopy-runtime:\u003clatest-version\u003e\")\n}\n```\n\n## KCP\n\nThis is a nearly perfect version I think. It works like `copy` does. You can install this IntelliJ plugin: [DeepCopy](https://plugins.jetbrains.com/plugin/19915-deepcopy-for-kotlin-data-class) and setup your project like this:\n\n```gradle\nplugins {\n    kotlin(\"jvm\") version \"1.9.20\"\n    id(\"com.bennyhuo.kotlin.plugin.deepcopy\") version \"\u003clatest-version\u003e\"\n}\n\ndependencies {\n    implementation(\"com.bennyhuo.kotlin:deepcopy-runtime:\u003clatest-version\u003e\")\n}\n```\n\nAnd then try to call the `deepCopy` function directly!\n\n# Change Log\n\nSee [releases](https://github.com/bennyhuo/KotlinDeepCopy/releases).\n\n# License\n\n[MIT License](LICENSE)\n\n    Copyright (c) 2018 Bennyhuo\n    \n    Permission is hereby granted, free of charge, to any person obtaining a copy\n    of this software and associated documentation files (the \"Software\"), to deal\n    in the Software without restriction, including without limitation the rights\n    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n    copies of the Software, and to permit persons to whom the Software is\n    furnished to do so, subject to the following conditions:\n    \n    The above copyright notice and this permission notice shall be included in all\n    copies or substantial portions of the Software.\n    \n    THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n    SOFTWARE.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbennyhuo%2Fkotlindeepcopy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbennyhuo%2Fkotlindeepcopy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbennyhuo%2Fkotlindeepcopy/lists"}