goparse

module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2019 License: MIT

README

Goparse

CircleCI codecov Go Report Card MIT licensed Godoc

This library is inspired by r1chardj0n3s/parse in Python

Parse() is the opposite of fmt.Sprintf()

var s string
err := goparse.Parse("Hello %s", "Hello World").Insert(&s)
if err != nil{
    panic(err)
}
fmt.Println(s)
// Output:
// World

Installation

go get github.com/MizukiSonoko/goparse

Example

Single string

format := "Hello %s"
expected := "World"
var res string
_ = goparse.Parse(format,fmt.Sprintf(format,expected)).Insert(&res)
fmt.Println(s)
// Output:
// World

Multiple string

format := "水樹素子「%s」。秋穂伊織「%s」"
str := "水樹素子「今日は天気が悪いね」。秋穂伊織「そうだね」"
var mizukiMsg, ioriMsg string
_ = goparse.Parse(format,str).Insert(&mizukiMsg, &ioriMsg)
fmt.Println(mizukiMsg)
fmt.Println(ioriMsg)
// Output:
// 今日は天気が悪いね
// そうだね

Base2 integer

format := "Robot says '%d'"
expected := 12345
var num int
_ = goparse.Parse(format, fmt.Sprintf(format, expected)).Insert(&num)
fmt.Println(num)
// Output:
// 12345

Base10 integer

format := "Hello my number is %d"
expected := 100
var num int
_ = goparse.Parse(format, fmt.Sprintf(format, expected)).Insert(&num)
fmt.Println(num)
// Output:
// 100

Base8 integer

format := "Hello my number is %o"
expected := 123
var numOct int
_ = goparse.Parse(format, fmt.Sprintf(format, expected)).Insert(&numOct)
fmt.Println(numOct)
// Output:
// 123

Boolean

format := "I can't tell whether it is %t or %t"
str := "I can't tell whether it is false or true"
var boolRes1, boolRes2 bool
_ = goparse.Parse(format, str).Insert(&boolRes1,&boolRes2)
fmt.Println(boolRes1)
fmt.Println(boolRes2)
// Output:
// false
// true

Struct

Note: arguments struct should expose all attribute

type sample struct {
    Name string
    Value int
};
format := "sample %v"
str := "sample {Hello 123}"
var res sample
_ := goparse.Parse(format, str).Insert(&res)
fmt.Println(res.Name)
fmt.Println(res.Value)
// Output:
// Hello
// 123

Of course, it supports primitive.
string

format := "sample %v"
str := "sample Hello"
var res string
_ := goparse.Parse(format, str).Insert(&res)
fmt.Println(res)
// Output:
// Hello

int

format := "sample %v"
str := "sample 123"
var res int
_ := goparse.Parse(format, str).Insert(&res)
fmt.Println(res)
// Output:
// 123

Error

Invalid type

format := "Hello %s"
str := "Hello sonoko"
var res int // Invalid 

err := goparse.Parse(format, str).Insert(&res)
assert.Error(t, err)

Too ambiguous to invese format

format := "%s%s%s"
str := "abc"
var res string

err := goparse.Parse(format, str).Insert(&res)
assert.Error(t, err)
assert.Contains(t, err.Error(), "ambiguous")

and more...

The format 'verbs'

Cite by https://golang.org/pkg/fmt/

I support this verbs as follows:

Boolean:

[o] %t	the word true or false

Integer:

[o] %b	base 2
[o] %d	base 10
[o] %o	base 8

Floating-point and complex constituents:

[o] %f	decimal point but no exponent, e.g. 123.456

String and slice of bytes (treated equivalently with these verbs):

[o] %s	the uninterpreted bytes of the string or slice

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL