Fusion (Typoscript 2): How to access a variable from a parent object? - neoscms

This is sort of a follow-up question for How to define and access local variable in Typoscript 2 (Neos)?
If I define a local variable, called myLocalVar in the example below, how can I access it from other objects, in this case from Neos.Fusion:Case?
prototype(Some.Namespace:SomeNodeType) < prototype(TYPO3.Neos:Content) {
myLocalVar = ${String.split(q(node).property('example'), '/', 2)}
myResult = Neos.Fusion:Case {
a = Neos.Fusion:Matcher {
condition = ${???.myLocalVar[0] == 'aaa'}
renderer = 'first part is aaa'
}
b = Neos.Fusion:Matcher {
condition = ${???.myLocalVar[0] == 'bbb'}
renderer = 'first part is bbb'
}
}
}
In this concrete example: How can I access myLocalVar from inside Neos.Fusion:Matcher?
The part in question is the condition: condition = ${???.myLocalVar[0] == 'aaa'}

You need to define your myLocalVar as context variable:
#context.myLocalVar = ${String.split(q(node).property('example'), '/', 2)}
the context is inherited by all nesting objects, so you can just access the value like this
a = Neos.Fusion:Matcher {
condition = ${myLocalVar[0] == 'aaa'}
renderer = 'first part is aaa'
}

Related

Change value of variable by reference

Hi I have 4 variables and I want to refer to one of them depending on some conditions, my current code looks as follows:
switch color {
case .Azul:
botonACambiar.tintColor = colores.salidaAzul
if backTees == true {
if option == 1{self.colorHcpBackTees1 = .Azul} else{self.colorHcpBackTees2 = .Azul}
} else {
if option == 1{self.colorHcpFrontTees1 = .Azul} else{self.colorHcpFrontTees2 = .Azul}
}
...
...
case . Cafe:
botonACambiar.tintColor = colores.salidaMarron
if backTees == true {
if option == 1{self.colorHcpBackTees1 = .Cafe} else{self.colorHcpBackTees2 = .Cafe}
} else {
if option == 1{self.colorHcpFrontTees1 = .Cafe} else{self.colorHcpFrontTees2 = .Cafe}
}
}
I have around 8 options, and as you can see it gets kind of messy.
So far I only have 4 different variables that i might manipulate:
Option 1 & 2 for FrontTees & option 1 & 2 for BackTees, but in the future the app might support more options and more colors for front and back tees.
This 4 values are saved in the app and because of this I need to keep track of them individually, thus I have the four variables:
var colorHcpBackTees1: ColoresDeSalidas = .Negra
var colorHcpBackTees2: ColoresDeSalidas = .Blanca
var colorHcpFrontTees1: ColoresDeSalidas = .Roja
var colorHcpFrontTees2: ColoresDeSalidas = .Blanca
Each with their default values, it would be nice to have a way of initializing a reference variable in such a way that I could do something like:
var choice: ColoresDeSalidas{
if backTees && option == 1 { return self.colorHcpBackTees1}
if backTees && option == 2 { return self.colorHcpBackTees2}
if !backTees && option == 1 { return self.colorHcpFrontTees1}
if !backTees && option == 2 { return self.colorHcpFrontTees2}
}
And simply use the variable choice to manipulate the value of the right variable
You can accomplish this using a ReferenceWritableKeyPath.
What is a ReferenceWritableKeyPath?
Think of it as a precise description of a property in a class or struct. A real world example would be The Empire State Building instead of the address which would be 20 W 34th St, New York, NY 10001. You could tell either one to a cab driver and she could take you there.
A ReferenceWritableKeyPath is a generic type. You need to specify the class or struct name and the type of the variable you will be accessing. So your choice variable would be a ReferenceWritableKeyPath<YourClass,ColoresDeSalidas>.
Here is a standalone example:
enum ColoresDeSalidas : String {
case Negra
case Blanca
case Roja
case Azul
case Verde
}
class Foo {
var backTees = false
var option = 1
var colorHcpBackTees1: ColoresDeSalidas = .Negra
var colorHcpBackTees2: ColoresDeSalidas = .Blanca
var colorHcpFrontTees1: ColoresDeSalidas = .Roja
var colorHcpFrontTees2: ColoresDeSalidas = .Blanca
var choice: ReferenceWritableKeyPath<Foo,ColoresDeSalidas> {
if backTees && option == 1 { return \.colorHcpBackTees1 }
if backTees && option == 2 { return \.colorHcpBackTees2 }
if !backTees && option == 1 { return \.colorHcpFrontTees1 }
if !backTees && option == 2 { return \.colorHcpFrontTees2 }
fatalError("We were supposed to return a keyPath for choice")
}
func test() {
backTees = true
option = 2
print("Before: \(self.colorHcpBackTees2)")
// Now update the correct property using the choice KeyPath
self[keyPath: choice] = .Azul
print("After: \(self.colorHcpBackTees2)")
backTees = false
option = 1
// Assign it to another variable, just to show you can
let choiceFront1 = choice
option = 2
// choiceFront1 still refers to !backTees and option 1
// even though option and choice have changed
print("colorHcpFrontTees1 = \(self[keyPath: choiceFront1])")
colorHcpFrontTees1 = .Verde
print("colorHcpFrontTees1 = \(self[keyPath: choiceFront1])")
}
}
Run the test:
Foo().test()
Output:
Before: Blanca
After: Azul
colorHcpFrontTees1 = Roja
colorHcpFrontTees1 = Verde
To clean the code you could wrap the logic for initialising the properties within the enum itself.
extension ColoresDeSalidas {
init?(isBackTee: Bool, option: Int) {
switch (isBackTee, option) {
case (true, 1): self = .colorHcpBackTees1
case (true, 2(: self = .colorHcpBackTees2
case (false, 1): self = .colorHcpFrontTees1
case (false, 2): self = .colorHcpFrontTees2
default: return nil //or assign a default and make non-failable
}
And then you can initialise / update with:
let myVar = ColoresDeSalidas(isBackTee: true, option: 1)

Swift : affect struct by reference or "alias" to a variable

I have to update a struct, but this struct needs a long expression to be found from my viewController :
let theMessage:Message? = self.messageSections.first(where: { $0.date == DateDMY(fromNSDate:msg.date) })?
.msgs.first(where: { $0 == msg })
I want to mutate one property of this struct, but not a copy of it, I want to update it "where it is", that is in messageSections[].msgs[]
The problem here is that if I try this code after the one above :
theMessage.status = .NOT_SENT
Only the local copy will be updated, and not the real one in messageSections[].msgs[]. So when I reload my collectionView, nothing changes.
I could do
self.messageSections.first(where: { $0.date == DateDMY(fromNSDate:msg.date) })?
.msgs.first(where: { $0 == msg }).status = .NOT_SENT
But if I have to manipulate this property several times in my function, I dont want to re-write the whole expression each time. So what I'm looking for is something like in C :
let theMessage:Message?* = &self.messageSections.first(where: { $0.date == DateDMY(fromNSDate:msg.date) })?
.msgs.first(where: { $0 == msg })
if(theMessage != NULL)
theMessage->status = .NOT_SENT
I saw in other questions that I could use & before argument and inout before parameter name, but its only for functions calling. I'm looking for a way to create an alias for a big expression when affecting a variable, just to avoid re-writting it each time.
You can use firstIndex to get the index of the element in the array, then access the element directly using the retrieved index.
if let messageSectionIndex = self.messageSections.first(where: { $0.date == DateDMY(fromNSDate:msg.date) }), let messageIndex = self.messageSections[messageSectionIndex].msgs.firstIndex(where: { $0 == msg }) {
self.messageSections[messageSectionIndex].msgs[messageIndex].status = .NOT_SENT
}

How to identify the type of an object?

Here is my JSON response for a particular API.
Case 1
ChallengeConfiguration = {
AnswerAttemptsAllowed = 0;
ApplicantChallengeId = 872934636;
ApplicantId = 30320480;
CorrectAnswersNeeded = 0;
MultiChoiceQuestion = (
{
FullQuestionText = "From the following list, select one of your current or previous employers.";
QuestionId = 35666244;
SequenceNumber = 1;
},
{
FullQuestionText = "What color is/was your 2010 Pontiac Grand Prix?";
QuestionId = 35666246;
SequenceNumber = 2;
}
)
}
The key "MultiChoiceQuestion" returns an array with two questions. So here is my code.
let QuestionArray:NSArray = dict1.objectForKey("ChallengeConfiguration")?.objectForKey("MultiChoiceQuestion") as! NSArray
Case 2
ChallengeConfiguration =
{
AnswerAttemptsAllowed = 0;
ApplicantChallengeId = 872934636;
ApplicantId = 30320480;
CorrectAnswersNeeded = 0;
MultiChoiceQuestion = {
FullQuestionText = "From the following list, select one of your
current or previous employers.";
QuestionId = 35666244;
SequenceNumber = 1;
}
}
For Case 2 my code does not work and app crashes because it returns a dictionary for that specific Key. So how could I write a generic code that would work for all objects?
It looks like the key can contain either an array of dictionary values or a dictionary, so you just need to try casting to see which one you have.
so I would likely do it like this:
if let arr = dict1.objectForKey("ChallengeConfiguration")?.objectForKey("MultiChoiceQuestion") as? Array {
// parse multiple items as an array
} else if let arr = dict1.objectForKey("ChallengeConfiguration")?.objectForKey("MultiChoiceQuestion") as? [String:AnyObject] {
// parse single item from dictionary
}
You should never really use ! to force unwrap something unless you are completely certain that the value exists and is of the type you are expecting.
Use conditional logic here to test the response and parse it safely so that your app doesn't crash, even in failure.

Code for modify DB?

I have no idea what kind of language of programming is this, or what the method does, any help?
It could be some code for modify some DB tables?
ClassMethod CreateNewConfiguration(pintInsTypeConf As %Integer) As %Integer
{
Set objRecord = ##class(Table.tInsTypeConfigurations).%OpenId(pintInsTypeConf)
Set objNewRecord = ##class(Table.tInsTypeConfigurations).%New()
Set objClassDef = ##class(%Dictionary.ClassDefinition).%OpenId("Table.tInsTypeConfigurations")
Set intTotal = objClassDef.Properties.Count()
For intCount = 1:1:intTotal
{
If (((objClassDef.Properties.GetAt(intCount).Relationship = 0) &&
(objClassDef.Properties.GetAt(intCount).Calculated = 0)) ||
((objClassDef.Properties.GetAt(intCount).Relationship = 1) &&
(objClassDef.Properties.GetAt(intCount).Cardinality = "one")))
{
Set strName = objClassDef.Properties.GetAt(intCount).Name
Set $zobjproperty(objNewRecord,strName) = $zobjproperty(objRecord,strName)
}
}
Set objNewRecord.Name = objNewRecord.rInstrumentTypes.%Id() _ " Config B “
Set intResult = objNewRecord.%Save()
If ((intResult '= 1) || ($ZERROR '= ""))
{
Quit 0
}
Quit objNewRecord.%Id()
}
yes, #duskwuff is right, it is Caché ObjectScript code.
And in this code, just make a copy properties for some object, from class Table.tInsTypeConfigurations with id pintInsTypeConf, to new object. It is not optimized code, but anyway, it should does this task.

CoffeeScript: Create a shallow copy of an object and rename/drop properties with a one-liner

Given:
externalObject = {
UglyKeyOne: 'val1'
UglyKeyTwo: 'val2'
UglyUnusedKey: 'boo'
}
Is there a way to do the below 2 lines, in a one liner? i.e. to create newObject, and use the destructuring assignment in one step?
{ UglyKeyOne: keyOne, UglyKeyTwo: keyTwo } = externalObject
newObject = { keyOne, keyTwo }
This works:
newObject = { keyOne, keyTwo } = { keyOne: externalObject.UglyKeyOne, keyTwo: externalObject.UglyKeyTwo }