EMF: Defining a generic containment reference in an Ecore metamodel - eclipse-emf

It is a long time since I have used EMF and I am stuck on this.
I would like to create a generic type equivalent to:
class Result<T:ASTNode>{
T root;
}
I am defining this in Kotlin:
val result = ePackage.createEClass("Result").apply {
// I think this part is correct
val typeParameter = EcoreFactory.eINSTANCE.createETypeParameter().apply {
this.name = "T"
this.eBounds.add(EcoreFactory.eINSTANCE.createEGenericType().apply {
// astNode is my EClass
this.eClassifier = astNode
})
}
this.eTypeParameters.add(typeParameter)
val rootContainment = EcoreFactory.eINSTANCE.createEReference()
rootContainment.name = "root"
// STUCK!
// here should I set rootContainment.eType? rootContainment.eGenericType?
// how?
rootContainment.isContainment = true
rootContainment.lowerBound = 0
rootContainment.upperBound = 1
this.eStructuralFeatures.add(rootContainment)
addContainment("issues", issue, 0, -1)
}

The equivalent .ecore is :
<eClassifiers xsi:type="ecore:EClass" name="Result">
<eTypeParameters name="T">
<eBounds eClassifier="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
</eTypeParameters>
<eStructuralFeatures xsi:type="ecore:EReference" name="t">
<eGenericType eTypeParameter="#//Result/T"/>
</eStructuralFeatures>
so you want to use rootContainment.eGenericType with a new EGenericType that references your ETypeParameter

Related

Dart print multiple list item at once

How can I type something like "print(list[1,4]);" in Dart?
For example:
int main() {
var products = new List(5);
products[0] = "Laptop";
products[1] = "Mouse";
products[2] = "Keyboard";
products[3] = "Monitor";
products[4] = "Microphone";
print(products[1]); // Mouse
print(products[1,3]); // I want to see 'Mouse,Monitor'
}
This is not directly supported in the SDK but you can easily make a extension on e.g. List to add this feature:
void main() {
final products = List<String>(5);
products[0] = "Laptop";
products[1] = "Mouse";
products[2] = "Keyboard";
products[3] = "Monitor";
products[4] = "Microphone";
print(products[1]); // Mouse
print(products.selectMultiple([1,3]).join(',')); // Mouse,Monitor
}
extension MultiSelectListExtension<E> on List<E> {
Iterable<E> selectMultiple(Iterable<int> indexes) sync* {
for (final index in indexes) {
yield this[index];
}
}
}
You can't make it so [1,3] (as in you own example) would be valid since the [] operator does only allow one argument. So instead, we need to make a method which takes our requested indexes as argument.

Accessing different properties in a typescript union type

I'm creating a function that handles objects from the database. I have two different data structures where the same property has a different name. I can't change that, so I have to handle it in JavaScript.
The objects have other differences, but that's not important to this function.
I want to use the same function for two different types of objects. Here's sample code demonstrating my problem:
interface TypeA {
itemName: string;
}
interface TypeB {
itemTitle: string;
}
function getItemName(item: TypeA | TypeB): string {
let name = '';
if (item.hasOwnProperty('itemName')) {
name = item.itemName;
} else {
name = item.itemTitle;
}
return name;
}
Of course, this code runs. But the IDE marks both the lines name = item.itemName; and name = item.itemTitle; as errors ("Property does not exist on type"), because both types do not have both properties.
So, what's the proper typescript way to do this?
You need to create a User Defined Type Guard, then you can use an if statement and get the correct typing.
function isTypeA(value: TypeA | TypeB): value is TypeA {
return value.hasOwnProperty('itemName');
}
Then you can get the typing much cleaner:
function getItemName(item: TypeA | TypeB): string {
return isTypeA(item) ? item.itemName : item.itemTitle;
}
Check it out here. Item is correctly cast to either TypeA or TypeB.
I might be a little bit late, but you could give this a try inside your function:
if ('itemName' in item) {
name = item.itemName;
} else {
name = item.itemTitle;
}
you can make a type assertion if you don't do this too often :
if (item.hasOwnProperty('itemName')) {
name = (item as TypeA).itemName;
} else {
name = (item as TypeB).itemTitle;
}
or
if (item.hasOwnProperty('itemName')) {
name = (<TypeA>item).itemName;
} else {
name = (<TypeB>item).itemTitle;
}
if you need to make this check more than once or twice, you'd better writing a type guard as #Daryl suggests.
interface TypeA {
a: string
}
interface TypeB {
b: string
}
const testFunction = (x: TypeA | TypeB): string => {
return (x as TypeA).a || (x as TypeB).b;
}
testFunction({ a: 'Hello' }); // 'Hello'
testFunction({ b: 'World' }); // 'World'
Intellij accepts this syntax:
function getItemName(item: TypeA): string;
function getItemName(item: TypeB): string;
function getItemName(item): string {
return item.hasOwnProperty('itemName') ? item.itemName : item.itemTitle;
}
the official way according to the typescript docs is this:
https://www.typescriptlang.org/docs/handbook/functions.html
I won't complicate things. If you're really sure that your object has either the one or the other property, a name = item['itemName'] || item['itemTitle'] or name = item.hasOwnProperty('itemName') ? item['itemName'] : item['itemTitle'] would be sufficient.
Note that TypeScript usually stops complaining if you access properties using the bracket notation instead of the dot notation. I would suggest adding a comment, though.
Use typeguards:
interface TypeA {
itemName: string;
}
interface TypeB {
itemTitle: string;
}
function isTypeA(val: any): val is TypeA
{
return val.hasOwnProperty('itemName');
}
function isTypeB(val: any): val is TypeB
{
return val.hasOwnProperty('itemTitle');
}
function getItemName(item: TypeA | TypeB): string
{
let name = '';
if (isTypeA(item))
{
name = item.itemName;
}
else
{
name = item.itemTitle;
}
return name;
}

