Interpolation and templating - templating

I am trying to verify my understanding of string interpolation and string templating.
Is it correct to say that the two Java code snippets are examples of templating?
public class Person {
//showing only relevant code
public String toString() {
return "Name: " + name + " salary: " + salary + " address: " + address;
}
}
public String toString() {
return String.format("name: %s salary: %d address: %s", name, salary, address);
}
This Groovy snippet is an example of string interpolation:
public class Person {
def name
def salary
def address
//showing only relevant parts of the code
public String toString() {
return """ name: ${name} salary: ${salary} address: ${address}"""
}
}
Would it be correct to say that Java and Python support templating but no interpolation, but Groovy does support interpolation.

Related

Can required parameters in a Dart constructor be named?

I am working with some Dart code for a Flutter/Dart class I'm taking. I expected the following code to compile, but it did not:
class Person {
String? name;
int? age;
Person(this.name, this.age);
#override
String toString() {
return "name: $name\nage: $age";
}
}
void main() {
final person = Person(name: 'Joe', age: 30);
print(person);
}
When I made the constructor parameters optional, as below, it does compile:
class Person {
String? name;
int? age;
Person({this.name, this.age});
#override
String toString() {
return "name: $name\nage: $age";
}
}
void main() {
final person = Person(name: 'Joe', age: 30);
print(person);
}
I tried searching the Flutter dev docs for a reason why this is so, or a way to have required parameters with their names in a constructor, but I didn't find anything. I can certainly imagine cases where I would want required constructor parameters to have names.
My pubspec.yaml specifies the following:
environment: sdk: ">=2.12.0 <3.0.0"
Your first example uses what are called "positional parameters" in dart. You cannot call a positional parameter with a name label, which is why the first example does not compile.
The second example uses "named parameters". Any parameter defined within {} is considered a named parameter and must be called using a name label. As explained in the dart language tour, named parameters are optional unless they’re explicitly marked as required.
So simply add the required keyword before any named parameter you want to require.
class Person {
String? name;
int? age;
Person({required this.name, required this.age});
#override
String toString() {
return "name: $name\nage: $age";
}
}
void main() {
final person = Person(name: 'Joe', age: 30);
print(person);
}

Spring Cloud Dataflow UI Not Rendering Whitelisted Properties

