本站源代码
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

157 lignes
5.0KB

  1. // Copyright 2018 Frank Schroeder. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package properties provides functions for reading and writing
  5. // ISO-8859-1 and UTF-8 encoded .properties files and has
  6. // support for recursive property expansion.
  7. //
  8. // Java properties files are ISO-8859-1 encoded and use Unicode
  9. // literals for characters outside the ISO character set. Unicode
  10. // literals can be used in UTF-8 encoded properties files but
  11. // aren't necessary.
  12. //
  13. // To load a single properties file use MustLoadFile():
  14. //
  15. // p := properties.MustLoadFile(filename, properties.UTF8)
  16. //
  17. // To load multiple properties files use MustLoadFiles()
  18. // which loads the files in the given order and merges the
  19. // result. Missing properties files can be ignored if the
  20. // 'ignoreMissing' flag is set to true.
  21. //
  22. // Filenames can contain environment variables which are expanded
  23. // before loading.
  24. //
  25. // f1 := "/etc/myapp/myapp.conf"
  26. // f2 := "/home/${USER}/myapp.conf"
  27. // p := MustLoadFiles([]string{f1, f2}, properties.UTF8, true)
  28. //
  29. // All of the different key/value delimiters ' ', ':' and '=' are
  30. // supported as well as the comment characters '!' and '#' and
  31. // multi-line values.
  32. //
  33. // ! this is a comment
  34. // # and so is this
  35. //
  36. // # the following expressions are equal
  37. // key value
  38. // key=value
  39. // key:value
  40. // key = value
  41. // key : value
  42. // key = val\
  43. // ue
  44. //
  45. // Properties stores all comments preceding a key and provides
  46. // GetComments() and SetComments() methods to retrieve and
  47. // update them. The convenience functions GetComment() and
  48. // SetComment() allow access to the last comment. The
  49. // WriteComment() method writes properties files including
  50. // the comments and with the keys in the original order.
  51. // This can be used for sanitizing properties files.
  52. //
  53. // Property expansion is recursive and circular references
  54. // and malformed expressions are not allowed and cause an
  55. // error. Expansion of environment variables is supported.
  56. //
  57. // # standard property
  58. // key = value
  59. //
  60. // # property expansion: key2 = value
  61. // key2 = ${key}
  62. //
  63. // # recursive expansion: key3 = value
  64. // key3 = ${key2}
  65. //
  66. // # circular reference (error)
  67. // key = ${key}
  68. //
  69. // # malformed expression (error)
  70. // key = ${ke
  71. //
  72. // # refers to the users' home dir
  73. // home = ${HOME}
  74. //
  75. // # local key takes precedence over env var: u = foo
  76. // USER = foo
  77. // u = ${USER}
  78. //
  79. // The default property expansion format is ${key} but can be
  80. // changed by setting different pre- and postfix values on the
  81. // Properties object.
  82. //
  83. // p := properties.NewProperties()
  84. // p.Prefix = "#["
  85. // p.Postfix = "]#"
  86. //
  87. // Properties provides convenience functions for getting typed
  88. // values with default values if the key does not exist or the
  89. // type conversion failed.
  90. //
  91. // # Returns true if the value is either "1", "on", "yes" or "true"
  92. // # Returns false for every other value and the default value if
  93. // # the key does not exist.
  94. // v = p.GetBool("key", false)
  95. //
  96. // # Returns the value if the key exists and the format conversion
  97. // # was successful. Otherwise, the default value is returned.
  98. // v = p.GetInt64("key", 999)
  99. // v = p.GetUint64("key", 999)
  100. // v = p.GetFloat64("key", 123.0)
  101. // v = p.GetString("key", "def")
  102. // v = p.GetDuration("key", 999)
  103. //
  104. // As an alternative properties may be applied with the standard
  105. // library's flag implementation at any time.
  106. //
  107. // # Standard configuration
  108. // v = flag.Int("key", 999, "help message")
  109. // flag.Parse()
  110. //
  111. // # Merge p into the flag set
  112. // p.MustFlag(flag.CommandLine)
  113. //
  114. // Properties provides several MustXXX() convenience functions
  115. // which will terminate the app if an error occurs. The behavior
  116. // of the failure is configurable and the default is to call
  117. // log.Fatal(err). To have the MustXXX() functions panic instead
  118. // of logging the error set a different ErrorHandler before
  119. // you use the Properties package.
  120. //
  121. // properties.ErrorHandler = properties.PanicHandler
  122. //
  123. // # Will panic instead of logging an error
  124. // p := properties.MustLoadFile("config.properties")
  125. //
  126. // You can also provide your own ErrorHandler function. The only requirement
  127. // is that the error handler function must exit after handling the error.
  128. //
  129. // properties.ErrorHandler = func(err error) {
  130. // fmt.Println(err)
  131. // os.Exit(1)
  132. // }
  133. //
  134. // # Will write to stdout and then exit
  135. // p := properties.MustLoadFile("config.properties")
  136. //
  137. // Properties can also be loaded into a struct via the `Decode`
  138. // method, e.g.
  139. //
  140. // type S struct {
  141. // A string `properties:"a,default=foo"`
  142. // D time.Duration `properties:"timeout,default=5s"`
  143. // E time.Time `properties:"expires,layout=2006-01-02,default=2015-01-01"`
  144. // }
  145. //
  146. // See `Decode()` method for the full documentation.
  147. //
  148. // The following documents provide a description of the properties
  149. // file format.
  150. //
  151. // http://en.wikipedia.org/wiki/.properties
  152. //
  153. // http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load%28java.io.Reader%29
  154. //
  155. package properties
上海开阖软件有限公司 沪ICP备12045867号-1