title: Unmarshal hcl to struct using viper
body: <p>Trying to <code>Unmarshal</code> a <code>hcl</code> config file to a struct, using <code>viper</code>, this error is returned: <code>1 error(s) decoding:\n\n* 'NATS' expected a map, got 'slice'</code>. What is missing?</p>
  
  <p>The code:</p>
  
  <pre><code>func lab() {
      var c conf
  
      // config file
      viper.SetConfigName("draft")
      viper.AddConfigPath(".")
      viper.SetConfigType("hcl")
      if err := viper.ReadInConfig(); err != nil {
          log.Error(err)
          return
      }
  
      log.Info(viper.Get("NATS")) // gives [map[port:10041 username:cl1 password:__Psw__4433__ http_port:10044]]
  
      if err := viper.Unmarshal(&amp;c); err != nil {
          log.Error(err)
          return
      }
  
      log.Infow("got conf", "conf", c)
  }
  
  type conf struct {
      NATS struct {
          HTTPPort int
          Port     int
          Username string
          Password string
      }
  }
  </code></pre>
  
  <p>And the config file (<code>draft.hcl</code> inside current directory):</p>
  
  <pre><code>NATS {
      HTTPPort = 10044
      Port     = 10041
      Username = "cl1"
      Password = "__Psw__4433__"
  }
  </code></pre>
  
  <p><strong>Edit</strong></p>
  
  <p>Have checked this struct with <code>hcl</code> package and it gets marshaled/unmarshalled correctly. Also this works correctly with <code>yaml</code> and <code>viper</code>.</p>
  
  <p>There is a difference between these two where <code>log.Info(viper.Get("NATS"))</code> is called. While the <code>hcl</code> version returns a slice of maps, the <code>yaml</code> version returns a map: <code>map[password:__psw__4433__ httpport:10044 port:10041 username:cl1]</code>.</p>