Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent use of camelCase when dealing with Viper & configuration data #1785

Closed
mdhender opened this issue Jan 17, 2016 · 5 comments
Closed
Labels

Comments

@mdhender
Copy link
Contributor

I was trying to help @xiaomingplus with a themes question (https://discuss.gohugo.io/t/can-hugo-t-params-custom-a-directory/2481) and got confused on the way Hugo is using Viper. It looks like Viper is not case-sensitive, so things like ContentDir are spelled differently in different parts of the code.

viper.GetString("ContentDir")
viper.GetString("contentDir")
$ hugo config | grep -i contentdir
contentdir = "content"

This was really confusing since I thought that SetDefault("ThemesDir", "themes") in hugo/hugo.go would automatically pick up ThemesDir in the config file. If I change a line in hugolib/site.go

func (s *Site) absThemeDir() string {
    return helpers.AbsPathify(viper.GetString("themesDir") + "/" + viper.GetString("theme"))
}

to use ThemesDir rather than themesDir

func (s *Site) absThemeDir() string {
    return helpers.AbsPathify(viper.GetString("ThemesDir") + "/" + viper.GetString("theme"))
}

Hugo/Viper picks up the path like I'd expect. I think that it's a bug, but I don't know which part is the bug - Viper or Hugo.

@moorereason
Copy link
Contributor

I don't think this is a bug. All of the keys are converted to lowercase prior to lookup by Viper. We may be inconsistent in using camelCase or TitleCase when we reference them in our viper.GetString calls, but Viper doesn't care. See here

@moorereason
Copy link
Contributor

@mdhender,
Can we close this issue?

@mdhender
Copy link
Contributor Author

Steve suggested that I create a test case to determine if this is Hugo or Viper. I'm planning on doing that this weekend.

@mdhender
Copy link
Contributor Author

I've tried recreating it and running with the following test in hugolibs/site_test.go and can't duplicate it, so I'll close this issue.

func TestGetString(t *testing.T) {
    viper.Reset()
    defer viper.Reset()

        var myTomlExample = []byte(`

        ThemesDir = "/foo/bar/themes"
    theme = "vanilla"

        `)

        defaultValue := "themes"
        expectedValue := "/foo/bar/themes/vanilla"

        viper.SetDefault("ThemesDir", defaultValue)
        assert.Equal(t, defaultValue, viper.GetString("themesDir"))
        assert.Equal(t, defaultValue, viper.GetString("ThemesDir"))

        viper.SetConfigType("toml")
        viper.ReadConfig(bytes.NewBuffer(myTomlExample))

    assert.Equal(t, expectedValue, helpers.AbsPathify(viper.GetString("themesDir") + "/" + viper.GetString("theme")))
    assert.Equal(t, expectedValue, helpers.AbsPathify(viper.GetString("ThemesDir") + "/" + viper.GetString("theme")))
}

@github-actions
Copy link

github-actions bot commented Apr 9, 2022

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants