View Single Post
08/13/14, 12:41 PM   #5
Sasky
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 231
Versioning shouldn't be too hard to implement and I'd think it'd be almost required to implement if looking to replace the ZO_SavedVars. Here's some ideas for doing versioning and default values:

Store version number
I think this would be best done per data group. Account-wide and OS-wide you could put one level up from where the data is stored easily. Per character you either need to put in with the data (and just have the version field be reserved) or put under the account level and prefix with character name.

New format sample:
Lua Code:
  1. SavedVariableName = {
  2.     ["libASV_Strcuture_1"] = {
  3.         libASV_OS_version = 3,
  4.         ["libASV_OS_user_wide"] = {
  5.              --data goes here
  6.         },
  7.         --Showing first option for characters here
  8.         ["@acountname1"] = {
  9.             libASV_version = 1, --For AccountWide variables
  10.             ["libASV_AccountWide"] = {},
  11.             version_Character1 = 1,
  12.             ["Character1"] = {},
  13.             version_Character2 = 1,
  14.             ["Character2"] = {}
  15.         },
  16.         --Showing second option for characters here
  17.         ["@acountname2"] = {
  18.             libASV_version = 1,
  19.             ["libASV_AccountWide"] = {},
  20.             ["Character 3"] = {
  21.                 libASV_version = 1, --Reserved field
  22.                 --Other data
  23.             }
  24.         }
  25.    }
  26. }

Access functions
You'd need to pass in the version number and an upgrade function. You could do a variable-wide upgrade, but that would expose your underlying structure, which is hardly ideal.
:AccessOSUserWide("SavedVariableName", version, [defaults], [upgradeFunction])
:AccountWide("SavedVariableName", version, [defaults], [upgradeFunction], [accountOverride])
:AllCharacters("SavedVariableName", [accountOverride])
:CharacterWide("SavedVariableName", version, [defaults], [upgradeFunction], [accountOverride], [characterOverride])

The function behavior (except AllCharacters) would be:
Code:
IF not initialized THEN
    COPY in default values
    RETURN table
END

IF version different THEN
   IF upgradeFunction THEN
       CALL upgrade function on table
   ELSE
       COPY in default values
   END
END

RETURN table
You'd need to use the deep copy utility function for setting the default variables.

Upgrade function
Parameters:
- existing version
- existing table

Result/return:
Could either have the function modify the table in-place (more efficient) or just return the new table (susceptible to errors if not familiar with tables are reference).

Again, you don't know what to do for upgrade in the default, so you'd just reset to defaults.