katox avatar katox committed 1fb5c5c

make ObjectID serializable via to/fromStr and allow unchecked ObjectID.fromStr

Comments (0)

Files changed (2)

+syntax: glob
+*.orig
+*.rej
+*~
+*.o
+*.svn/*
+*.DS_Store
+*.class
+*.metadata/*
+.idea/
+*.iml
+*.settings/*
 **
 **  ObjectID
 **
+@Serializable { simple = true }
 const class ObjectID 
 {
   static const Int SIZE := 12
     this.time = DateTime.nowUnique.and(0xFFFFFFFF)
     this.machine = IpAddr.local.bytes.toDigest("MD5").readS4.and(0xFFFFFFFF)
   }
+
+  new makeAll(Int inc, Int time, Int machine)
+  { 
+    this.inc = inc
+    this.time = time
+    this.machine = machine
+  }
   
   new fromStream(InStream ins)
   {
     this.inc = ins.readS4.and(0xFFFFFFFF)
   }
   
-  new fromStr(Str s)
+  ** Parse ObjectID from according to the string format defined in mongodb documentation
+  ** If invalid format and checked is false
+  ** return null, otherwise throw Err.
+  static new fromStr(Str s, Bool checked := true)
   {
-    if (!isValid(s)) throw Err("invalid ObjectID format")
-      
-    this.time = s[0..7].toInt(16).and(0xFFFFFFFF)
-    this.machine = s[8..15].toInt(16).and(0xFFFFFFFF)
-    this.inc = s[16..23].toInt(16).and(0xFFFFFFFF)
+    if (!isValid(s)) {
+      if (checked)
+        throw Err("invalid ObjectID format")
+      return null
+    }
+    return makeAll(s[16..23].toInt(16).and(0xFFFFFFFF),
+                   s[0..7].toInt(16).and(0xFFFFFFFF),
+                   s[8..15].toInt(16).and(0xFFFFFFFF))
   }
   
   override Str toStr()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.