1+ package com .eternalcode .multification .okaeri ;
2+
3+ import com .eternalcode .multification .notice .Notice ;
4+ import com .eternalcode .multification .notice .Notice .Builder ;
5+ import com .eternalcode .multification .notice .NoticeKey ;
6+ import com .eternalcode .multification .notice .NoticePart ;
7+ import com .eternalcode .multification .notice .resolver .NoticeContent ;
8+ import com .eternalcode .multification .notice .resolver .NoticeDeserializeResult ;
9+ import com .eternalcode .multification .notice .resolver .NoticeResolverRegistry ;
10+ import com .eternalcode .multification .notice .resolver .NoticeSerdesResult ;
11+ import com .eternalcode .multification .notice .resolver .NoticeSerdesResult .Multiple ;
12+ import com .eternalcode .multification .notice .resolver .NoticeSerdesResult .Single ;
13+ import com .eternalcode .multification .notice .resolver .chat .ChatContent ;
14+ import eu .okaeri .configs .schema .GenericsDeclaration ;
15+ import eu .okaeri .configs .serdes .DeserializationData ;
16+ import eu .okaeri .configs .serdes .ObjectSerializer ;
17+ import eu .okaeri .configs .serdes .SerializationData ;
18+ import java .util .Collections ;
19+ import java .util .List ;
20+ import java .util .Set ;
21+ import org .jetbrains .annotations .NotNull ;
22+
23+ public class MultificationNoticeSerializer implements ObjectSerializer <Notice > {
24+
25+ private static final int SINGLE_SERIALIZE_DESERIALIZE_PART = 1 ;
26+
27+ private final NoticeResolverRegistry noticeRegistry ;
28+
29+ public MultificationNoticeSerializer (NoticeResolverRegistry noticeRegistry ) {
30+ this .noticeRegistry = noticeRegistry ;
31+ }
32+
33+ @ Override
34+ public boolean supports (@ NotNull Class <? super Notice > type ) {
35+ return Notice .class .isAssignableFrom (type );
36+ }
37+
38+ @ Override
39+ public void serialize (Notice notice , @ NotNull SerializationData data , @ NotNull GenericsDeclaration generics ) {
40+ List <NoticePart <?>> parts = notice .parts ();
41+
42+ boolean isChatBeautifulSerialized = trySerializeChatBeautiful (data , notice );
43+
44+ if (isChatBeautifulSerialized ) {
45+ return ;
46+ }
47+
48+ for (NoticePart <?> part : parts ) {
49+ NoticeSerdesResult result = this .noticeRegistry .serialize (part );
50+
51+ if (result instanceof NoticeSerdesResult .Single single ) {
52+ data .add (part .noticeKey ().key (), single .element ());
53+ continue ;
54+ }
55+
56+ if (result instanceof Multiple multiple ) {
57+ data .add (part .noticeKey ().key (), multiple .elements ());
58+ }
59+ }
60+ }
61+
62+ @ Override
63+ public Notice deserialize (DeserializationData data , @ NotNull GenericsDeclaration generics ) {
64+ Builder builder = Notice .builder ();
65+
66+ if (data .isValue ()) {
67+ Object value = data .getValueRaw ();
68+
69+ if (value instanceof String stringValue ) {
70+ List <String > messages = Collections .singletonList (stringValue );
71+ builder .withPart (NoticeKey .CHAT , new ChatContent (messages ));
72+ }
73+
74+ if (value instanceof List ) {
75+ List <String > messages = data .getValueAsList (String .class );
76+ builder .withPart (NoticeKey .CHAT , new ChatContent (messages ));
77+ }
78+
79+ return builder .build ();
80+ }
81+
82+ Set <String > keys = data .asMap ().keySet ();
83+
84+ for (String key : keys ) {
85+ Object value = data .getRaw (key );
86+
87+ if (value instanceof String stringValue ) {
88+ NoticeDeserializeResult <?> noticeResult = this .noticeRegistry .deserialize (key , new Single (stringValue ))
89+ .orElseThrow (() -> new UnsupportedOperationException (
90+ "Unsupported notice key: " + key + " with value: " + stringValue ));
91+
92+ this .withPart (builder , noticeResult );
93+ continue ;
94+ }
95+
96+ if (value instanceof List ) {
97+ List <String > messages = data .getAsList (key , String .class );
98+
99+ NoticeDeserializeResult <?> noticeResult = this .noticeRegistry .deserialize (key , new Multiple (messages ))
100+ .orElseThrow (() -> new UnsupportedOperationException (
101+ "Unsupported notice key: " + key + " with values: " + messages ));
102+
103+ this .withPart (builder , noticeResult );
104+ continue ;
105+ }
106+
107+ throw new UnsupportedOperationException (
108+ "Unsupported notice type: " + value .getClass () + " for key: " + key );
109+ }
110+
111+ return builder .build ();
112+ }
113+
114+ private <T extends NoticeContent > void withPart (Builder builder , NoticeDeserializeResult <T > noticeResult ) {
115+ builder .withPart (noticeResult .noticeKey (), noticeResult .content ());
116+ }
117+
118+ private static boolean trySerializeChatBeautiful (SerializationData data , Notice notice ) {
119+ List <NoticePart <?>> parts = notice .parts ();
120+
121+ if (parts .size () != 1 ) {
122+ return false ;
123+ }
124+
125+ NoticePart <?> part = parts .get (0 );
126+
127+ if (part .noticeKey () != NoticeKey .CHAT ) {
128+ return false ;
129+ }
130+
131+ ChatContent chat = (ChatContent ) part .content ();
132+ List <String > messages = chat .contents ();
133+
134+ if (messages .size () == SINGLE_SERIALIZE_DESERIALIZE_PART ) {
135+ data .setValue (messages .get (0 ));
136+ return true ;
137+ }
138+
139+ data .setValue (messages );
140+ return true ;
141+ }
142+ }
0 commit comments