I have Spring Cloud DataFlow v1.3.1.RELEASE running locally, and I've created a small sample 'processor' app to illustrate what I see happening.
The Boot application has two #ConfigurationProperties classes:
DemoApplicationProperties:
#ConfigurationProperties
#Validated
public class DemoApplicationProperties {
/**
* The first name of the person.
*/
private String firstName;
/**
* The last name of the person.
*/
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
and DemoApplicationPropertiesTwo:
#ConfigurationProperties
#Validated
public class DemoApplicationPropertiesTwo {
/**
* The person's middle name.
*/
private String middleName;
/**
* The date of birth.
*/
private String birthdate;
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getBirthdate() {
return birthdate;
}
public void setBirthdate(String birthdate) {
this.birthdate = birthdate;
}
}
I also include a unit test to make sure the BootApplicationConfigurationMetadataResolver is resolving all the whitelisted classes appropriately.
public class WhiteListTests {
private BootApplicationConfigurationMetadataResolver metadataResolver;
#Test
public void testMetadataResolver() {
metadataResolver = new BootApplicationConfigurationMetadataResolver(this.getClass().getClassLoader());
Resource app = new FileSystemResource(".\\target\\classes\\");
List<ConfigurationMetadataProperty> list = metadataResolver.listProperties(app);
for(ConfigurationMetadataProperty listItem : list) {
StringBuilder sb = new StringBuilder();
sb.append(listItem.getId() + ": " + listItem.getName() + " :: " + listItem.getType());
System.out.println(sb.toString());
}
}
}
The output of the unit test is as expected:
birthdate: birthdate :: java.lang.String
middle-name: middle-name :: java.lang.String
first-name: first-name :: java.lang.String
last-name: last-name :: java.lang.String
However, when I register the Boot application as a 'processor' in Spring Cloud Dataflow, and inspect the registered application, the UI only partially renders the discovered whitelisted properties:
I have a ZIP file of the project source code, but for whatever reason, cannot figure out how to attach it here.
Inside the file spring-configuration-metadata-whitelist.properties did you add the two classes in the property ?
Example
configuration.classes = org.springframework.cloud.stream.app.file.sink.FileSinkProperties
and
com.anotherpackage.MainConfig.java
Both the properties class must be declared in the spring-configuration-metadata-whitelist.properties file. Shell, Dashboard, and REST endpoints should then be able to produce the results consistently.
Here's the same example in action.

strings not showing in console

For some reason this isn't showing what I write in the console - It's not printing in the console. I may be getting confused with the static thing. Also, how come the getUserInformation() method is a void when it is returning information from the console to the variables declared? Thanks
namespace Student_Information
{
class Program
{
static void Main(string[] args)
{
string firstName = string.Empty, lastName = string.Empty, birthday = string.Empty;
getUserInformation();
printStudentDetails(firstName, lastName, birthday);
}
static void getUserInformation()
{
Console.WriteLine("Enter the student's first name: ");
string firstName = Console.ReadLine();
Console.WriteLine("Enter the student's last name");
string lastName = Console.ReadLine();
Console.WriteLine("Enter your bithdate");
string birthday = Console.ReadLine();
}
static void printStudentDetails(string firstName, string lastName, string birthday)
{
Console.WriteLine("{0} {1} was born on: {2}", firstName, lastName, birthday);
Console.ReadLine();
}
}
}
Use
Console.WriteLine(firstName+" "+lastName+" was born on: "+birthday);

why are my test pages being ignored?

I have followed a couple of different tutorials for setting up a simple slim fitnesse environment with .NET. I have attempted this with both fitsharp and netrunner but both end up in my test page being ignore. Everything imports fine and even running RunnerW.exe provides nothing. I have checked numerous times and all of my paths are correct. In the end, when running the test, all I get are what is displayed in the screenshots below. I have been struggling with this for a few hours now so any help would be greatly appreciated.
NetRunner:
Result: http://screencast.com/t/mBdkCyGow
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NetRunner;
using NetRunner.ExternalLibrary;
class Employee : BaseTestContainer
{
private string firstName;
private string lastName;
private string number;
public Employee() { }
public Employee(string firstName, string lastName, string number)
{
this.firstName = firstName;
this.lastName = lastName;
this.number = number;
}
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public string Name
{
get { return firstName + " " + lastName; }
}
public string Number
{
get { return number; }
set { number = value; }
}
}
Fitsharp:
Result: http://screencast.com/t/GMqdgwxA6
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using fit;
class Employee : ColumnFixture
{
private string firstName;
private string lastName;
private string number;
public Employee() { }
public Employee(string firstName, string lastName, string number)
{
this.firstName = firstName;
this.lastName = lastName;
this.number = number;
}
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public string Name
{
get { return firstName + " " + lastName; }
}
public string Number
{
get { return number; }
set { number = value; }
}
}
You're using a fixture from the Fit test system, ColumnFixture, with the Slim test system: !define TEST_SYSTEM {slim}
If you want to use ColumnFixture, then !define TEST_SYSTEM {fit}
If you want to use Slim, use the Decision table: http://www.fitnesse.org/FitNesse.UserGuide.WritingAcceptanceTests.SliM.DecisionTable
Please change a little you C# code and FitNesse:
C#:
internal sealed class MyTestContainer : BaseTestContainer
{
public EmployeeArgument Employee()
{
return new EmployeeArgument();
}
}
internal sealed EmployeeArgument : BaseTableArgument
{
public void CheckName(string firstName, string lastName, out string name)
{
name = firstName + " " + lastName;
}
}
What I did:
Test Container - a class with the list of the functions which are visible by NetRunner. These and only these functions are used for testing. You can create any count of the such classes, all function lists will be unioned
I created function Employee (the first row of FitNesse table). This name is the same with first table row. You can also use attributes to have different function names in code and in FitNesse.
The BaseTableArgument - specific type to execute the same function on the each table row.
Function CheckName uses out parameter for result checking.
FitNesse changes:
| '''employee''' |
| '''First Name''' | '''Last Name''' | '''Name''' |
| Ryan | Cheek | Ryan Cheek |
| Ryan | Cheek | abc |
What I did:
Important: add fit mode to the top of the FitNesse text: !define TEST_SYSTEM {fit}
First row is name of function. It should be bold (three ' before and after text). The bold text is meta-data (function names, parameter names, etc.). Non-bold text is related to variables.
Second row - list of the parameter names (or property names, see here the same example). NetRunner will find function with the same parameter list.
Next rows: input and output values. I used string type as the simplest way, however you can use any input/output types.

Three domain classes relationship in GORM

is there a special way with gorm to map a three domain classes relationship like this:
1 person belongs to N companies with M given roles (one or more roles for a given company)
Thanks in advance.
look at http://www.grails.org/Many-to-Many+Mapping+without+Hibernate+XML (i think it's up to date).
be aware of: http://codedumpblog.blogspot.com/2010/02/grails-many-to-many-with-lists.html
the code below works for me using grails 1.2.0. but it seems like i had to do a lot of save()'s. don't forget to make the controllers and set scaffold=domain_class
package p
class Company {
String toString() { "$name"
}
static hasMany=[roles:Role]
static constraints = {
}
String name
}
package p
class Role {
String toString() { "$name"
}
static belongsTo=[company:Company]
static hasMany=[personRoleAssociations:PersonRoleAssociation]
static constraints = {
}
String name
}
package p
class Person {
String toString() { "$name"
}
static hasMany=[personRoleAssociations:PersonRoleAssociation]
static constraints = {
}
String name
}
package p
class PersonRoleAssociation {
String toString() { "${person.name} as ${role.name}"
}
static belongsTo=[person:Person,role:Role]
static constraints = {
}
}
import p.*
class BootStrap {
def init = { servletContext ->
Person dick=new Person(name:'dick')
Person jane=new Person(name:'jane')
dick.save()
jane.save()
Company ibm=new Company(name:'ibm')
ibm.save()
Role ibmManager=new Role(name:'ibmmanager')
Role ibmPeon=new Role(name:'ibmpeon')
ibm.addToRoles(ibmManager)
ibmManager.save()
ibm.addToRoles(ibmPeon)
ibmPeon.save()
ibm.save()
Company sun=new Company(name:'sun')
sun.save()
Role sunManager=new Role(name:'sunmanager')
Role sunPeon=new Role(name:'sunpeon')
sun.addToRoles(sunManager)
sunManager.save()
sun.addToRoles(sunPeon)
sunPeon.save()
sun.save()
PersonRoleAssociation dickManager=new PersonRoleAssociation()
dick.addToPersonRoleAssociations(dickManager)
ibmManager.addToPersonRoleAssociations(dickManager)
PersonRoleAssociation dickPeon=new PersonRoleAssociation()
dick.addToPersonRoleAssociations(dickPeon)
sunPeon.addToPersonRoleAssociations(dickPeon)
PersonRoleAssociation janeManager=new PersonRoleAssociation()
jane.addToPersonRoleAssociations(janeManager)
sunManager.addToPersonRoleAssociations(janeManager)
PersonRoleAssociation janePeon=new PersonRoleAssociation()
jane.addToPersonRoleAssociations(janePeon)
ibmPeon.addToPersonRoleAssociations(janePeon)
}
def destroy = {
}
}
I would try :
class Person {
String name
Set<Role> roles
Set<Company> companies
public String toString() {
return name + " roles : " + (roles.collect { it.name }).toString() + " - companies : " + (companies.collect { it.name }).toString()
}
static hasMany = [companies:Company, roles:Role]
static constraints = {
name (unique:true)
roles (nullable:true)
}
}
class Role {
String name
String toString() {
return name + " companies : " + (companies.collect { it.name }).toString()
}
static hasMany = [companies : Company]
static belongsTo = Company
static constraints = {
name (unique:true)
companies (nullable:false)
}
}
class Company {
String name
String toString() {
return name + " roles : " + (roles.collect { it.name }).toString()
}
static hasMany = [roles : Role]
static constraints = {
name (unique:true)
roles (nullable:true)
}
}
Didn't test though... I'd be interested in knowing if my solution has any problems and what they can be...

Resources