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

72
LINES

< > BotCompany Repo | #1026007 // _csetField - used by cset [LIVE]

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

Libraryless. Click here for Pure Java version (17417L/106K).

// returns true if change
sbool _csetField(Concept c, S field, O value) ctex {
  Field f = setOpt_findField(c.getClass(), field);
  //print("cset: " + c.id + " " + field + " " + struct(value) + " " + f);
  if (value instanceof RC) value = c._concepts.getConcept((RC) value);
  value = deref(value);
  
  if (value instanceof S && l((S) value) >= concepts_internStringsLongerThan) value = intern((S) value);
  
  if (f == null) {
    // dynamic field (undeclared)
    
    assertIdentifier(field);
    O oldVal = mapGet(c.fieldValues, field);
    
    if (value cast Concept) {
      if (oldVal instanceof Concept.Ref)
        // change existing reference
        ret ((Concept.Ref) oldVal).set(value);
      else {
        // overwrite non-reference value if any,
        // create new reference
        dynamicObject_setRawFieldValue(c, field, c.new Ref(value));
        c.change(); true;
      }
    } else {
      // value is not a concept
      
      // if it was a reference, cleanly delete it
      if (oldVal cast Concept.Ref) oldVal.unindexAndDrop();
    
      if (eq(oldVal, value)) false;
      
      if (isConceptList(value) && nempty(value/L)) {
        // TODO: clean-up etc
        dynamicObject_setRawFieldValue(c, field, c.new RefL(value/L));
        c.change(); true;
      }
      
      if (value == null) {
        // delete field
        dynamicObject_dropRawField(c, field);
      } else {
        // update or create field
        if (!isPersistable(value))
          fail("Can't persist: " + c + "." + field + " = "+ value);

        dynamicObject_setRawFieldValue(c, field, value);
      }
      c.change(); true;
    }
  } else if (isSubtypeOf(f.getType(), Concept.Ref.class)) {
    // Concept.Ref magic
    ((Concept.Ref) f.get(c)).set((Concept) derefRef(value));
    c.change(); true;
  } else if (isSubtypeOf(f.getType(), Concept.RefL.class)) {
    // Concept.RefL magic
    ((Concept.RefL) f.get(c)).replaceWithList(lmap derefRef((L) value));
    c.change(); true;
  } else {
    O old = f.get(c);
    if (neq(value, old)) {
      bool isTransient = isTransient(f);
      if (!isTransient && !isPersistable(value))
        fail("Can't persist: " + c + "." + field + " = "+ value);
      f.set(c, value);
      if (!isTransient) c.change(); 
      true;
    }
  }
  false;
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1026007
Snippet name: _csetField - used by cset [LIVE]
Eternal ID of this version: #1026007/20
Text MD5: 41a999cd7a3d7352d971cd6d98d73b6a
Transpilation MD5: e2213703e048d210ca9a41207fbdb50c
Author: stefan
Category: javax / concepts
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-08-30 18:52:39
Source code size: 2377 bytes / 72 lines
Pitched / IR pitched: No / No
Views / Downloads: 431 / 633
Version history: 19 change(s)
Referenced in: [show references]