question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Object members switch

See original GitHub issue

When using metamodel, for object information extraction, like member attributes or member functions. It would be nice to have possibility to switch over this extracted values.

For example I would like to create mapping between two representation of the same domain model, a DTO and database model, for some reason names of attributes does not match.

class PersonDb(Integer id,shared String personName,shared Integer personAge){}
class PersonDto(shared String name,shared Integer age ){}

Currently the simplest form of mapping between those two attributes I can make, look like:

Attribute<PersonDto>? mapAttributes(Attribute<PersonDb> dbAttribute) =>
	if(dbAttribute==`PersonDb.personAge`) then `PersonDto.age` 
	else if(dbAttribute==`PersonDb.personName`) then `PersonDto.name`
	else null;

And it would be nice to have something like this:

Attribute<PersonDto>? mapSwitchAttributes(Attribute<PersonDb> dbAttribute) =>
	switch(dbAttribute)
	case(is `PersonDb.personAge`){
	    return `PersonDto.age`;
	}
	case(is `PersonDb.personName`){
	    return `PersonDto.name`;
	}else{
	    return null;
	}

Of course with exhausting requirement “Case types must cover all cases of the switch type or an else clause must appear”

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
gavinkingcommented, Mar 6, 2019

No, it’s not possible today, but in principle it would be pretty simple to add.

1reaction
xkr47commented, Mar 4, 2019

Some ideas…

  • use a map somehow:
value memberMap = map {
  `PersonDb.personAge` -> `PersonDto.age`,
  `PersonDb.personName` -> `PersonDto.name`
};
  • use annotations on members in either PersonDb or PersonDto like:
class PersonDb(
  Integer id,
  mapToDto(`PersonDto.name`) shared String personName,
  mapToDto(`PersonDto.age`) shared Integer personAge
){}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I check for object properties in a switch statement?
Simple answer is no, it is not supported,. As a workaround you may try to use following form of switch: switch (true) {...
Read more >
Switch case with an object - JavaScript - SitePoint Forums
Hi All, I need some help with JS which is a part of a sigma grid ajax function. Here's an array with variable...
Read more >
Working with objects - JavaScript - MDN Web Docs - Mozilla
An object is a collection of properties, and a property is an association between a name (or key) and a value. A property's...
Read more >
Replacing switch statements with Object literals
Let's set up a simple Object literal that returns a String value only. We might, however, need more complex code than a String...
Read more >
Switch | Object Players Official Wiki | Fandom
Switch is a handheld game console, He has a big black box with a grey screen. He also has two blue and red...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found