Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added LiftRules.funcNameGenerator which controls the logic of S.formFuncName #1506

Merged
merged 1 commit into from
Feb 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions web/webkit/src/main/scala/net/liftweb/http/LiftRules.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ object LiftRules extends LiftRulesMocker {
val ExecutionFailure = Value(11, "Execution Failure")
}

def defaultFuncNameGenerator(runMode: Props.RunModes.Value): () => String =
runMode match {
case Props.RunModes.Test => S.generateTestFuncName _
case _ => S.generateFuncName _
}
}

/**
Expand Down Expand Up @@ -1791,6 +1796,9 @@ class LiftRules() extends Factory with FormVendor with LazyLoggable {
/** Controls whether or not the service handling timing messages (Service request (GET) ... took ... Milliseconds) are logged. Defaults to true. */
@volatile var logServiceRequestTiming = true

/** Provides a function that returns random names for form variables, page ids, callbacks, etc. */
@volatile var funcNameGenerator: () => String = defaultFuncNameGenerator(Props.mode)

import provider.servlet._
import containers._

Expand Down
20 changes: 16 additions & 4 deletions web/webkit/src/main/scala/net/liftweb/http/S.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2600,20 +2600,32 @@ trait S extends HasParams with Loggable with UserAgentCalculator {
f
}

def formFuncName: String = if (Props.testMode && !disableTestFuncNames_?) {
def formFuncName: String = LiftRules.funcNameGenerator()

/** Default func-name logic during test-mode. */
def generateTestFuncName: String =
if (disableTestFuncNames_?)
generateFuncName
else
generatePredictableFuncName

/** Generates a func-name based on the location in the call-site source code. */
def generatePredictableFuncName: String = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not be explicit here and call it generatePredictableFuncNameFromStack?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thinking was generatePredictableFuncName describes the purpose and not the how.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair, but in this caseI feel like the purpose is important. It answers the question “why would I ever want to replace this” ;) If the configurable hook were this method, it would make sense for it to be non-specific, but since the hook is funcNameGenerator, I think we benefit from specificity. Could be wrong, though; willing to hear different thoughts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean you feel like the how is important? I think I see your point, if it were more specifically named it would make it easier to answer the question "why would I ever want to replace this". Personally I don't think many people would ask that question though, I imagined it would be "what do I get from using this".

Both our perspectives are valid. That considered I guess the most technically appropriate would be:

// Default for those who don't care how and just want a deterministic ID.
def predictableFuncName = predictableFuncNameDerivedFromStackTrace

// For those concerned with or dependant on the stack-trace approach specifically
def predictableFuncNameDerivedFromStackTrace = // ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that approach makes sense if predictableFuncName is replaceable. In this context, it really isn't, which is why I figure you can just shortcut to the specific, purpose-tied name. The specific one answers “what do I get” both generally (it's predictable) and specifically (it's from the stack). This can be useful because there are cases where the predictable generator actually is too predictable—you can get duplicates at times, leading to weird behavior—and knowing that it comes from the stack trace as a user might help tell you what weirdness is happening without having to get into the nitty gritty of what the framework is doing.

Basically my attitude for the framework is the less work for a user doing debugging, the better. I can see this helping that goal.

I don't consider it a huge deal, and other than that this PR looks good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why you think predictableFuncName is not replaceable? I can't think of any reasons why it wouldn't be, in fact I'd hope that one day it gets a better implementation that works in all cases (it fails given my usage).

I get your point about making life as easier as possible for the framework user and I agree. It is a noble goal. I'm not sure that making the function name more specific would help too much as it would already take a bit of digging to get to that point and it doesn't call out to user code meaning for the name to show up in a debugger they'd either need to do lots of stepping or already be aware of the functions existence and have set a breakpoint.

(Personally I think that generateTestFuncName which is there to preserve existing behaviour, is more of a barrier to clarity and comprehension.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant runtime-replaceable. I'm also not sure it should be publicly callable.

In general, I have a preference for encoding some things in the function name that others put in the comment above it. I find that makes it more likely for the thing that describes what the function does to be updated when the implementation changes, vs the comment which is often left behind.

Net net, I'm good to go on this. Haven't seen any other objections, so I'm going to go ahead and merge it. Hang onto your butts 😎

val bump: Long = ((_formGroup.is openOr 0) + 1000L) * 100000L
val num: Int = formItemNumber.is
formItemNumber.set(num + 1)
import java.text._
val prefix: String = new DecimalFormat("00000000000000000").format(bump + num)
// take the first 2 non-Lift/non-Scala stack frames for use as hash issue 174
"f" + prefix + "_" + Helpers.hashHex((new Exception).getStackTrace.toList.filter(notLiftOrScala).take(2).map(_.toString).mkString(","))
} else {
}

/** Standard func-name logic. This is the default routine. */
def generateFuncName: String =
_formGroup.is match {
case Full(x) => Helpers.nextFuncName(x.toLong * 100000L)
case _ => Helpers.nextFuncName
case _ => Helpers.nextFuncName
}
}

def formGroup[T](group: Int)(f: => T): T = {
val x = _formGroup.is
Expand Down
56 changes: 56 additions & 0 deletions web/webkit/src/test/scala/net/liftweb/http/SSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2013 WorldWide Conferencing, LLC
*
* 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 net.liftweb
package http

import org.specs2.mutable.Specification
import util.Helpers
import util.Props.RunModes
import LiftRules.defaultFuncNameGenerator

object SSpec extends Specification {
"S Specification".title

"formFuncName" should {
"generate random names when not in Test mode" in {
for (mode <- RunModes.values if mode != RunModes.Test) {
val a,b = defaultFuncNameGenerator(mode)()
a must startWith("F")
a.length must_== Helpers.nextFuncName.length
a must_!= b
}
}

"generate predictable names in Test mode" in {
val a,b = S.formFuncName
a must startWith("f")
a.length must_!= Helpers.nextFuncName.length
a must_== b
a must_!= S.formFuncName
}

"generate resort back to random names when test func-names disabled" in {
S.disableTestFuncNames {
val a,b = S.formFuncName
a must startWith("F")
a.length must_== Helpers.nextFuncName.length
a must_!= b
}
}

}
}