Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

26
LINES

< > BotCompany Repo | #1025447 // ClassesAndPatterns - parse/unparse just using record definitions

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3445L/22K).

1  
sclass ClassesAndPatterns {
2  
  new LPair<Class, S> entries;
3  
  bool convertPatternsToLowerCase = true; // should only be a cosmetic difference
4  
5  
  *(L<Class> classes, Class outerClass) {
6  
    entries = map(classes,
7  
      c -> pair(c, lowerIf(convertPatternsToLowerCase, dollarsToStarsAndSplitCamelCase(classNameRelativeTo(c, outerClass)))));
8  
  }
9  
10  
  O parse(S s, IF1<S, O> parseArg) {
11  
    new Matches m;
12  
    fOr (Pair<Class, S> p : entries)
13  
      if (match3_plusBrackets_uncurly(p.b, s, m))
14  
        ret nuObject(p.a, mapToObjectArray(parseArg, m.m));
15  
    null;
16  
  }
17  
  
18  
  S unparse(O o) {
19  
    if (o == null) null;
20  
    if (!o instanceof IFieldsToList) ret str(o);
21  
    Class c = _getClass(o);
22  
    Pair<Class, S> p = firstThat(entries, x -> x.a == c);
23  
    if (p == null) ret str(o); // class not in list
24  
    ret format3_curlyIfMultiToken(p.b, allToString_array(o/IFieldsToList._fieldsToList()));
25  
  }
26  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1025447
Snippet name: ClassesAndPatterns - parse/unparse just using record definitions
Eternal ID of this version: #1025447/11
Text MD5: a9c9177953c945458298d0c5e63e7be1
Transpilation MD5: 2cc0791138c1fa47d9fa4da9f35a6f87
Author: stefan
Category: javax / a.i.
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-09-29 02:16:35
Source code size: 916 bytes / 26 lines
Pitched / IR pitched: No / No
Views / Downloads: 266 / 691
Version history: 10 change(s)
Referenced in: [show references]