vendredi 3 mai 2019

Can someone help me to to a pattern substitution? in Perl

I have a perl file (Example: hello.pl) containing this string: $Label_TEST = 'Number of test|>=0|0'; I need to create another pl file (Example Hello2.pl) in which I need to print the contents of hello but if I find >= i need to print instead of this: greater or equal then, Example:

$Label_TEST = 0 # Number of test (greater of equal to 0) [0];

jeudi 2 mai 2019

Interfaces without dependency injection

Since DO and IOC became well known and used ive seen a trend of an increase of using interfaces for lots of classes even if those classes are not services resolves by D/IOC. Like it would be better to at least have an interface of the class EVEN when the class itself is often referenced instead of the interface. What is the benefit of using interfaces like this if there is no DI/IOC.

IoC in Golang: how do I inject interfaces?

I'm trying to understand the best pattern to inject interfaces in go. I'm completely open to the possibility that I'm trying to abuse a model that just doesn't work in go, but I can't figure out what the cleanest solution would be.

Let's say I have a set of functions in the "foo" package that depend on a set of interfaces defined in the "bar" package. Let's use the example of trying to inject different persistent stores into a set of services.

So in "bar"

package bar

type DatabaseOne interface {
    Read() (string, error)
    Write() error
}

type DatabaseOneClient struct {
    Conn *db.ConnectionPool
}

func (client *DatabaseOneClient) Read() (string, error) { ... }
func (client *DatabaseOneClient) Write() error { ... }

Now, in "foo" I want to use this (and many more) client. So

package foo

import (
    "bar"
)

func DoSomething(dbClient *bar.DatabaseOneClient) {
    dbClient.Read()
    ...
}

The above is my current structure. The problem is that I can't inject a DatabaseOneClient mock, since DoSomething expects the struct.

I'm not sure how I would inject the interface. Should I create a new interface that contains all my potential clients and then implement that giant interface in my foo package? Basically, I want to do the following:

func DoSomething(dbClientOne *bar.DatabaseOne, dbClientTwo *bar.DatabaseTwo, dbClientThree *bar.DatabaseThree)

Then I can just create a mock Database in my test file...

I feel like I'm thinking about this in entirely the wrong way, but I don't understand the go way of doing this.

I'd appreciate insights. Thanks!

Command Pattern with Generic Return Type

I'm trying to implement the Command Pattern with a generic return type in Java.

After reviewing this answer on SO I created a Command class (interface) that looks like this:

public interface Command <T> {

    T execute(ArrayList<String> list, T type);
}


public class SearchResultsPage implements Command{

    @Override
    public <T extends List<ProductPOJO>>  T execute(ArrayList<String> list, T type) {

        List<ProductPOJO> productPOJOList = generatePOJOFromSearch(list);

        type.addAll(productPOJOList);

        return type;
}

}

However, Eclipse keeps complaining that:

The method execute(ArrayList, T) of type SearchResultsPage must override or implement a supertype method

When I click

Create execute() in supertype Command

Eclipse automatically creates method with exact same signature I created but the error message does not go away.

How can I fix this?

Thanks!

PHP design pattern for conditional instantiation of classes

I'm looking for best practices in the following scenario (using Laravel, but that's not relevant): I have a method strokePet, which depending on the request payload, will instantiate a DogStroker or a CatStroker class. The strokePet method is called via an API endpoint.

class PetController
{
    public function strokePet($request)
    {
        if ($request->pet == 'dog') {
            $stroker = new DogStroker;
        else if ($request->pet == 'cat') {
            $stroker = new CatStroker;
        } 
        $stroker->stroke();
    }
}

class DogStroker
{
    public function stroke()
    {
        echo 'grr';
    }
}

class CatStroker
{
    public function stroke()
    {
        echo 'prr';
    }
}

Is there any advantage in creating a PetStroker interface, e.g.

interface PetStroker
{
    public function stroke();
}

or is there some design pattern I'm missing here which would make this more OOP-idiomatic? (I'm aware this may get flagged for being too vague.)

How should I design my restfull API to accept data via POST in different formats (JSON,CSV) for the same purpose?

I have a business requirement to develop a restfull API using Spring boot which does the following :-

a) Accept the vehicle data in csv format over a POST request from the client. b) Accept the vehicle data in JSON format over a POST request from the client.

In the above a) and b) the fields are same but just in different formats ( One is JSON and another is CSV ).

My question is how should be my design to chieve this?

1) Shall I simply go ahead by creating class A and have 2 different endpoint methods. One to accept csv and another to accept json? Or there are any better ways to deal with such scenarios?

2) What should my class structure look like?

3) Any specific design patterns that suit this requirement? Or any specific reccomendation to deal with such scenario?

Any help is highly appreciated.

Authorization with: dynamic groups, permission based content lists, micro-services based architecture?

I'm not sure if Spring Security / Spring Cloud can be useful for my authorization needs. This is my use-case:

  • I've a CMS-like application.
  • A content can be assigned to one or more 'groups'
  • A content can be viewed by a user if this user belongs to at least one of the groups.
  • Important note: these groups are not hardcoded! Through the web application, the users can create (at run-time) new custom defined groups, so the groups and the users are inside DB tables.

Example: a content is assigned to the groups: 'Germany', 'Sweden', 'Programmers', 'Managers'.

  • a Manager from Germany can read that content
  • a Manager from France can read that content (he is a at least a Manager)
  • a UX Designer from France cannot read that content.

In order to manage permission to access a single content, the check is quite simple!

My biggest issue is about listing contents that a user is allowed to see. Currently I manage this through SQL queries. I join the contents with their assigned groups, then I use this kind of SQL to filter the contents:

... WHERE ... group IN  [[list of groups associated to the authenticated user]]

In order to prevent performance issue, these query results are cached.

The problems seems worse when I add 'data' to these contents, let's imagine this contents can be commented. I want to list just the comment a user is authorized to see.

The rule is quite simple, you can read a comment if you are allowed to see its parent content.

Again, if I want to check the user permissions to see a single comment, the check is very simple, but things are more difficult if I want to list comments.

And... finally, if I want to adopt a microservice based architecture, the comments will be managed by a different service (no?). But in order to list the comments a user is allowed to see I'd need again all the "users & groups" information, so a micro-service based architecture doesn't seem so appropriate.

Am I totally wrong in my design approach?

Thank you very much.