cs-script Unexpected character "$" - cs-script

Use cs-script in a console app with
dynamic script = CSScript.LoadCode(
#"using System;
public class Script
{
public void SayHello(string name)
{
Console.WriteLine($""Hello: {name}!"");
}
}")
.CreateObject("*");
script.SayHello("World");
Unexpected character "$" exception throwed.
But the following code do well.
dynamic script = CSScript.Evaluator.LoadCode(
#"using System;
public class Script
{
public void SayHello(string name)
{
Console.WriteLine($""Hello: {name}!"");
}
}");
script.SayHello("World");
And i hnow CSScript.Evaluator is more solwer.How can i use the syntax sugar faster?

Related

GraphQL : Query failed to validate

I'm testing graph-spqr librabry in a simple java program.
This is what I've done so far:
public class GraphQLResolver {
private String status;
private Integer price;
#GraphQLMutation(name="updateStatusOrder")
public void updateStatusOrder(#GraphQLArgument(name="id") String orderId,#GraphQLArgument(name="status") String status) {
//to do
}
#GraphQLQuery(name="getStatus")
public String getStatus(#GraphQLArgument(name="id") String orderId) {
this.status="Example of status";
return this.status;
}
}
Then, I calling building GraphQL methods in main method
public class Main {
public static void main(String[] args) {
GraphQLSchema schema = new GraphQLSchemaGenerator()
.withOperationsFromSingleton(new GraphQLResolver())
.generate(); //done ;)
GraphQL graphQL = new GraphQL.Builder(schema).build();
ExecutionResult result = null;
result = graphQL.execute("{getstatus(id:123){status}}");
}
}
I have error : Query failed to validate : '{status(id:123){status}}'
What is wrong?
I guess the correct query should be :
{
getstatus(id:"123")
}
with the following reasons :
You define the id argument as String , so need to use double quote around the argument value.
The getStatus query is defined to return a String which is a scalar but not an object type in term of GraphQL. So you don't need to further define what of its fields to be returned as the scalar does not have any fields for you to pick.

YAML to Java using snakeyaml

Can't figure out why I am getting a NullPointerException on my platform in my Configuration class when I try to print out the object.
Here's the yaml file:
platform:
platformType: CVN
hwVariant:
canesVariant: HW
hullNumber: XXX
tri: INST
Here is my parent object class Configuration.java
public class Configuration {
private Platform platform;
public Configuration() {
}
public Platform getPlatform() {
return platform;
}
public void setPlatform(Platform platform) {
this.platform = platform;
}
#Override
public String toString() {
return platform.toString();
}
}
Here's my Platform.java:
import java.util.Map;
public class Platform {
private String platformType;
private Map<String, String> hwVariant;
private String hullNumber;
private String tri;
public Platform() {
}
public String getPlatformType() {
return platformType;
}
public Map<String, String> getHWVariant() {
return hwVariant;
}
public String getHull() {
return hullNumber;
}
public String getTri() {
return tri;
}
public void setPlatformType(String platformType) {
this.platformType = platformType;
}
public void setHWVariant(Map<String, String> hwVariant) {
this.hwVariant = hwVariant;
}
public void setHullNumber(String hullNumber) {
this.hullNumber = hullNumber;
}
public void setTri(String tri) {
this.tri = tri;
}
#Override
public String toString() {
return "platform: \n" +
" platformType: " + platformType + "\n" +
" hwVariant: " + hwVariant + "\n" +
" hullNumber: " + hullNumber + "\n" +
" tri: " + tri;
}
}
Here's the NullPointerException I'm getting:
File is: E:\workspace_neon\PlatformInterviewer\resources\uswdss-platform.yml
Exception in thread "main" Can't construct a java object for tag:yaml.org,2002:net.progeny.setup.model.Configuration; exception=null
in 'reader', line 1, column 1:
platform:
^
at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:314)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructObjectNoCheck(BaseConstructor.java:204)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:193)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:159)
at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:146)
at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:524)
at org.yaml.snakeyaml.Yaml.loadAs(Yaml.java:518)
at net.progeny.setup.YamlConfiguration.main(YamlConfiguration.java:28)
Caused by: java.lang.NullPointerException
at net.progeny.setup.model.Configuration.toString(Configuration.java:31)
at java.lang.String.valueOf(String.java:2994)
at java.lang.StringBuilder.append(StringBuilder.java:131)
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:268)
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:149)
at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:309)
... 7 more
I just can't seem to get it working when I added the inner nested hwVariant option. If I remove the hwVariant and canesVariant from my yaml file it works fine, albeit without those key value pairs. What am I missing?
Change Getter and Setter method of field 'hwVariant' of class Platform to following.
public Map<String, String> getHwVariant() {
return hwVariant;
}
public void setHwVariant(Map<String, String> hwVariant) {
this.hwVariant = hwVariant;
}
SnakeYaml internally uses jackson to map yaml document into a java object. Jackson internally calls getter and setter method of the fields, so it expects getter and setter to be in standard form.

