Comments (4)
does it really need to be more complex than a map[string]string ?
Original comment by [email protected]
on 29 May 2014 at 12:20
from gcfg.
The reason I want to avoid using map[string]string comes from two factors. The
first is the preference for ease of use for users (those editing the config
files) over ease of use for developers. The second is the preference for APIs
that are easier to use correctly than incorrectly.
The first means that variable names should remain case-insensitive (as in other
sections, and as in git config). This provides simplicity for users by reducing
the things they need to pay attention to when editing the files. (Other
formats/packages such as json may be better suited if/when ease of use for the
developer is preferred over ease of editing.)
Based on this, using a map with a string key would make it very easy for
developers to incorrectly look up elements by the key in a case-sensitive
manner. This in turn creates inconsistency with other sections in the same
file, as well as with other programs using the gcfg format/package.
An additional but relatively minor issue with map[string]string is the value
type -- gcfg aims to support arbitrary types in a type-safe manner. (Even
though the first priority is ease of use for users, gcfg still does aim to do
as much as possible to assist the developer as well.) This itself (ignoring the
aforementioned issues for a second) could be solved by just using map[string]T
(as long as it can be distinguished from a subsection map definition), but it
complicates API design when trying to provide case-insensitive lookup with
user-defined value types.
I have a rough idea of an API that could enforce (or at least strongly
encourage) case-insensitive usage; using an opaque interim type gcfg.Idx for
keys, and having the developer embed an extra field that provides the methods
for lookup (and holds any additional data needed for lookup):
var cfg = &struct {
Alias struct {
gcfg.Idxer
Vals map[gcfg.Idx]MyType
}
}{}
var cfgStr = `
[alias]
lg=log`
func ExampleIdxer() {
if err := gcfg.ReadStringInto(cfg, cfgStr); err != nil {
panic(err)
}
alias := &cfg.Alias
// Idx(string): look up variable with known name (case-insensitive)
if val, ok := alias.Vals[alias.Idx("LG")]; ok {
fmt.Println(val)
}
// Names(): iterate over variables with undefined order and case
for _, n := range alias.Names() {
fmt.Println(n, alias.Vals[alias.Idx(n)])
}
}
Suggestions for enhancements or alternative approaches are welcome.
Original comment by [email protected]
on 1 Jun 2014 at 1:29
from gcfg.
sounds good to me. :) but I have no clue how to implent `gcfg.Idxer` ... do you
have a branch somewhere to test?
Original comment by [email protected]
on 15 Jun 2014 at 1:14
from gcfg.
This issue was updated by revision 1390defd5d3d.
Original comment by [email protected]
on 4 Jul 2014 at 10:21
from gcfg.
Related Issues (15)
- Error messages should be human readable HOT 2
- support for "array of section" HOT 7
- doc: make tag explanation more specific and example discoverable HOT 3
- restructure for adaptability to different formats HOT 2
- [patch] allow "_" in the key HOT 1
- Subsections should be made available in an sorted order HOT 1
- migrate to gopkg.in/sconf/sconf.v0 HOT 6
- Empty comment line causes the next line to be interpreted as comment HOT 2
- do we need to import anything? HOT 2
- config files with 'extra' content HOT 4
- Support for `-` in variable/section names HOT 7
- Writing/setting gcfg files HOT 3
- gcfg failed to get HOT 2
- dashes in field names are always converted to underscores HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gcfg.