jeudi 8 avril 2021

Why is .JSON recommended over static code for protobufjs?

For the protobufjs library, they recommend using .JSON over static code

https://github.com/protobufjs/protobuf.js/tree/master/cli#reflection-vs-static-code

For production environments it is recommended to bundle all your .proto files to a single .json file, which minimizes the number of network requests and avoids any parser overhead (hint: works with just the light library):

However, I don't understand why this is recommended:

  1. The number of network requests would be the same for JSON and compiled source code. In other words, both would just have one request if the user rendered the .proto files into one .js/.json file.
  2. To me, the 'minimal' library suggests a smaller size than 'light' library

And, as a smaller question, the "tradeoff" of no reflection for stati code seems wrong to me - I've thought of using reflection as a code smell. Am curious why this is listed as a trade off.





Convert custome Key+Value JSON object Response to C# Model

i have this API that i want to creat a dynamic object. My point here is to have normal object to deal with rather than what this API returns to me, i can't controll this API so the retrived data can't be modified, this is the body, and it represnet a view in Database

Also i think reflection could help me in this case, any idea....

Request Body:

{
    "ViewName": "Person",
    "ViewOutput": "Name, Email, Number",
    "ViewFilter": [
        {
            "filterKey": "Number",
            "filterValue": "532000000"
        }
    ]
}

I want the ViewName + ViewOutput+ ViewFilter to be paramterized, ViewName it will take single value, ViewOutput will be array of string ViewFilter will be list of filteration ("FilterKey", "FilterVlaue") because it could be mutliple filteration value like this:

"ViewFilter": [
        {
            "filterKey": "Number",
            "filterValue": "532000000"
        },
        {
            "filterKey": "Email",
            "filterValue": "test1@test.ps"
        }
    ]

This is What API Return to me, a list of Person Keys and values,

Response:

{
    "ResponseCode": "0",
    "ResponseMessage": "Success",
    "NumberOfRecords": "1",
    "MainData": [
        {
            "recordData": [
                {
                    "dataKey": "Name",
                    "dataValue": "Test Name"
                },
                {
                    "dataKey": "Email",
                    "dataValue": "test@test.ps"
                },
                {
                    "dataKey": "Number",
                    "dataValue": "532000000"
                }
            ]
        }
    ]
}

What i want to be the output is like this:

"Person": [
{
    "Name":"Test",
    "Email":"test@test.ps",
    "Number":"532000000",
}]




mercredi 7 avril 2021

Kotlin: Getting actual generic type of super interface via reflection

open class Device

class Router : Device()

interface GenericDAO<T, ID>

open class DeviceDAO<T : Device> : GenericDAO<T, Long>

class RouterDAO : DeviceDAO<Router>()

I need a function get T and ID actual type





Add custom annotation to the @Entity class at runtime

I'm trying to add at runtime @EntityListeners annotation to the following entity:

@Data
@Builder
@Entity
@CustomEntityAnnotation
public class DummyEntity {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Integer id;

  @CustomFieldAnnotation
  @Column(name = "dummy_filed")
  private String dummyField;
}

I'm using custom ApplicationContextInitializer where I'm trying before the spring context is loaded to find all entity classes that are marked by @CustomEntityAnnotation and add to those classes annotation @EntityListeners(CustomListener.class) which contains @PrePersist @PreUpdate @PreRemove methods to intercept the operations with database and do some audit stuff with the fields marked with @CustomFieldAnnotation.

Reflections reflections = new Reflections("com.spai.utils.auditor");
ClassPool classPool = ClassPool.getDefault();
reflections.getTypesAnnotatedWith(AuditableEntity.class).forEach(clazz -> {
  log.info(clazz.getName());
  try {
    ClassLoader classLoader = classPool.getClassLoader();
    CtClass ctClass = classPool.makeClass(clazz.getName());
    ClassFile classFile = ctClass.getClassFile();
    ConstPool constPool = classFile.getConstPool();
    AnnotationsAttribute annotationsAttribute =
        new AnnotationsAttribute(constPool, AnnotationsAttribute.visibleTag);
    Annotation annotation = new Annotation(EntityListeners.class.getCanonicalName(), constPool);
    ClassMemberValue value =
        new ClassMemberValue(CustomListener.class.getCanonicalName(), constPool);
    annotation.addMemberValue("value", value);
    annotationsAttribute.addAnnotation(annotation);
    classFile.addAttribute(annotationsAttribute);

    ctClass.getAnnotations(); // here annotation is present but not in clazz.getAnnotations()
    //ctClass.toClass(); // this line throws LinkageError attempted duplicate class definition for com.dummy.DummyEntity
    System.out.println();
  } catch (Exception e) {
    e.printStackTrace();
  }
}

Above in the comments in code, I described a situation I'm experiencing... what I'm missing or doing wrong? since the annotation is not appearing...





In Java, given a subclass, how to check if it overrides the default method of its parent interface using reflection?

Looking at Method, I'm not sure if there is an obvious way for doing this.





How to avoid a bug that crashes the PHP script instance when attempting to use ReflectionProperty::getValue?

I have to do the following reflection code because the WooCommerce version I have available has a bug (v4.9.0).

Please see the comments in the code below:

// checked before the existence of the class with class_exists
$rp = new ReflectionProperty('WC_Product_Variable_Data_Store_CPT', 'prices_array');
$rp->setAccessible(true);
var_dump('start'); // echoes something to the html code
$protected_prices_array = $rp->getValue($ths); // crashes the PHP script instance
var_dump('stop'); // this is not printed anymore

If requested, I can offer more code.

Currently I am attempting to inherit the given class to see if I can walk around the bug.

On staging site I have PHP 7.4.16.

Update 1

I have this code in my own function my_read_price_data( $ths, &$product, $for_display = false ) { ... which does the same as WC's data store's read_price_data public method which accesses the prices_array property which is protected.





Get class attributes from class defintion

I would like to get a list of attributes from the class itself (not a class instance)

class A { a:string; b:string; }
getAttributes(A); // returns ['a', 'b']

Is this possible in typescript ?