Variables. scope inside of a cfmodule - scope

I have a page with a name of
summary.cfm
inside of it:
<cfinclude template="view/totals.cfm>
view/totals.cfm
inside of it:
variables.grandTotalHTML = invoke("viewtotals, "doSummary", {...});
view/viewtotals.cfc
inside of it
<cfmodule template="summarytemplate.cfm" ...>
<!--- database is not passed in here --->
view/summarytemplate.cfm
Inside of it we have
param attributes.database = session.database;
...
databaseoverride = attributes.database;
...
<cfquery name="qData">
SELECT *
FROM [#variables.databaseoverride#]
...
</cfquery>
Now question
I don't know where the databaseoverride is coming from.
Does global page request scope?
Does it come from variables in viewtotals.cfc ?
Does the unscoped version override it?

The variables scope at the module level is local to the module. An unscoped variable in a module is in variables scope.
This line
databaseoverride = attributes.database;
is equivalent to
variables.databaseoverride = attributes.database;
so is setting the value used here
<cfquery name="qData">
SELECT *
FROM [#variables.databaseoverride#]
...
</cfquery>

(Too long for comments)
Just to elaborate on Dan's answer:
summary.cfm
Only shares a VARIABLES scope with the included template, "view/totals.cfm"
view/totals.cfm
Only shares a VARIABLES scope with the parent template, "summary.cfm"
view/viewTotals.cfc
Its VARIABLES scope is not shared with any of the calling templates (summary.cfm and view/totals.cfm)
Its VARIABLES are accessible to the cfmodule - through the CALLER scope (as are the function's local and arguments scopes)
view/summaryTemplate.cfm
Does not share its VARIABLES scope with anyone.
Can view/modify any scopes in the parent component (viewTotals.cfc) through the CALLER scope.
( The REQUEST scope is accessible to all of the scripts above.)

Related

Terraform: how to declare a variable as required (or optional) in variables.tf?

Here I have a variables.tf to define the Input variables.
# Input variable definitions
variable "project" {
description = "project name, e.g. paylocity, paychex, ultipro"
type = string
}
variable "environment" {
description = "the environment of project, e.g. production, sandbox, staging"
type = string
default = "sandbox"
}
Is there a way to mark a variable as required or optional?
The pseudo-code in my mind looks like this.
variable "project" {
description = "project name, e.g. paylocity, paychex, ultipro"
type = string
presence = required (or optional)
}
The purpose of variables.tf
variables.tf - here, you define the variables that must have values in order for your Terraform code to validate and run. You can also define default values for your variables in this file. Note that you don't need to define all of your variables in a file named
from What is the difference between variables.tf and terraform.tfvars?
Input Variables: Default Value
The variable declaration can also include a default argument. If present, the variable is considered to be optional and the default value will be used if no value is set when calling the module or running Terraform. The default argument requires a literal value and cannot reference other objects in the configuration.
From Terraform Documentation
Summary
All defined variables must have values in order to run Terraform code.
Once you set a default value for a variable, it becomes optional.
I'm using terraform 1.3.1. I think that this problem can solve with argument nullable.
The nullable argument in a variable block controls whether the
module caller may assign the value null to the variable.
This feature is available in Terraform v1.1.0 and later.
Example:
variable "example" {
type = string
nullable = false
}
Reference: https://developer.hashicorp.com/terraform/language/values/variables?optInFrom=terraform-io#disallowing-null-input-values
Since 0.14 there is an experimental (in time of writing) function that can allow you this. Here are some documents on the matter.
By default, for required attributes, Terraform will return an error if the source value has no matching attribute. Marking an attribute as optional changes the behavior in that situation: Terraform will instead just silently insert null as the value of the attribute, allowing the receiving module to describe an appropriate fallback behavior.
https://www.terraform.io/language/expressions/type-constraints#experimental-optional-object-type-attributes
https://danielrandell93.medium.com/terraform-optional-values-73407f1d5ce5

How to set a puppet class variable from within a hiera_hash each loop?

hiera data
ae::namespace_by_fqdn_pattern:
'((^dfw-oel6)|(^dfw-oel7)|(^dfw-ubuntu1604))-((client))([0-9]{2}).pp-devcos-ae.us-central1.gcp.dev.blah.com': '/test/blah/regression/client'
'((^dfw-oel6)|(^dfw-oel7)|(^dfw-ubuntu1604))-((server))([0-9]{2}).pp-devcos-ae.us-central1.gcp.dev.blah.com': '/test/blah/regression/server'
class
class ae {
$namespace = hiera('ae::namespace')
$target_host_patterns = hiera('ae::target_host_patterns')
hiera_hash('ae::namespace_by_fqdn_pattern').each |String $pattern, String $ns| {
if $facts['networking']['fqdn'].match($pattern) {
$ae::namespace = "${ns}"
}
}
<snip>
... yields
Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Illegal attempt to assign to 'ae::enforcerd_namespace'. Cannot assign to variables in other namespaces (file: /etc/puppetlabs/code/environments/ar/modules/ae/manifests/init.pp, line: 21, column: 13) on node dfw-ubuntu1604-client02.pp-devcos.us-central1.gcp.dev.blah.com
... anyone here know how to do this correctly? trying to conditionally override that $ae::namespace variable but i'm too puppet-ignorant to know how to get it working : (
the loop and the pattern matching bits work. just can't figure out how to correctly set that class variable from within the hiera_hash().each loop.
How to set a puppet class variable from within a hiera_hash each loop?
You cannot. The associated block of an each() call establishes a local scope for each iteration. Variable assignments within apply to that local scope, and therefore last only for the duration of one execution of the block. You cannot anyway assign a new value to a variable during its lifetime, so even if you could assign to a class variable from within an each() call, it would be difficult to use that capability (and your approach would not work).
There are several ways you could approach the problem without modifying the form of the data. You could leverage the filter() function, for example, but my personal recommendation would be to use the reduce() function, something like this:
$namespace = lookup('ae::target_host_patterns').reduce(lookup('ae::namespace')) |$ns, $entry| {
$facts['networking']['fqdn'].match($entry[0]) ? { true => $entry[1], default => $ns }
}
That does pretty much exactly what your original code seems to be trying to do, except that the selected namespace is returned by the reduce() call, to be assigned to a variable by code at class scope, instead of the lambda trying to assign it directly. Note also that it takes care not only of testing the patterns but of assigning the default namespace when none of the patterns match, as it needs to do because you can only assign to the namespace variable once.
so the solution i landed on was to change the hiera data to:
ae::namespace : '/test/blah/regression'
ae::namespace_patterns: ['((^dfw-oel6)|(^dfw-oel7)|(^dfw-ubuntu1604))-((client))([0-9]{2}).pp-devcos-ae.us-central1.gcp.dev.blah.com', '((^dfw-oel6)|(^dfw-oel7)|(^dfw-ubuntu1604))-((server))([0-9]{2}).pp-devcos-ae.us-central1.gcp.dev.blah.com']
ae::namespace_by_pattern:
'((^dfw-oel6)|(^dfw-oel7)|(^dfw-ubuntu1604))-((client))([0-9]{2}).pp-devcos-ae.us-central1.gcp.dev.blah.com': '/test/paypal/regression/client'
'((^dfw-oel6)|(^dfw-oel7)|(^dfw-ubuntu1604))-((server))([0-9]{2}).pp-devcos-ae.us-central1.gcp.dev.blah.com': '/test/paypal/regression/server'
then the class code to:
$pattern = hiera_hash('ae::namespace_patterns').filter |$pattern| {
$facts['networking']['fqdn'] =~ $pattern
}
if length($pattern) {
$namespace = hiera('ae::namespace_by_pattern')[$pattern[0]]
} else {
$namespace = hiera('ae::namespace')
}
definitely still open to better answers. just what my own hacking produced as workable so far through much trial and error.

How can I see my node.js variables already initiated?

I'm pretty new to node.js and I was wondering: seeing as the scope of any variable created is set to a sort of global scope, is there any way of listing out what variable names have already been initiated?
node.js variables are generally not global. The default is that any variable declared at the top level of a module is only in the module scope and does not conflict or interfere with any other modules.
Only variables explicitly assigned to the global object are actually available globally.
global.myVar = "foo";
All others are contained within a smaller scope - module scope or a function scope within a module.
So, if you have a module like this:
var x = 3;
module.exports = function() {
return x;
}
Then, the variable x is not a global variable at all. It is contained with the scope of this module. It would not conflict with a variable in any other module of the same name.
Because top level variables in a module are actually local variables within a module function wrapper and Javascript does not provide any means of iterating the local variables within a function scope, there is no way to iterate all top level variables within a module scope.
If you wanted to be able to iterate all variables in some context, then you can use a different format and put them as properties on an object.
var obj = {};
obj.x = 3;
obj.y = 5;
// list all enumerable properties of obj
for (var prop in obj) {
console.log("property: " + prop + " = ", + obj[prop]);
}
FYI, you can also use a debugger like node-inspector to view all variables declared within any given scope. You set a breakpoint in that scope and then look at the local variables present at that breakpoint.

MDX Scope Assignment returns same value

I have some MDX in my SSAS cube something like:
CREATE MEMBER CURRENTCUBE.[Measures].[xx] AS 1;
SCOPE ([Measures].[xx]);
SCOPE ([Measures].[xx], [DimA].[DimA Hierarchy].Members);
This= 20;
END SCOPE;
SCOPE ([Measures].[xx], [DimB].[DimB Hierarchy].Members);
This= 2;
END SCOPE;
END SCOPE;
Browsing the cube and selecting the [xx] measure always returns 2, no matter if I use the DimA, DimB or no dimensions at all.
If I swap the order of the inner scope declrations, (ie DimB's scope before DimA's) I get 20 no matter what.
Clearly I'm doing something wrong??
SCOPE only makes sense if you use it on a subset of the members of a hierarchy.
[DimB].[DimB Hierarchy].Members and [DimA].[DimA Hierarchy].Members have no effect in scope, as they refer to all members of the hierarchy. Hence, one of these is always the current member, and the last scope always is used. Except you define a calculated member on that hierarchy, and it is the current member.
You probably want at least to exclude the All member, so that the SCOPE is only used for other members that this member, which is by default the current member if you do not use the hierarchy at all. In case DimA Hierarchy is an attribute hierarchy and not a user hierarchy, you can use [DimA].[DimA Hierarchy].[DimA Hierarchy].Members to get the members of the DimA Hierarchy level, which are all members except the All member.
Try something like this:
CREATE MEMBER CURRENTCUBE.[Measures].[xx] AS 1;
SCOPE ([Measures].[xx]);
SCOPE ([DimA].[DimA Hierarchy].Members);
This= 20;
END SCOPE;
SCOPE ([DimB].[DimB Hierarchy].Members);
This= 2;
END SCOPE;
END SCOPE;

Initialize class reference

I get null value of instrumentRented
when I run
public class RentalAgreement
{
private MusicalInstrument instrumentRented;
public RentalAgreement(Customer renter,
RentalDate dateRented,
MusicalInstrument instrumentRented){
customer = renter;
rentalDate = dateRented;
instrumentRented = instrumentRented;
How to initialize MusicalInstrument reference in RentalAgreement?
Use this.instrumentRented = instrumentRented;
Since the parameter has the same name as the field attribute, you need to prefix with an explicit this to specify the scope.
You must instantiate a class with the new operator.
So somehwere in your code must do
instrumentRented = new MusicalInstrument();
before you access it. After you have done this you can execute functions from that class.
instrumentRented.doSomething();
In the above code you seem to pass it in in the constructor, so this means the caller has to instantiate it.
However I would advise to adopt a naming convention where you can see if a variable is a class member or a local variable. In your above code the local variable has the same name as the parameter, so it will not be set as a member variable, instead you assign it to itself. You might get a warning about this, depending on the evironment (Not sure about this but Eclipse definitely warns somethign like this). This is called shadowing, so what you need to do is:
this.instrumentRented = instrumentRented;

Resources