Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/harwoeck/magic
:carousel_horse: magic is an auto-parsing library and competitive coding helper package with batteries included. The library takes care of allocating and populating your memory.
https://github.com/harwoeck/magic
auto-parsing competitive-programming competitive-programming-contests go golang hacktoberfest hacktoberfest2019 input-parsing magic not-for-production parsing reflect reflection reflection-magic
Last synced: 1 day ago
JSON representation
:carousel_horse: magic is an auto-parsing library and competitive coding helper package with batteries included. The library takes care of allocating and populating your memory.
- Host: GitHub
- URL: https://github.com/harwoeck/magic
- Owner: harwoeck
- License: apache-2.0
- Created: 2019-10-10T10:31:14.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-05-02T09:53:03.000Z (over 4 years ago)
- Last Synced: 2024-06-20T15:48:03.004Z (5 months ago)
- Topics: auto-parsing, competitive-programming, competitive-programming-contests, go, golang, hacktoberfest, hacktoberfest2019, input-parsing, magic, not-for-production, parsing, reflect, reflection, reflection-magic
- Language: Go
- Homepage: https://harwoeck.com
- Size: 41 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# magic
`magic` is an auto-parsing library and competitive coding helper package with batteries included. You just need to initialize it with a `SrcProvider` (e.g. a string, file, stream, etc.) and subsequently wish a type to auto-parse. The library takes care of allocating and populating your memory. The sequence of inputs is defined by the memory layout of the reading-type.
**Under no circumstances should you use this package for production code. Use it only during competetive coding, when coding time matters and input parsing is trivial. I originally built this for the [Catalysts Coding Contest (codingcontest.org)](http://codingcontest.org)**
[![GoDoc](https://godoc.org/github.com/harwoeck/magic?status.svg)](https://godoc.org/github.com/harwoeck/magic)
[![Go Report Card](https://goreportcard.com/badge/github.com/harwoeck/magic)](https://goreportcard.com/report/github.com/harwoeck/magic)## Installation
To install `magic`, simly run:
```bash
$ go get -u github.com/harwoeck/magic
```## Quick start
1. Create a `SrcProvider`
```go
src := bufio.NewScanner(strings.NewReader("INPUT"))f, _ := os.Open("/path/to/file.txt")
src := bufio.NewScanner(f)
```2. Create a new `Manager` instance
```go
m := magic.NewManager(src)
```3. `Read`, either primitive and complex types, from the manager
```go
parsedBool := m.ReadBool()
parsedInt := m.ReadInt()
parsedFloat := m.ReadFloat32()
parsedString := m.ReadString()parsedStructList := m.Read([]myStruct).([]myStruct)
```### Complete example
In the following example a complex type (slice of structs) is read and auto-parsed. Within the main struct there are uncaped slices. In these situations `magic` will dynamically try to infer the size by reading an integer from the `SrcProvider` and incremting the cursor position accordingly.
```go
type tx struct {
id int64
authority string
from []txpart
to []txpart
}type txpart struct {
origin string
amount float64
}func main() {
src := bufio.NewScanner(strings.NewReader(
// Image the strings below are single lines within an input-file
"1 FINANCE_MINISTRY 1 YourBankAccount 100 1 MyBankAccount 100\n" +
"2 FINANCE_MINISTRY_DE 2 YourBusinessAcount1 50 YourBusinessAcount2 150 1 MyBusinessAcount 200\n"
))m := magic.NewManager(src)
for _, item := range m.Read([]tx{}).([]tx) {
fmt.Printf("%d %s\n", item.id, item.authority)
}
}
```