Mapstruct dynamic source to target mapping - spring-boot

I have to map one of the dto to the target model. How can I map source to target based upon suffix prefix
for example I have to do below
#Mapping(source = "dto.balanceSheetInfoDto.assets.value", target = "assetsValue")
#Mapping(source = "dto.balanceSheetInfoDto.assets.isNA", target = "assetsIsNA")
#Mapping(source = "dto.balanceSheetInfoDto.assets.note", target = "assetsNote")
#Mapping(source = "dto.balanceSheetInfoDto.liquidAssets.value", target = "liquidAssetsValue")
#Mapping(source = "dto.balanceSheetInfoDto.liquidAssets.isNA", target = "liquidAssetsIsNA")
#Mapping(source = "dto.balanceSheetInfoDto.liquidAssets.note", target = "liquidAssetsNote")
How can I avoid doing this repeatedly for different fields?

If you are not tied to MapStruct, and if it is not too late for that, I think ModelMapper is better suited for what you need. It does exactly what you describe here, out of the box.
See : http://modelmapper.org/getting-started/#mapping

Related

Create publisher dynamically with conditions

I'm just looking for an idea for converting this pseudo code into a reactive style.
var records = new ArrayList<>();
var query = new Query();
var results = query.executeQuery();
records.addAll(results.getRecords());
while (results.hasMore()) {
query = new Query(results.offset())
deals = hubspotQuery.executeQuery(Deals.class);
records.addAll(results.getRecords());
}
The idea is to collect all records into a Flux
Here is one possible solution. Maybe there is others, but this one is simple.
This is not real code, but it describe the logic.
Flux<Records> query = createFluxQuery();
query.expand(record -> (record.hasMore()) ? createFluxQuery(record.offset) : Flux.empty());

Extensions in proto3

Given the following proto definitions:
syntax = "proto3";
import "google/protobuf/descriptor.proto";
option java_package = "com.example.dto";
option java_multiple_files = true;
extend google.protobuf.FieldOptions {
Projector projector = 50002;
}
message Projector {
string name = 1;
string class = 2;
bool default = 3;
}
message SearchRequest {
string query = 1 [(projector) = {name: "queryProjector", class: "foobar"}];
int32 page_number = 2;
int32 result_per_page = 3;
}
How can I access the field extension?
As far as I understand extension still work in proto3, but are generally replaced by the Any type?
I came as this far:
final Descriptors.Descriptor descriptor = SearchRequest.getDescriptor();
final Descriptors.FieldDescriptor query = descriptor.findFieldByName("query");
Is this the right way? Whats the next step?
As stated here https://github.com/google/protobuf/issues/1460
Custom options are still supported. It's the only place where you can use extensions in proto3. It works the same way as in proto2. Languages that don't support proto2 may provide a special API to access custom options as they don't support extensions.
so custom options seem to be still supported and you should get them using
descriptor.findFieldByName("query").getOptions().getAllFields();
That would return you a map of your custom options (as fields)
final Map<Descriptors.FieldDescriptor, Object> allFields;
whereas the value would be the type of your option, Projector in your case.
The FileDescriptor for this custom option (projector) seems to be generated as a public static in a class named after your *.proto file using its camelCase name.
If your proto file is called search_service_v1.proto you might find the custom option directly as follows:
final DescriptorProtos.FieldOptions options descriptor.findFieldByName("query").getOptions();
final Object field = options.getField(SearchServiceV1.projector.getDescriptor());
And you'll get your extension by
final Projector projector = Projector.class.cast(field);

SpEL not able to extract attribute value from Scala object

I have a simple Scala class called Case
case class Case(
#(Id#field) var id: String,
var state: CaseState = new OpenCaseState,
var notes: List[CaseNote] = new ArrayList(),
var assignedGroups:Set[String] = new HashSet(),
var aclTemplateIds: Set[String] = new HashSet()
) extends Serializable { }
I created an instance of this class called a_case, setting id as 123. I am trying to get the value of the id attribute. I tried this
var parser: ExpressionParser = new SpelExpressionParser
var context: EvaluationContext = new StandardEvaluationContext(a_case)
var extractedId = parser.parseExpression("'id'").getValue(context).asInstanceOf[String]
All I get is "id" in my extractedId variable. When I try to parse "id" without the single quotes, I get an exception saying the property id is not found in Case. Am I missing something here or is this a Scala issue?
SpEL can do that for you if your id has getter.
I'm not well with Scala, but:
BeanProperty
You can annotate vals and vars with the #BeanProperty annotation. This generates getters/setters that look like POJO getter/setter definitions. If you want the isFoo variant, use the BooleanBeanProperty annotation. The ugly foo$_eq becomes
setFoo("newfoo");
getFoo();
https://twitter.github.io/scala_school/java.html

How to determine the custom parameters and values used during a previous TFS Build?

How can one determine the custom parameter values that were used during a TFS build?
You can achieve that via using TFS API, check the following code:
TfsTeamProjectCollection tfctc = new TfsTeamProjectCollection(new Uri("http://tfsservername:8080/tfs/DefaultCollection"));
IBuildServer bs = tfctc.GetService<IBuildServer>();
IBuildDetail[] builds = bs.QueryBuilds("teamprojectname", "builddefinitionname");
foreach (var build in builds)
{
var buildefinition = build.BuildDefinition;
IDictionary<String, Object> paramValues = WorkflowHelpers.DeserializeProcessParameters(buildefinition.ProcessParameters);
string processParametersValue = paramValues["argument1"].ToString();
Console.WriteLine(processParametersValue);
}

How to check for null attributes in LinqToXML expressions?

I have a LinqToXML expression where I am trying to select distinct names based on similar attributes. The code is working great and I've put it below:
var q = xmlDoc.Element("AscentCaptureSetup").Element("FieldTypes")
.Descendants("FieldType")
.Select(c => new { width = c.Attribute("Width").Value,
script = c.Attribute("ScriptName").Value,
sqlType = c.Attribute("SqlType").Value,
enableValues = c.Attribute("EnableValues").Value,
scale = c.Attribute("Scale").Value,
forceMatch = c.Attribute("ForceMatch").Value,
forceMatchCaseSensitive = c.Attribute("ForceMatchCaseSensitive").Value,
sortAlphabetically = c.Attribute("SortAlphabetically").Value,
})
.Distinct();
The problem arises since not all the attributes are required, and if one of them is omitted, for example sortAlphabetically, I get an Object not Referenced error. Makes sense, but it there a way to alter the query to only use assign the new values if the attribute actually exists? (Thereby bypassing any null pointer errors)
Instead of using the Value property (which will blow up on a null reference), simply cast the XAttribute to string - you'll either get the value, or a null reference if the XAttribute reference is null. (XElement works the same way, and this applies to all conversions to nullable types.)
So you'd have:
.Select(c => new {
width = (string) c.Attribute("Width"),
script = (string) c.Attribute("ScriptName"),
sqlType = (string) c.Attribute("SqlType"),
enableValues = (string) c.Attribute("EnableValues"),
scale = (string) c.Attribute("Scale"),
forceMatch = (string) c.Attribute("ForceMatch"),
forceMatchCaseSensitive = (string) c.Attribute("ForceMatchCaseSensitive"),
sortAlphabetically = (string) c.Attribute("SortAlphabetically"),
})
Some of those attributes sound like they should actually be cast to int? or bool?, mind you...

Resources