Skip to content

Commit

Permalink
surface (fix): Fixes #3356 for case classes with private/protected fi…
Browse files Browse the repository at this point in the history
…elds (#3407)

- **surface (fix): Fixes #3356 for case classes with private/protected
parameters**
- **Fix for Scala 2**
  • Loading branch information
xerial authored Feb 25, 2024
1 parent 186e8a2 commit c810acc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,13 @@ private[surface] object SurfaceMacros {
case other => q"None"
}

val accessor = if (method.isConstructor) {
val isPublic: Boolean = targetType.member(arg.paramName.name) match {
case NoSymbol =>
false
case s =>
s.asTerm.isPublic
}
val accessor = if (method.isConstructor && isPublic) {
arg.accessor(targetType)
} else {
q"None"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,10 +595,11 @@ private[surface] class CompileTimeSurfaceFactory[Q <: Quotes](using quotes: Q):
// Generate a field accessor { (x:Any) => x.asInstanceOf[A].(field name) }
val paramIsAccessible =
t.typeSymbol.fieldMember(paramName) match
case nt if nt == Symbol.noSymbol => false
case m if m.flags.is(Flags.Private) => false
case m if m.flags.is(Flags.Artifact) => false
case _ => true
case nt if nt == Symbol.noSymbol => false
case m if m.flags.is(Flags.Private) => false
case m if m.flags.is(Flags.Protected) => false
case m if m.flags.is(Flags.Artifact) => false
case _ => true
// println(s"${paramName} ${paramIsAccessible}")

val accessor: Expr[Option[Any => Any]] = if method.isClassConstructor && paramIsAccessible then
Expand Down
32 changes: 32 additions & 0 deletions airframe-surface/src/test/scala/wvlet/airframe/surface/i3356.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package wvlet.airframe.surface

import wvlet.airspec.AirSpec

object i3356 extends AirSpec {

case class C(protected val id: Int, private val key: String)

test("List private/protected fields as parameters") {
val s = Surface.of[C]
debug(s.params)
s.params.size shouldBe 2
val p1 = s.params(0)
p1.name shouldBe "id"

val p2 = s.params(1)
p2.name shouldBe "key"
}
}

0 comments on commit c810acc

Please sign in to comment.