fixture-monkey icon indicating copy to clipboard operation
fixture-monkey copied to clipboard

Generator on kotlin secondary constructor

Open acktsap opened this issue 4 years ago • 7 comments

secondary constructor에 대한 generator

한개만 있으면 그걸 사용하고 여러개가 있을 경우 annotation을 사용해서 mark

data class A(val value: Int) {
     constructor() : this(0) {
         // do something
     }

     @SecondaryConstructor // 이런 식으로
     constructor(val value: String) : this(value.toInt()) {
         // do something
     }

}

acktsap avatar Nov 30 '21 08:11 acktsap

프로덕션 코드에 어노테이션을 추가해야만 하나요?

seongahjo avatar Dec 01 '21 03:12 seongahjo

딱히 다른 방법이 없으면 프로덕션 코드에 annotation을 추가하는 것을 생각하고 있었습니다.

acktsap avatar Dec 02 '21 12:12 acktsap

@acktsap @ConstructorProperties 가 붙어있으면 해당 생성자를 사용하게 하면 될거 같습니다.

mhyeon-lee avatar Dec 02 '21 12:12 mhyeon-lee

@mhyeon-lee ConstructorProperties도 처음에 생각해봤었는데요, 뭔가 java.beans package에 있기도 하고 역할이 조금 다른거 같아서 간단한거 한개 새로 추가하는거도 괜찮다고 생각했습니다. 그냥 사용해도 큰 문제는 없을거 같긴 합니다.

acktsap avatar Dec 02 '21 13:12 acktsap

@acktsap Fixture Monkey 를 위한 애노테이션이 추가되고, 이 애노테이션을 프로덕션 코드에서 사용하려면, fixture-monkey dependency 수준을 testImplemenation 이 아닌 implementation 으로 올려야 됩니다.

가능하면 fixture monkey 를 사용하기 위해 프로덕션에 의존성이 추가되는 방향은 지양하려고 생각중입니다.

반드시 필요한 경우라면 production 의존성을 위한 모듈을 별도로 만들어야 되는데, 아직은 그렇게 까지 필요하다고 생각되지는 않습니다.

mhyeon-lee avatar Dec 02 '21 13:12 mhyeon-lee

@acktsap javax 나 kotlin 에 시중에 나와있는 Annotation 중에 적당한게 없을까 봤는데 딱히 없네요 Spring Data 에서 사용하는 @PersistenceConstructor 이거는 spring-data-common 의존성이 있고요

딱히 마땅한게 없다면 @ConstructorProperties 가 그나마 가이드할만할 거 같습니다. 말씀하신대로 용도가 정확히 맞지는 않는데 그렇다고 더 좋은 애노테이션이 없네요

코틀린이니까 @CosntructorProperties 가 붙어 있는 생성자를 선택하되 properties 는 애노테이션에 명시할 필요 없이 직접 생성자의 정보를 읽어도 좋을거 같습니다.

mhyeon-lee avatar Dec 02 '21 13:12 mhyeon-lee

@mhyeon-lee 아 그렇네요 test level로 쓰고있었죠. 네 @ConstructorProperties를 그냥 쓰면 될거같네요 감사합니다.

acktsap avatar Dec 02 '21 13:12 acktsap

Use ConstructorPropertiesArbitrartyIntrospector since 0.4.3

seongahjo avatar Nov 23 '22 01:11 seongahjo