You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

string.go 3.3 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2023 The casbin Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package util
  15. import (
  16. "encoding/base64"
  17. "errors"
  18. "fmt"
  19. "io/ioutil"
  20. "math/rand"
  21. "strconv"
  22. "strings"
  23. "time"
  24. "github.com/google/uuid"
  25. )
  26. func IndexAt(s, sep string, n int) int {
  27. idx := strings.Index(s[n:], sep)
  28. if idx > -1 {
  29. idx += n
  30. }
  31. return idx
  32. }
  33. func ParseInt(s string) int {
  34. i, err := strconv.Atoi(s)
  35. if err != nil {
  36. panic(err)
  37. }
  38. return i
  39. }
  40. func ParseIntWithError(s string) (int, error) {
  41. i, err := strconv.Atoi(s)
  42. if err != nil {
  43. return -1, err
  44. }
  45. if i < 0 {
  46. return -1, errors.New("negative version number")
  47. }
  48. return i, nil
  49. }
  50. func ParseFloat(s string) float64 {
  51. f, err := strconv.ParseFloat(s, 64)
  52. if err != nil {
  53. panic(err)
  54. }
  55. return f
  56. }
  57. func GetOwnerAndNameFromId(id string) (string, string) {
  58. tokens := strings.Split(id, "/")
  59. if len(tokens) != 2 {
  60. panic(errors.New("GetOwnerAndNameFromId() error, wrong token count for ID: " + id))
  61. }
  62. return "admin", tokens[1]
  63. }
  64. func GetOwnerAndNameFromId3(id string) (string, string, string) {
  65. tokens := strings.Split(id, "/")
  66. if len(tokens) != 3 {
  67. panic(errors.New("GetOwnerAndNameFromId3() error, wrong token count for ID: " + id))
  68. }
  69. return tokens[0], fmt.Sprintf("%s/%s", tokens[0], tokens[1]), tokens[2]
  70. }
  71. func GetOwnerAndNameFromId3New(id string) (string, string, string) {
  72. tokens := strings.Split(id, "/")
  73. if len(tokens) != 3 {
  74. panic(errors.New("GetOwnerAndNameFromId3New() error, wrong token count for ID: " + id))
  75. }
  76. return tokens[0], tokens[1], tokens[2]
  77. }
  78. func GetIdFromOwnerAndName(owner string, name string) string {
  79. return fmt.Sprintf("%s/%s", owner, name)
  80. }
  81. func GenerateId() string {
  82. return uuid.NewString()
  83. }
  84. func ReadStringFromPath(path string) string {
  85. data, err := ioutil.ReadFile(path)
  86. if err != nil {
  87. panic(err)
  88. }
  89. return string(data)
  90. }
  91. func WriteStringToPath(s string, path string) {
  92. err := ioutil.WriteFile(path, []byte(s), 0o644)
  93. if err != nil {
  94. panic(err)
  95. }
  96. }
  97. func ReadBytesFromPath(path string) []byte {
  98. data, err := ioutil.ReadFile(path)
  99. if err != nil {
  100. panic(err)
  101. }
  102. return data
  103. }
  104. func WriteBytesToPath(b []byte, path string) {
  105. err := ioutil.WriteFile(path, b, 0o644)
  106. if err != nil {
  107. panic(err)
  108. }
  109. }
  110. func DecodeBase64(s string) string {
  111. res, err := base64.StdEncoding.DecodeString(s)
  112. if err != nil {
  113. panic(err)
  114. }
  115. return string(res)
  116. }
  117. func GetRandomName() string {
  118. rand.Seed(time.Now().UnixNano())
  119. const charset = "0123456789abcdefghijklmnopqrstuvwxyz"
  120. result := make([]byte, 6)
  121. for i := range result {
  122. result[i] = charset[rand.Intn(len(charset))]
  123. }
  124. return string(result)
  125. }
  126. func GetId(owner, name string) string {
  127. return fmt.Sprintf("%s/%s", owner, name)
  128. }