PHPUnit: how to mock multiple method calls with multiple arguments and no straight order?

I need to test following function:
[...]
public function createService(ServiceLocatorInterface $serviceManager)
{
$firstService = $serviceManager->get('FirstServiceKey');
$secondService = $serviceManager->get('SecondServiceKey');
return new SnazzyService($firstService, $secondService);
}
[...]
I know, I might test it this way:
class MyTest extends \PHPUnit_Framework_TestCase
{
public function testReturnValue()
{
$firstServiceMock = $this->createMock(FirstServiceInterface::class);
$secondServiceMock = $this->createMock(SecondServiceInterface::class);
$serviceManagerMock = $this->createMock(ServiceLocatorInterface::class);
$serviceManagerMock->expects($this->at(0))
->method('get')
->with('FirstServiceKey')
->will($this->returnValue($firstService));
$serviceManagerMock->expects($this->at(1))
->method('get')
->with('SecondServiceKey')
->will($this->returnValue($secondServiceMock));
$serviceFactory = new ServiceFactory($serviceManagerMock);
$result = $serviceFactory->createService();
}
[...]
or
[...]
public function testReturnValue()
{
$firstServiceMock = $this->createMock(FirstServiceInterface::class);
$secondServiceMock = $this->createMock(SecondServiceInterface::class);
$serviceManagerMock = $this->createMock(ServiceLocatorInterface::class);
$serviceManagerMock->expects($this->any())
->method('get')
->withConsecutive(
['FirstServiceKey'],
['SecondServiceKey'],
)
->willReturnOnConsecutiveCalls(
$this->returnValue($firstService),
$this->returnValue($secondServiceMock)
);
$serviceFactory = new ServiceFactory($serviceManagerMock);
$result = $serviceFactory->createService();
}
[...]
Both workes fine, but if I swap the ->get(xxx) lines in the createService function, both tests will fail.
So, how do I have to change the testcases which doesn't need a specific sequenz for the parameters 'FirstServiceKey', 'SecondServiceKey, ...
You can try with willReturnCallback or willReturnMap strategy, as example of willReturnCallback:
public function testReturnValue()
{
$firstServiceMock = $this->createMock(FirstServiceInterface::class);
$secondServiceMock = $this->createMock(SecondServiceInterface::class);
$serviceManagerMock = $this->createMock(ServiceLocatorInterface::class);
$serviceManagerMock->expects($this->any())
->method('get')
->willReturnCallback(
function ($key) use($firstServiceMock, $secondServiceMock) {
if ($key == 'FirstServiceKey') {
return $firstServiceMock;
}
if ($key == 'SecondServiceKey') {
return $secondServiceMock;
}
throw new \InvalidArgumentException; // or simply return;
}
);
$serviceFactory = new ServiceFactory($serviceManagerMock);
$result = $serviceFactory->createService();
}
Hope this help
It's been a while and so there is a new option to solve this: Change to 'Prophets'
public function testReturnValue()
{
$this->container = $this->prophesize(ServiceLocatorInterface::class);
$firstService = $this->prophesize(FirstServiceInterface::class);
$secondService = $this->prophesize(SecondServiceKey::class);
$this->container->get('FirstServiceKey')->willReturn(firstService);
// And if you like
$this->container->get('FirstServiceKey')->shouldBeCalled();
$this->container->get('SecondServiceKey')->willReturn(secondService);
[...]
$serviceFactory = new ServiceFactory();
/*
* Little error in my example. ServiceLocatorInterface is parameter of
* createService() and not the constructor ;) (ZF2/3)
*/
$result = $serviceFactory->createService(
$this->container->reveal()
);
[...]
}
and now, it doesn't matter, in which sequence you'll make the '$serviceManager->get([...]);' calls \o/

Convert a string into closure in ColdFusion

I have a very basic question. Is it possible to convert a string into a closure? I tried evaluate() but it didn't work.
evaluate( "myFunction = function(val){ return dollarFormat( val ); }" );
What I have in mind is to save custom functions in the database as string and then run it as needed.
Thank you!
Edit: Just to clarify: I want to be able to save "function(val){ return dollarFormat( val ); }" as a string in database and be able to convert it into a functioning closure.
I would go with user2943775 answer:
<cfscript>
FileWrite("/ram/UDFs.cfm", "<cfset myFunction = function(val){ return dollarFormat( val ); }>")
include template="/ram/UDFs.cfm";
writedump(myFunction(10));
</cfscript>
And in your Application.cfc
component {
this.mappings["/ram"] = "ram://";
...
}
I came across a similar solution, though I was unable to use the in-memory filesystem due to security restrictions. In my Application.cfc, I added the following mapping:
this.mappings = {
"/models" = "#APP_ROOT_PATH#cfcs/models",
"/utils" = "#APP_ROOT_PATH#cfcs/utils",
"/modules" = "#APP_ROOT_PATH#_modules",
"/components" = "#APP_ROOT_PATH#cfcs",
"/udfs" = "#APP_ROOT_PATH#includes/udfs" // path for global (and temporary) UDFs
};
The UDF I created is as follows:
/**
* Takes a string representation of a function and returns it as a Closure
* #output false
* #return Closure
*/
private any function toClosure (required string closure) {
local.id = replace(createUUID(), "-", "", "all");
local.udfpath = "/udfs/udf#id#.cfm";
local.script = "<cfscript>local.fn#id# = #closure#;</cfscript>";
try {
fileWrite(expandPath(udfPath), script);
include udfpath;
} catch (any e) {
} finally {
try {
fileDelete(expandPath(udfPath));
} catch (any e) {}
}
if (!structkeyExists(local, "fn#id#") || !isClosure(local["fn#id#"])) {
throw (message="Unable to compile closure");
}
// return the closure
return local["fn#id#"];
}
And the result:
myFn = toClosure("function (num) { return num + 1; }");
myFn(1); // returns 2

How to update Target object from markup extension in SL5?

I'm trying this code in a markup extension in Silverlight 5.
public override object ProvideValue(IServiceProvider serviceProvider)
{
_target = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));
_depObj = _target.TargetObject as DependencyObject;
_depProp = _target.TargetProperty as DependencyProperty;
return GetText(TextId, DefaultText);
}
depObj seems to be provided correctly, however depProp seems not to be of type
DependencyProperty (The type is Sytem.String (of System.Reflection.PropertyInfo)). The cast results in null.
depProp is representing to the right Text or Content property, but without it being a
DependencyProperty I cannot set its value.
Any input greatly appreciated.
SiKo
Not sure why its coming up as a PropertyInfo but you can of course set the value with code something like:
if (TargetProperty is DependencyProperty)
{
var dependencyObject = target as DependencyObject;
if (dependencyObject != null)
{
var newValue = GetValue();
dependencyObject.SetValue(TargetProperty as DependencyProperty, newValue);
}
}
else if (TargetProperty is PropertyInfo)
{
var pi = target as PropertyInfo;
pi.SetValue(target, GetValue(), null);
}
The approach #Berryl gave above could work. But if you also need to get to the DependencyProperty, you can use something like this. This code also demonstrates the subtle differences between silverlight and WPF, but both can work great.
For silverlight or WPF only, you can remove the parts before or after the #else to simplify the code a little bit.
But the part that resolves the dependency property is in the middle (assigning _property).
public override object ProvideValue(IServiceProvider serviceProvider)
{
IProvideValueTarget Target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
if (Target == null) {
throw new InvalidOperationException(
"Cannot resolve the IProvideValueTarget. Are you binding to a property?");
}
/* we need the dependency property (for bindings) */
#if SILVERLIGHT
PropertyInfo Property = (PropertyInfo)Target.TargetProperty;
/* resolve the dependency property */
FieldInfo DepPropertyInfo = Target.TargetObject.GetType()
.GetField(Property.Name + "Property", BindingFlags.Static | BindingFlags.Public);
_property = (DependencyProperty)DepPropertyInfo.GetValue(/* obj = */ null);
#else
_property = Target.TargetProperty as DependencyProperty;
#endif
if (_property != null) {
#if SILVERLIGHT
if (Property.Name.StartsWith("Text")) {
UpdateSourceTrigger = UpdateSourceTrigger.Default;
#else
if (_property.Name.StartsWith("Text")) {
UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
#endif
}
else {
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
}
}