Is it possible with MapStruct 1.2 to map a source property with a specific value to a specific different value in the target?
I think about something like this:
public abstract class JiraKpmMapper {
#Mappings({
#Mapping(source = "mySource.propA", target = "myTarget.propX")
})
#ValueMappings({
#ValueMapping(source = "ABC", target = "XYZ"),
#ValueMapping(source = "123", target = "789")
})
public abstract MyTarget source2Target(final MySource mySource);
}
So that when MapStruct sees during the mapping that when mySource.propA has the value "ABC" myTarget.propX needs to be set to value "XYZ" and so forth.
To be more precisely, I even want something more elaborated:
The target should be a class haven three properties in which the resulting target value must be split into.
For instance, if mySource.propA has the value "ABC" the target myTarget should get a value like "V01.123.456.AB". This value in turn shall be split up in a preValue, a middleValue and an endValue:
preValue = "V01"
middleValue = "123.456"
endValue = "AB"
whereby there's no property holding the complete result string.
That's why I already wrote a custom mapper and I tell the MyMapper to use it via
#Mapper(componentModel = "spring", uses = MyCustomMapper.class)
This works so far but I can't achieve it to tell the MyCustomMapper to put "V01.123.456.AB" into the target when the souzrce comes with "ABC".
You can't really do that with MapStruct. The #ValueMapping annotation is for mapping of Enum(s).
In order to achieve what you are looking for you would need to do that in #BeforeMapping or #AfterMapping.
For example you can do something like:
#Mapper
public interface JiraKpmMapper {
#BeforeMapping
default void beforeMapping(#MappingTarget MyTarget target, MySource source) {
if (source.getPropY().equals("ABC") {
target.setPropX("V01.123.456.AB");
}
}
#Mapping(target = "propX", ignore = true) // This is now mapped in beforeMapping
MyTarget source2Target(final MySource mySource);
}
Your custom mapper then should have an #AfterMapping. Where you would convert the propX into your class. You can even do this as part of the #BeforeMapping I wrote and directly create your class (or invoke the method that does the conversion from String into a class)
you can do something like this
#Mapping(target = "myTarget.propX",expression="java(mySource.getPropA().equals(\"Abc\")?"\"V01.123.456.AB\":\"\")")
Related
In JavaScript you can do the following:
var obj = {
property: 1,
method1: function() {
//...
},
method2: function() {
//...
}
};
obj.method1()
I am wondering if there is a groovy equivalent for this (a map containing a method). I know this is just like a class, but I dont want a class ha..
Yes, you can put closures inside a map. But this is not the way to get
objects in Groovy. There is no concept of "this", that knows about the
map.
def obj = [
inc: { it + 1 }
]
println obj.inc(10)
Ok so Javascript is not OOP. They have OBJECTS but that is it. What you are showing is an OBJECT.
In Groovy, you can do this with a class that can instantiate the object and then you can do that on the object. For example you can create a CommandObject (which is what you are probably wanting) and then fill in the properties like you want or fill them in on instantiation. For example (using above example):
def paramsDesc = new ParamsDescriptor()
paramsDesc.paramType = 'paramtype'
paramsDesc.keyType = 'keyType'
paramsDesc.name = 'name'
paramsDesc.idReferences = 'id'
paramsDesc.description = 'desc'
paramsDesc.mockData = 'mock'
paramsDesc.values = []
OR (if you create a constructor) you can instantiate all at once:
def paramsDesc = new ParamsDescriptor('paramtype','keyType','name','id','desc','mock',[])
CommandObjects can have methods and functions (like above). But you just have to instantiate them first (def paramsDesc = new ParamsDescriptor())
This is the difference between a class and an object; think of a class as the blueprint and the object as what is created from the blueprint.
I am writing some DXL for use as a DXL column that for each object in a module, looks at the in-links and returns the link name. Then if the link name starts with "verif", it will get the object text from an attribute "TestResultFloating" in the linked module and show it in the current module, in the DXL column.
The problem I will have when I use this on the whole database (currently I am just using a sandbox) is that some of the modules linked through the "verif" link module will not contain the "TestResultFloating" attribute. For these I would like to oppress the 'unknown Object attribute (TestResultFloating)' error and instead display something like N/A for that Object in the current module.
Below is my code that currently works as long as the "TestResultFloating" attribute is present in the linked module, but will throw the error if the attribute is not present.
ModName_ mSrc
Object o = current
Object nObject
Object oSrc, oDest
LinkRef lr = null
Link l = null
string linkname = ""
string attrbName = "TestResultFloating"
for mSrc in (obj <- "*") do {
if (!open(mSrc)) {
read(fullName(mSrc), true)
}
}
for l in (obj <- "*") do {
oSrc = source(l)
linkname = name(module(l))
string linkmodname = upper(linkname[0:4])
if(linkmodname == "VERIF") {
string objText = oSrc."TestResultFloating"
display(objText)
}
}
I tried one way of doing it which I got from the dxl reference manual which was to check whether the attribute exists and then do the operation. This is what I added but it doesn't seem to work, I still get the same error "unknown Object attribute (TestResultFloating)"
What I tried is shown below:
if(linkmodname == "VERIF") {
if(exists attribute "TestResultFloating"){
string objText = oSrc."TestResultFloating"
display(objText)
}
else {
display("N/A")
}
}
Please also note that i'm very new to DOORS and DXL so if I am doing something drastically wrong or I am asking a simple question please forgive me.
There is a utility function called string probeAttr_(Object o, string attrName) that can be used for getting an attribute value if you are not sure whether the attribute is readable or whether it even exists.
This function and a lot of similar functions tailored for different circumstances can be found in the file "c:\Program Files\IBM\Rational\DOORS\9.6\lib\dxl\utils\attrutil.inc"
I'm trying to parse a JSON data and assign it to a POJO in Grails.
I started with
obj.param=jsonRequest.jsonWrap.attrib.something.jsonParam
After some experimenting and refactoring, it looks like this now.
jsonRequest.jsonWrap.attrib.something.with {
obj.param1=jsonParam1
obj.param2=jsonParam2
//...
}
}
Now, can I avoid the repeated use of obj reference?
I'm imagining that your actual starting point is something like the following. On the JSON side:
import groovy.json.JsonSlurper
String jsonText = '''{
"jsonWrap":{
"attrib":{
"something":{
"jsonParam1": "value1",
"jsonParam2": "value2",
"jsonParam3": "value3",
"jsonParam4": "value4",
"jsonParam5": "value5"
}
}
}
}'''
def jsonRequest = new JsonSlurper().parseText(jsonText)
On the Groovy side:
class ObjectType {
def param1, param2, param3, param4, param5
}
def obj = new ObjectType()
Now, if I had any control over how either the JSON side or the Groovy side are defined then I would do my darnedest to ensure that the property names of the JSON "something" object are exactly the same as the property names in the Groovy "ObjectType" class. For example, like this:
class ObjectType {
def jsonParam1, jsonParam2, jsonParam3, jsonParam4, jsonParam5
}
Then, unmarshalling the "something" object into Groovy is as simple as this:
def obj = new ObjectType(jsonRequest.jsonWrap.attrib.something)
Only one reference to the JSON object. Only one reference to the Groovy object. And the former is used to instantiate the latter. And furthermore, notice that there is no need to reference the properties at all. That is, JSON objects from the slurper are instances of Map, so if the property names match up, you can use the default "Map constructor" syntax.
If, however, you do not control property naming in either set of objects, I would still recommend a different Map-based short-cut. First define a constant Map from one set of property names to the other, like so:
def map = [param1:"jsonParam1", param2:"jsonParam2", param3:"jsonParam3",
param4:"jsonParam4", param5:"jsonParam5"]
Then I would use something like this for the object unmarshalling:
def obj = new ObjectType().with { o ->
jsonRequest.jsonWrap.attrib.something.with { j ->
map.each { oParam, jParam -> o[oParam] = j[jParam] }
}
o
}
i don't think there is a trivial way to trick groovy into "use objectA, if getting is needed and objectB for setting". If obj above is a map or you can apply a map to this object, then you could produce a map in your with block and use this. If you have to have nested structures then more work is needed.
def jsonParam = new Expando([ p1: 'p1', p2: 'p2', p3: 'p3', ])
def obj = new Expando(
jsonParam.with{
[
param1: p1,
param3: p3,
] // `with` will return this map
})
assert obj.param1==jsonParam.p1
assert obj.param3==jsonParam.p3
I use expandos for simple code.
This query produces an error No value given for one or more required parameters:
using (var conn = new OleDbConnection("Provider=..."))
{
conn.Open();
var result = conn.Query(
"select code, name from mytable where id = ? order by name",
new { id = 1 });
}
If I change the query string to: ... where id = #id ..., I will get an error: Must declare the scalar variable "#id".
How do I construct the query string and how do I pass the parameter?
The following should work:
var result = conn.Query(
"select code, name from mytable where id = ?id? order by name",
new { id = 1 });
Important: see newer answer
In the current build, the answer to that would be "no", for two reasons:
the code attempts to filter unused parameters - and is currently removing all of them because it can't find anything like #id, :id or ?id in the sql
the code for adding values from types uses an arbitrary (well, ok: alphabetical) order for the parameters (because reflection does not make any guarantees about the order of members), making positional anonymous arguments unstable
The good news is that both of these are fixable
we can make the filtering behaviour conditional
we can detect the category of types that has a constructor that matches all the property names, and use the constructor argument positions to determine the synthetic order of the properties - anonymous types fall into this category
Making those changes to my local clone, the following now passes:
// see https://stackoverflow.com/q/18847510/23354
public void TestOleDbParameters()
{
using (var conn = new System.Data.OleDb.OleDbConnection(
Program.OleDbConnectionString))
{
var row = conn.Query("select Id = ?, Age = ?", new DynamicParameters(
new { foo = 12, bar = 23 } // these names DO NOT MATTER!!!
) { RemoveUnused = false } ).Single();
int age = row.Age;
int id = row.Id;
age.IsEqualTo(23);
id.IsEqualTo(12);
}
}
Note that I'm currently using DynamicParameters here to avoid adding even more overloads to Query / Query<T> - because this would need to be added to a considerable number of methods. Adding it to DynamicParameters solves it in one place.
I'm open to feedback before I push this - does that look usable to you?
Edit: with the addition of a funky smellsLikeOleDb (no, not a joke), we can now do this even more directly:
// see https://stackoverflow.com/q/18847510/23354
public void TestOleDbParameters()
{
using (var conn = new System.Data.OleDb.OleDbConnection(
Program.OleDbConnectionString))
{
var row = conn.Query("select Id = ?, Age = ?",
new { foo = 12, bar = 23 } // these names DO NOT MATTER!!!
).Single();
int age = row.Age;
int id = row.Id;
age.IsEqualTo(23);
id.IsEqualTo(12);
}
}
I've trialing use of Dapper within my software product which is using odbc connections (at the moment). However one day I intend to move away from odbc and use a different pattern for supporting different RDBMS products. However, my problem with solution implementation is 2 fold:
I want to write SQL code with parameters that conform to different back-ends, and so I want to be writing named parameters in my SQL now so that I don't have go back and re-do it later.
I don't want to rely on getting the order of my properties in line with my ?. This is bad. So my suggestion is to please add support for Named Parameters for odbc.
In the mean time I have hacked together a solution that allows me to do this with Dapper. Essentially I have a routine that replaces the named parameters with ? and also rebuilds the parameter object making sure the parameters are in the correct order.
However looking at the Dapper code, I can see that I've repeated some of what dapper is doing anyway, effectively it each parameter value is now visited once more than what would be necessary. This becomes more of an issue for bulk updates/inserts.
But at least it seems to work for me o.k...
I borrowed a bit of code from here to form part of my solution...
The ? for parameters was part of the solution for me, but it only works with integers, like ID. It still fails for strings because the parameter length isn't specifed.
OdbcException: ERROR [HY104] [Microsoft][ODBC Microsoft Access Driver]Invalid precision value
System.Data.Odbc. OdbcParameter.Bind(OdbcStatementHandle hstmt,
OdbcCommand command, short ordinal, CNativeBuffer parameterBuffer, bool allowReentrance)
System.Data.Odbc.OdbcParameterCollection.Bind(OdbcCommand command, CMDWrapper cmdWrapper, CNativeBuffer parameterBuffer)
System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, string method, bool needReader, object[] methodArguments, SQL_API odbcApiMethod)
System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, string method, bool needReader)
System.Data.Common.DbCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
Dapper.SqlMapper.QueryAsync(IDbConnection cnn, Type effectiveType, CommandDefinition command) in SqlMapper.Async.cs
WebAPI.DataAccess.CustomerRepository.GetByState(string state) in Repository.cs
var result = await conn.QueryAsync(sQuery, new { State = state });
WebAPI.Controllers.CustomerController.GetByState(string state) in CustomerController .cs
return await _customerRepo.GetByState(state);
For Dapper to pass string parameters to ODBC I had to specify the length.
var result = await conn.QueryAsync<Customer>(sQuery, new { State = new DbString { Value = state, IsFixedLength = true, Length = 4} });
How to add a String type field to class (i.e to all instances of that class), like grails add an id and version fields to all domain classes? If it is possible, how to specify the type of field as String / Long etc
EDIT:
The added fields are not updated in DB. how to make them persistent?
With respect to POGO, you can use ExpandoMetaClass to add/override a property/field/constructor.
class Foo{
String bar
}
//Add a field to Foo at runtime
//Type is set to String in this case
Foo.metaClass.baz = "Hello World"
//Add a method to Foo at runtime
Foo.metaClass.doSomething = {String str -> str.reverse()}
assert new Foo().baz == "Hello World"
assert new Foo().doSomething("Hello") == "olleH"
For your use case, you may be able to use normal inheritance:
abstract class Base {
// common fields
// constraints for those fields
// etc.
}
class MyDomain extends Base {
}