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).

sclass ClassesAndPatterns {
  new LPair<Class, S> entries;
  bool convertPatternsToLowerCase = true; // should only be a cosmetic difference

  *(L<Class> classes, Class outerClass) {
    entries = map(classes,
      c -> pair(c, lowerIf(convertPatternsToLowerCase, dollarsToStarsAndSplitCamelCase(classNameRelativeTo(c, outerClass)))));
  }

  O parse(S s, IF1<S, O> parseArg) {
    new Matches m;
    fOr (Pair<Class, S> p : entries)
      if (match3_plusBrackets_uncurly(p.b, s, m))
        ret nuObject(p.a, mapToObjectArray(parseArg, m.m));
    null;
  }
  
  S unparse(O o) {
    if (o == null) null;
    if (!o instanceof IFieldsToList) ret str(o);
    Class c = _getClass(o);
    Pair<Class, S> p = firstThat(entries, x -> x.a == c);
    if (p == null) ret str(o); // class not in list
    ret format3_curlyIfMultiToken(p.b, allToString_array(o/IFieldsToList._fieldsToList()));
  }
}

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: 267 / 692
Version history: 10 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)