Giter VIP home page Giter VIP logo

Comments (1)

chickenchickenlove avatar chickenchickenlove commented on June 15, 2024

Hello, @ikhoon nim.

I've come up with an idea for a scalable API, although it might be a bit rough.
By using enums and Maps together, I expect that it can be extended in various ServiceConfig's Children Classes.

How about this way?

public abstract class ServiceConfig { 
     // Add this abstract method. 
    public <E, T> T getSpecificValue(E field, Class<T> type);
     ...
}
  • Add abstract method to ServiceConfig. all children classes of ServiceConfig should implement this method.
  • This method is to produce common interface to get their specific values. (For example, AnnotatedService's method)
  • E means kind of enum. T means type of values.
// Add this enum
public enum AnnotedServiceField {
  Method, 
  ServiceObject, 
  DefaultStatus,
  ...
}
  • Add enum for each concrete class of ServiceConfig to prevent to forget some fields.
public class AnnotatedService { 
  
  ...
  // Add this method.
  public Map<AnnotatedServiceField, Object> fieldsToMap(){ 
    final Map<AnnotatedServiceField, Object> maps = Arrays.stream(AnnotatedServiceField.values())
              .collect(Collectors.toMap(v -> v, v -> null));
    
    ...
    maps.put(AnnotatedServiceField.HttpStatus, this.httpStatus);
    maps.put(AnnotatedServiceField.Method, this.method);
    ...

   return maps;
  } 
...
}
  • In case of AnnotatedService, i propose that add fieldsToMap() method to return Map their specific fields.
public class AnnotatedServiceConfig extends ServiceConfig {

  ...
  // add this methods.
  private final Map<AnnotatedServiceField, Object> specificConfig;
  public T getSpecificValue(AnnotatedServiceField field, Class<T> type){
      Object field = specifigConfig.get(field);
      return (type) field;
  }
  ...

}
  • AnnotatedServiceConfig should implement abstract method T getSpecificValue(AnnotatedServiceField field, Class<T> type).
public class ServiceConfigBuilder implements ServiceConfigSetters {
    ServiceConfig build(...) {
        final Map<AnnotatedServiceField, Object> specificFields = service.fieldsToMap()   // Add this code.
        ServiceErrorHandler errorHandler =
                serviceErrorHandler != null ? serviceErrorHandler.orElse(defaultServiceErrorHandler)
                                            : defaultServiceErrorHandler;
        ...
    }
}
  • In case of AnnotatedServiceConfig, service.fieldsToMap() can be added to ServiceConfigBuilder#build(...) for specificFields to be included AnnotatedServiceConfig.

IMHO, The key aspects are as follows:

  1. Declare an enum for each Concrete ServiceConfig class to specify certain values.
  2. Create a Map that uses the enum as a Key and stores values.
  3. The ServiceConfig interface provides a Generic method to retrieve values from the map.

Through this approach, I expect to ServiceConfig can produce API that can be extended to multiple concrete class of ServiceConfig.

What do you think?
When you have some free time, please take a look 🙇‍♂️

from armeria.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.