How can I inject #PathVariable into a field?

Let's say I have following controller.
#RestController()
#RequestMapping("/my/{path}")
public class MyController {
public void some1(#PathVariable("path") String path) {
}
public void some2(#PathVariable("path") String path) {
}
public void some3(#PathVariable("path") String path) {
}
}
Now I want the path be injected into a field.
// I usually do this with JAX-RS
#RequestScope // I added this!
#RestController()
#RequestMapping("/my/{path}")
public class MyController {
public void some1() {
}
public void some2() {
}
public void some3() {
}
// single declaration for all methods
// I know ElementType.FIELD is not one of #PathVariable's target
// Is there any equivalent way to do this with Spring?
#PathVariable("path")
String path
}
Not compiles.
How can I do this?
request url : /a/b/c
#RequestMapping(value = "/a/{some}/c")
public void some(#PathVariable("some") String some) {
}
#PathVariable Annotation which indicates that a method parameter should be bound to a URI template variable.
Examples :
#ResponseBody
RequestMapping(value="/myapp/{id}")
public String method(#PathVariable("id") int id){
return "id="+id;
}
#ResponseBody
#RequestMapping(value="/myapp/{id:[\\d]+}/{name}")
public String method(#PathVariable("id") long id, #PathVariable("name") String name){
return "id= "+id+" and name="+name;
}
Refer more for below links :
Spring mvc #PathVariable
https://www.journaldev.com/3358/spring-requestmapping-requestparam-pathvariable-example

How Get data from a derived class?

Get data from a derived class..
My samples.
public class Maintest
{
public string name = "2";
}
public class test : Maintest
{
string bla = name;
}
or
public class test : Maintest
{
test child = new test();
string bla = child.name;
}
Please reply
or
Share a link to explore
For example there is the main class
and I have a derived class that will output data of the first class.
As an example, I just wanted to pass the value of the derivative in the main class. For a proper understanding
If you return the field from a property, it might look a little something like this.
using System;
public class Program
{
public void Main()
{
var test = new Test();
Console.WriteLine(test.greeting);
}
}
// make this abstract if you're never directly instantiate MainTest
public abstract class MainTest
{
public string name = "world";
}
public class Test : MainTest
{
public string greeting {get { return "Hello " + name;}}
}
http://dotnetfiddle.net/R8kwh3
Also, you can enforce a contract by doing something like
public abstract class MainTest
{
public string name = "world";
// create an abstract property to ensure it gets implemented in the inheriting class
public abstract string greeting {get; private set;}
}
public class Test : MainTest
{
public override string greeting {get { return "Hello " + name;}}
}
Maybe you want get data in method? Therefore, you can use this:
public class test : Maintest
{
public string GetData()
{
return name;
}
}

Why it seems impossible to use BeanUtils.copyProperties from a JPA entity to a JAX-B Bean?

We are using JPA Entities to get the database rows and then when we transfer that to the external, we want to use disconnected object (DTO) which are simple beans annotated with JAX-B.
We use a mapper and its code looks like this:
public BillDTO map(BillEntity source, BillDTO target) {
BeanUtils.copyProperties(source, target);
return target;
}
But when the code is running we get an error like this:
java.lang.IllegalArgumentException: argument type mismatch
Note this is the Spring implementation of the BeanUtils:
import org.springframework.beans.BeanUtils
And the naming of the properties are identical (with their getter/setter).
Anybody knows why the error happens?
And how to use a fast way instead just copying properties one by one?
This example working well. Here String property is copied to enum property:
Entity:
public class A {
private String valueFrom;
public String getValue() {
return valueFrom;
}
public void setValue(String value) {
this.valueFrom = value;
}
}
DTO (En is enumeration):
public class B {
private En valueTo;
public void setValue(String def) {
this.valueTo = En.valueOf(def);
}
public void setEnumValue(En enumVal) {
this.valueTo = enumVal;
}
}
As for your GitHub example, problem in class B in getter should be:
public String getValue()
Example:
public String getValue() {
return value.toString();
}

Resources