MagicMirror² v2.13.0 is available! For more information about this release, check out this topic.

mergeConfig() function


  • Module Developer

    configMerge

    Hi, this kind of script can help developer to merge userconfig and default module config.
    I use it in my ALL modules !
    I make it public in a way to help you 😉

    Using:

    In main module core, you can put it in start() function

    this.config = configMerge({}, this.defaults, this.config)
    

    your default config will be merge with defined user config !
    If the defined user config don’t define all config value (minimal config for exemple), this script will merge it to do an entire config
    So you can send your config to node_helper.js without fear of having undefined values

    Arg Number Function
    Arg 1 Initial objet
    Arg 2 Config model
    Arg 3 Config to merge

    Load the script in MagicMirror

    You have to load the script on the main core of your module

    getScripts: function () {
      return [
        "configMerge.js"
      ]
    },
    

    Why using it ?

    Object.assign() function don’t to all job

    it don’t merge all thing in depth

    -> object in object and array is not merging

    Note:

    You can also use the Minified code version mergeConfig.min.js

    How download it:

    In your terminal use the wget command

    wget https://raw.githubusercontent.com/bugsounet/configMerge/master/configMerge.js
    

    or for Minified version

    wget https://raw.githubusercontent.com/bugsounet/configMerge/master/configMerge.min.js
    


  • @Bugsounet I don’t understand


  • Module Developer

    I will code a sample wait 😉

    LoOOooL, now this script is not needed 🙂
    now MM v2.12.0 have solve this issue 🙂
    but still in v2.11.x

    I understand why you say i don't understand

    I see this at now, pfff i don’t read ChangeLog lol



  • @Bugsounet also, _loadash lib is previously loaded, and loadash.merge does the deep merge, while Object.Assign does not.

    and it only matters for modules with big deep config structures. like GA


  • Module Developer

    it’s new because in v2.11.x, I have to use mergeConfig()



  • @Bugsounet lodash.merge loaded before 2.12.

    just saying it’s easy, if u need to do this, without another script


  • Module Developer

    So I do some another test and not worked without configMerge()

    Module name : MMM-Freebox (in dev for new version)
    in MMM-Freebox.js:

      defaults: {
        updateDelay:  5 * 1000,
        token: "",
        activeOnly: false,
        showIcon: true,
        showButton: true,
        showBandWidth: true,
        showRate: true,
        showClient: true,
        showClientRate: true,
        showClientCnxType: true,
        showFreePlayer: true,
        showMissedCall: true,
        showVPNUsers: true,
        maxMissed: 3,
        showIP: true,
        showPing: true,
        pingAdress: "google.fr",
        textWidth: 250,
        excludeMac: [],
        sortBy: null,
        debug: false,
        verbose: false,
        dev: false,
        debitText: "Débit total utilisé : ",
        player : {
          showPlayerInfo: false,
          // depuis le firmware 4.2.3, problemes d'affichage des logos
          // essayez avec les ips :  "192.168.0.254" (l'ip du freebox server)
          //                         "mafreebox.free.fr" ou le resultat de l'ip de mafreebox.free.fr
          //                         "212.27.38.253" l'ip de mafreebox.free.fr (a voir si cela fonctionne pour vous)
          ServerIP: "212.27.38.253",
          UseEPGDayURL: true,
          EPGDelay: 2* 60 *60 *1000
        }
      },
    

    I try to merge config with MMM-Freebox.js :

      start: function () {
        this.config = Object.assign({}, this.defaults, this.config)
    

    I send config to node_helper.js with:

      notificationReceived: function (notification, payload) {
        switch(notification) {
          case "DOM_OBJECTS_CREATED":
            this.sendSocketNotification("INIT", this.config)
            break
        }
      },
    

    module configuration in config.js:

    {
      module: "MMM-Freebox",
      disabled: false,
      header: "INFO Freebox",
      position: "top_center",
      config: {
        token: "xxxx",
        updateDelay:  5 * 1000,
        activeOnly: false,
        sortBy: "type",
        showButton: true,
        debug: true,
        //dev: true,
        //verbose: true,
        showVPNUsers: true,
        textWidth: 250,
        player: {
          showPlayerInfo: true
        }
      }
    },
    

    result in node_helper.js:

    [2020-08-10 16:53:52.705] [LOG]    {
      updateDelay: 5000,
      token: 'xxxx',
      activeOnly: false,
      showIcon: true,
      showButton: true,
      showBandWidth: true,
      showRate: true,
      showClient: true,
      showClientRate: true,
      showClientCnxType: true,
      showFreePlayer: true,
      showMissedCall: true,
      showVPNUsers: true,
      maxMissed: 3,
      showIP: true,
      showPing: true,
      pingAdress: 'google.fr',
      textWidth: 250,
      excludeMac: [],
      sortBy: 'type',
      debug: true,
      verbose: false,
      dev: false,
      debitText: 'Débit total utilisé : ',
      player: { showPlayerInfo: true }
    }
    

    so config is not merged !

    now … Let’s try with configMerge() with SAME configuration
    just modify :

      start: function () {
        this.config = configMerge({}, this.defaults, this.config)
    

    result in node_helper.js:

    [2020-08-10 17:08:14.984] [LOG]    {
      updateDelay: 5000,
      token: 'xxxx',
      activeOnly: false,
      showIcon: true,
      showButton: true,
      showBandWidth: true,
      showRate: true,
      showClient: true,
      showClientRate: true,
      showClientCnxType: true,
      showFreePlayer: true,
      showMissedCall: true,
      showVPNUsers: true,
      maxMissed: 3,
      showIP: true,
      showPing: true,
      pingAdress: 'google.fr',
      textWidth: 250,
      excludeMac: [],
      sortBy: 'type',
      debug: true,
      verbose: false,
      dev: false,
      debitText: 'Débit total utilisé : ',
      player: {
        showPlayerInfo: true,
        ServerIP: '212.27.38.253',
        UseEPGDayURL: true,
        EPGDelay: 7200000
      }
    }
    

    You can’t see that player: {} value is now set and merged with default value

    @MichMich :

    in MM core js/module.js

    	/* setConfig(config)
    	 * Set the module config and combine it with the module defaults.
    	 *
    	 * argument config object - Module config.
    	 */
    	setConfig: function (config) {
    		this.config = Object.assign({}, this.defaults, config);
    	},
    

    Can I send you a patch with configMerge() script for solve this developer issue (It take head !) ?



  • @Bugsounet open a PR, describe the problem, and your solution


  • Module Developer

    sure, i just open it


  • Module Developer

    Ok, so, after one month

    1. No news on this topic (I have mentionned @MichMich but …)
    2. I have open an issue, no news
    3. i have make a PR, euh no news too ??

    So i decide I close Issue and PR
    it’s not a problem for me… i use my own tools 🙂

    In this forum,
    There are not many developers left …
    So avoid listening to them, you would have even less 🙂
    I don’t know what you are looking for 🙂

    For the moment, you have one motivated!
    This is not likely to continue for long …

    @bugsounet for @MichMich


Log in to reply