astrut.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. package astruct
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. "os"
  7. "reflect"
  8. "strings"
  9. "golang.org/x/exp/maps"
  10. "golang.org/x/exp/slices"
  11. )
  12. type Map map[string]reflect.Kind
  13. // The main structure to use with astruct
  14. //
  15. // This structure contains options to configure behavior and methods in which to parse (JSON) and output (structs in source code)
  16. type Astruct struct {
  17. // Options
  18. Use32Bit bool // Defaults to false, using 64-bit int and 64-bit float, when true, uses 32-bit variants of int and float
  19. UseAny bool // Defaults to true, when true the key word any (which is an alias to interface{}) will be used, when false an error will be thrown (this only applies to arrays/slices who have varying types)
  20. VerboseLogging bool // Dump verbose info into logs (User must setup log first!)
  21. AssumeStruct bool // Defaults to true, when true instead of a map of a basic type a structure will be made (even if the type matches), while false a map could be used in places where the type matches
  22. PrefixStructName string // If not an empty "" string, Structures will be prefixed (for cases where you're going to have a single file produced with various structures from astruct, helps prevent name collisions, which would normally error out)
  23. SuffixForArrayLikes string // If not empty "" string, and it's an array this suffix is added (default's to "Item")
  24. // In-Memory Data (This data is gathered to form final products)
  25. // Almost all of these will be key being field name for in a structure (if one needs to be built) and value of reflect.Kind to determine:
  26. // 1. If the given thing was seen before (as with the case of say an []struct) or if that isn't possible as it's something else (in which a structure might not be possible)
  27. // 2. If a map is actually better represented as a structure (In the case of AssumeStruct the map won't be an option instead a map[string]struct would be used, or even []struct)
  28. MapLikes map[string]Map // Holds map-like structures which might be better defined as structures if they have varying types (else it will retain a single type being a map)
  29. ArrayLikes map[string][]reflect.Kind // Holds array-like structures (this includes slices) which might be able to be defined as arrays (but if varying types are encountered then it might end up as []any or []interface{})
  30. Parent any // Position just above the Current position (for when we're done parsing this "depth", or if this is nil to indicate we're done)
  31. Depth uint // Tracks how many nested structures (map, array/slice) deep we are
  32. }
  33. // Initializes to default options (this also resets the In-Memory Data to an initialized state)
  34. func (A *Astruct) Defaults() {
  35. A.Use32Bit = false
  36. A.UseAny = true
  37. A.VerboseLogging = false
  38. A.AssumeStruct = true
  39. A.PrefixStructName = ""
  40. A.SuffixForArrayLikes = "Item"
  41. A.Init()
  42. }
  43. // This resets the In-Memory Data to an initialized state
  44. func (A *Astruct) Init() {
  45. A.MapLikes = make(map[string]Map)
  46. A.ArrayLikes = make(map[string][]reflect.Kind)
  47. A.Parent = nil
  48. A.Depth = 0
  49. }
  50. // Creates a new Astruct, if given a Prefix for generated structs (only the first one) will be used
  51. func NewAstruct(PrefixStructName ...string) *Astruct {
  52. A := &Astruct{}
  53. A.Defaults()
  54. if len(PrefixStructName) != 0 {
  55. A.PrefixStructName = CamelCase(PrefixStructName[0])
  56. }
  57. return A
  58. }
  59. func (A *Astruct) ReadFile(filename string) error {
  60. data, err := os.ReadFile(filename)
  61. if err != nil {
  62. return fmt.Errorf("astruct.Astruct.ReadFile(filename='%s') On os.ReadFile > %w", filename, err)
  63. }
  64. var root map[string]any
  65. err = json.Unmarshal(data, &root)
  66. if err != nil {
  67. return fmt.Errorf("astruct.Astruct.ReadFile(filename='%s') On json.Unmarshal > %w", filename, err)
  68. }
  69. // Begin parsing...
  70. A.parse(root, "")
  71. return nil
  72. }
  73. func (A *Astruct) parse(at any, name string) error {
  74. switch reflect.TypeOf(at).Kind() {
  75. case reflect.Map:
  76. m := at.(map[string]any)
  77. var last_type reflect.Kind = 0
  78. var same_type bool = true
  79. for k, v := range m {
  80. fmt.Printf(strings.Repeat(" ", int(A.Depth))+"'%s' = %s\n", k, reflect.TypeOf(v).Kind().String())
  81. if same_type {
  82. if last_type == 0 {
  83. last_type = reflect.TypeOf(v).Kind()
  84. } else if reflect.TypeOf(v).Kind() != last_type {
  85. same_type = false
  86. }
  87. }
  88. switch reflect.TypeOf(v).Kind() {
  89. case reflect.Map:
  90. A.Depth += 1
  91. err := A.parse(v, k)
  92. if err != nil {
  93. return err
  94. }
  95. A.Depth -= 1
  96. case reflect.Array, reflect.Slice:
  97. A.Depth += 1
  98. err := A.parse(v, k)
  99. if err != nil {
  100. return err
  101. }
  102. A.Depth -= 1
  103. }
  104. }
  105. A.MapLikes[name] = make(Map)
  106. if !same_type {
  107. for k, v := range m {
  108. if reflect.TypeOf(v).Kind() == reflect.Float32 || reflect.TypeOf(v).Kind() == reflect.Float64 {
  109. f := v.(float64)
  110. if IsInt(f) {
  111. A.MapLikes[name][k] = reflect.Int64
  112. } else {
  113. A.MapLikes[name][k] = reflect.Float64
  114. }
  115. } else {
  116. A.MapLikes[name][k] = reflect.TypeOf(v).Kind()
  117. }
  118. }
  119. } else { // We'll use a empty string "", to indicate this map is same type
  120. A.MapLikes[name][""] = reflect.TypeOf(m[maps.Keys(m)[0]]).Kind()
  121. for k, v := range m {
  122. if reflect.TypeOf(v).Kind() == reflect.Float32 || reflect.TypeOf(v).Kind() == reflect.Float64 {
  123. f := v.(float64)
  124. if IsInt(f) {
  125. A.MapLikes[name][k] = reflect.Int64
  126. } else {
  127. A.MapLikes[name][k] = reflect.Float64
  128. }
  129. } else {
  130. A.MapLikes[name][k] = reflect.TypeOf(v).Kind()
  131. }
  132. }
  133. }
  134. case reflect.Array, reflect.Slice:
  135. a := at.([]any)
  136. var last_type reflect.Kind = 0
  137. var same_type bool = true
  138. for i, v := range a {
  139. fmt.Printf(strings.Repeat(" ", int(A.Depth))+"%d = %s\n", i, reflect.TypeOf(v).Kind().String())
  140. if same_type {
  141. if last_type == 0 {
  142. last_type = reflect.TypeOf(v).Kind()
  143. } else if reflect.TypeOf(v).Kind() != last_type {
  144. same_type = false
  145. }
  146. }
  147. switch reflect.TypeOf(v).Kind() {
  148. case reflect.Map:
  149. A.Depth += 1
  150. err := A.parse(v, A.SuffixForArrayLikes)
  151. if err != nil {
  152. return err
  153. }
  154. A.Depth -= 1
  155. case reflect.Array, reflect.Slice:
  156. A.Depth += 1
  157. err := A.parse(v, A.SuffixForArrayLikes)
  158. if err != nil {
  159. return err
  160. }
  161. A.Depth -= 1
  162. }
  163. }
  164. A.ArrayLikes[name] = []reflect.Kind{}
  165. if !same_type {
  166. for _, v := range a {
  167. if reflect.TypeOf(v).Kind() == reflect.Float32 || reflect.TypeOf(v).Kind() == reflect.Float64 {
  168. f := v.(float64)
  169. if IsInt(f) {
  170. A.ArrayLikes[name] = append(A.ArrayLikes[name], reflect.Int64)
  171. } else {
  172. A.ArrayLikes[name] = append(A.ArrayLikes[name], reflect.Float64)
  173. }
  174. } else {
  175. A.ArrayLikes[name] = append(A.ArrayLikes[name], reflect.TypeOf(v).Kind())
  176. }
  177. }
  178. } else {
  179. v := a[0]
  180. if reflect.TypeOf(v).Kind() == reflect.Float32 || reflect.TypeOf(v).Kind() == reflect.Float64 {
  181. f := v.(float64)
  182. if IsInt(f) {
  183. A.ArrayLikes[name] = append(A.ArrayLikes[name], reflect.Int64)
  184. } else {
  185. A.ArrayLikes[name] = append(A.ArrayLikes[name], reflect.Float64)
  186. }
  187. } else {
  188. A.ArrayLikes[name] = append(A.ArrayLikes[name], reflect.TypeOf(v).Kind())
  189. }
  190. }
  191. default:
  192. fmt.Printf(strings.Repeat(" ", int(A.Depth))+"%s\n", reflect.TypeOf(at).Kind().String())
  193. }
  194. return nil
  195. }
  196. // Checks if the given JSON Number (represented as float64) can be a int64 instead
  197. func IsInt(v float64) bool {
  198. str := fmt.Sprintf("%f", v)
  199. fmt.Printf("'%s' = ", str)
  200. var hit_dot bool = false
  201. for _, r := range str {
  202. if r == '.' {
  203. hit_dot = true
  204. continue
  205. }
  206. if hit_dot {
  207. if r != '0' {
  208. fmt.Println("false")
  209. return false
  210. }
  211. }
  212. }
  213. fmt.Println("true")
  214. return true
  215. }
  216. // Checks if the given data (either map or array/slice) contains the same types
  217. //
  218. // Used to identify if a map or array/slice could be used (unless AssumeStruct is true in which a struct will always be made)
  219. func SameType(data any) bool {
  220. if reflect.TypeOf(data) == reflect.TypeOf(Map{}) {
  221. m := data.(Map)
  222. var last_type reflect.Kind = 0
  223. for _, v := range m {
  224. if last_type == 0 {
  225. last_type = v
  226. } else if v != last_type {
  227. return false
  228. }
  229. }
  230. return true
  231. }
  232. switch reflect.TypeOf(data).Kind() {
  233. case reflect.Map:
  234. m := data.(map[string]any)
  235. var last_type reflect.Kind = 0
  236. for _, v := range m {
  237. if last_type == 0 {
  238. last_type = reflect.TypeOf(v).Kind()
  239. } else if reflect.TypeOf(v).Kind() != last_type {
  240. return false
  241. }
  242. }
  243. return true
  244. case reflect.Array, reflect.Slice:
  245. a := data.([]any)
  246. var last_type reflect.Kind = 0
  247. for _, v := range a {
  248. if last_type == 0 {
  249. last_type = reflect.TypeOf(v).Kind()
  250. } else if reflect.TypeOf(v).Kind() != last_type {
  251. return false
  252. }
  253. }
  254. return true
  255. }
  256. return false
  257. }
  258. func CamelCase(field string) string {
  259. if len(field) == 0 {
  260. return ""
  261. }
  262. var result string
  263. if !strings.Contains(field, " ") || !strings.Contains(field, "-") {
  264. result += strings.ToUpper(string(field[0]))
  265. result += field[1:]
  266. return result
  267. }
  268. result = strings.ToTitle(field)
  269. result = strings.ReplaceAll(result, " ", "")
  270. result = strings.ReplaceAll(result, "-", "")
  271. return result
  272. }
  273. func (A *Astruct) WriteFile(filename, packageName string, permissions os.FileMode) error {
  274. if packageName == "" {
  275. return fmt.Errorf("astruct.Astruct.WriteFile(filename='%s', packageName='%s', permissions=%d) > %s", filename, packageName, permissions, "Missing 'packageName', this must not be empty!")
  276. }
  277. var successful bool = true
  278. fh, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, permissions)
  279. if err != nil {
  280. return fmt.Errorf("astruct.Astruct.WriteFile(filename='%s', packageName='%s', permissions=%d) On os.OpenFile > %w", filename, packageName, permissions, err)
  281. }
  282. defer func() {
  283. fh.Close()
  284. if !successful {
  285. err = os.Remove(filename)
  286. if err != nil {
  287. log.Panicf("astruct.Astruct.WriteFile(filename='%s', packageName='%s', permissions=%d) On os.Remove > %v", filename, packageName, permissions, err)
  288. }
  289. }
  290. }()
  291. fh.WriteString(fmt.Sprintf("package %s\n\n", packageName))
  292. //var slices_to_structs []string = []string{}
  293. var seen []string = []string{}
  294. for name, m := range A.MapLikes {
  295. /*if name == "" { // Ignore the root node for now
  296. continue
  297. }*/
  298. if slices.Contains(seen, name) {
  299. continue
  300. }
  301. _, sameType := m[""] // Check if the empty string exists in this map (if it does it contains same typed data)
  302. if !A.AssumeStruct || !sameType {
  303. fh.WriteString(fmt.Sprintf("type %s%s struct {\n", A.PrefixStructName, CamelCase(name)))
  304. for k, v := range m {
  305. if slices.Contains(maps.Keys(A.MapLikes), k) {
  306. _, sameType2 := A.MapLikes[k][""]
  307. if sameType2 && !A.AssumeStruct {
  308. fh.WriteString(fmt.Sprintf("\t%s map[string]%s\n", CamelCase(k), A.MapLikes[k][""].String()))
  309. seen = append(seen, k)
  310. } else if !sameType2 || A.AssumeStruct && v == reflect.Map {
  311. fh.WriteString(fmt.Sprintf("\t%s %s%s\n", CamelCase(k), A.PrefixStructName, CamelCase(k)))
  312. } else if !sameType2 || A.AssumeStruct && v != reflect.Map {
  313. fh.WriteString(fmt.Sprintf("\t%s %s\n", CamelCase(k), v.String()))
  314. }
  315. } else {
  316. if slices.Contains(maps.Keys(A.ArrayLikes), k) {
  317. a := A.ArrayLikes[k]
  318. if len(a) == 1 {
  319. if a[0] == reflect.Float64 && A.Use32Bit {
  320. fh.WriteString(fmt.Sprintf("\t%s []float32\n", CamelCase(k)))
  321. continue
  322. } else if a[0] == reflect.Int64 && A.Use32Bit {
  323. fh.WriteString(fmt.Sprintf("\t%s []int32\n", CamelCase(k)))
  324. continue
  325. } else if a[0] == reflect.Map {
  326. fh.WriteString(fmt.Sprintf("\t%s []%s%s\n", CamelCase(k), CamelCase(k), A.SuffixForArrayLikes))
  327. continue
  328. }
  329. fh.WriteString(fmt.Sprintf("\t%s []%s\n", CamelCase(k), a[0]))
  330. } else {
  331. //fh.WriteString(fmt.Sprintf("\t%s []%s\n", CamelCase(k), CamelCase(k)))
  332. //slices_to_structs = append(slices_to_structs, k)
  333. if A.UseAny {
  334. fh.WriteString(fmt.Sprintf("\t%s []any\n", CamelCase(k)))
  335. } else {
  336. return fmt.Errorf("astruct.Astruct.WriteFile(filename='%s', packageName='%s', permissions=%d) > Field '%s' is a slice, but it contains varying types (!UseAny, !UseInterface)", filename, packageName, permissions, k)
  337. }
  338. }
  339. } else {
  340. if v == reflect.Float64 && A.Use32Bit {
  341. fh.WriteString(fmt.Sprintf("\t%s float32\n", CamelCase(k)))
  342. continue
  343. } else if v == reflect.Int64 && A.Use32Bit {
  344. fh.WriteString(fmt.Sprintf("\t%s int32\n", CamelCase(k)))
  345. continue
  346. }
  347. fh.WriteString(fmt.Sprintf("\t%s %s\n", CamelCase(k), v.String()))
  348. }
  349. }
  350. }
  351. fh.WriteString("}\n\n")
  352. } else if A.AssumeStruct && sameType {
  353. fh.WriteString(fmt.Sprintf("type %s%s struct {\n", A.PrefixStructName, CamelCase(name)))
  354. t := m[""]
  355. var kind reflect.Kind
  356. if t == reflect.Float64 && A.Use32Bit {
  357. kind = reflect.Float32
  358. } else if t == reflect.Int64 && A.Use32Bit {
  359. kind = reflect.Int32
  360. } else {
  361. kind = t
  362. }
  363. for k := range m {
  364. if k == "" {
  365. continue
  366. }
  367. fh.WriteString(fmt.Sprintf("\t%s %s\n", CamelCase(k), kind))
  368. }
  369. fh.WriteString("}\n\n")
  370. }
  371. }
  372. /*
  373. for _, name := range slices_to_structs {
  374. fh.WriteString(fmt.Sprintf("type %s%s struct {\n", A.PrefixStructName, name))
  375. for _,
  376. }
  377. */
  378. return nil
  379. }