Skip to content

Commit

Permalink
पुनरीक्षण
Browse files Browse the repository at this point in the history
  • Loading branch information
boseji committed Sep 5, 2024
1 parent e0679cb commit 40cd740
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 95 deletions.
1 change: 1 addition & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,5 @@
// The name `tppi` has been taken from a story of an imaginative kid
// discovering the mysteries of the Universe and the world all around
// from a remote village in the heart of Karnataka, Bharat(India).

package tppi
69 changes: 29 additions & 40 deletions example_Discover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,62 +44,51 @@ package tppi_test

import (
"fmt"
"strings"

"github.com/boseji/go-tppi"
)

func (b *myBool) Recover(Type, Tag, Data string) error {
if Type != myBoolTypeSignature {
return fmt.Errorf("invalid bool type")
}
b.Tag = Tag
if Data == myBoolDataTrue {
b.bool = true
} else if Data == myBoolDataFalse {
b.bool = false
} else {
return fmt.Errorf("invalid bool data")
}
return nil
}

func (m *myStruct) Recover(Type, Tag, Data string) (err error) {
if Type != myStructTypeSig {
return fmt.Errorf("wrong type supplied")
}
_ = Tag
sa := strings.Split(Data, " ")
if len(sa) != 3 {
return fmt.Errorf("malformed data")
}

_, err = fmt.Sscanf(Data, myStructFormat, &m.TimeStamp, &m.Value, &m.Topic)
return
}

func ExampleDiscover() {
b := myBool{}
b := struct {
bool
Tag string
}{}
srcBool := "B~bit%7E5~1"
err := tppi.Discover(srcBool, b.Recover)
err := tppi.Discover(srcBool, func(s1, s2, s3 string) error {
if s1 != "B" {
return fmt.Errorf("wrong type")
}
b.Tag = s2
if s3 == "1" {
b.bool = true
} else if s3 == "0" {
b.bool = false
} else {
return fmt.Errorf("unknown data")
}
return nil
})
if err != nil {
fmt.Printf("failed to Discover - %v\n", err)
return
}
fmt.Printf("%q Transforms back %#v\n", srcBool, b)

m := myStruct{}
srcStruct := "MSTR~myStruct~134000000 12.345000 \"sensor/12\""
err = tppi.Discover(srcStruct, m.Recover)
srcFloat := "F~12.34560"
f := 0.00
err = tppi.Discover(srcFloat, func(s1, s2, s3 string) error {
_ = s1
_ = s2
_, err := fmt.Sscanf(s3, "%f", &f)
return err
})
if err != nil {
fmt.Printf("failed to Discover - %v\n", err)
return
}
fmt.Printf("%q Transforms back\n", srcStruct)
fmt.Printf("%#v", m)
fmt.Printf("%q Transforms back %#v\n", srcFloat, f)

// Output:
// "B~bit%7E5~1" Transforms back tppi_test.myBool{bool:true, Tag:"bit~5"}
// "MSTR~myStruct~134000000 12.345000 \"sensor/12\"" Transforms back
// tppi_test.myStruct{TimeStamp:134000000, Value:12.345, Topic:"sensor/12"}
// "B~bit%7E5~1" Transforms back struct { bool; Tag string }{bool:true, Tag:"bit~5"}
// "F~12.34560" Transforms back 12.3456
}
12 changes: 10 additions & 2 deletions example_Protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,23 @@ import (
)

func ExampleAssemble() {
ba := []myBool{
ba := []struct {
bool
Tag string
}{
{true, "Bit~0"},
{false, "Bit-1"},
{false, "Bit-2"},
{true, "Bit-3"},
}
sa := make([]string, 0, len(ba))
for _, b := range ba {
sa = append(sa, tppi.Specify(b.Type(), b.Tag, b.String))
sa = append(sa, tppi.Specify("B", b.Tag, func() string {
if b.bool {
return "1"
}
return "0"
}))
}
s := tppi.Assemble(sa...)
s = tppi.PacketJoin(s)
Expand Down
70 changes: 17 additions & 53 deletions example_Specify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,66 +44,30 @@ package tppi_test

import (
"fmt"
"time"

"github.com/boseji/go-tppi"
)

type myBool struct {
bool
Tag string
}

const (
myBoolTypeSignature = "B"
myBoolDataTrue = "1"
myBoolDataFalse = "0"
)

func (b myBool) Type() string {
return myBoolTypeSignature
}

func (b myBool) String() string {
if b.bool {
return myBoolDataTrue
}
return myBoolDataFalse
}

type myStruct struct {
TimeStamp time.Duration
Value float32
Topic string
}

const (
myStructTypeSig = "MSTR"
myStructFormat = "%d %f %q"
)

func (m myStruct) Type() string {
return myStructTypeSig
}

func (m myStruct) String() string {
return fmt.Sprintf(myStructFormat, m.TimeStamp, m.Value, m.Topic)
}

func ExampleSpecify() {
b := myBool{true, "bit~5"}
s := tppi.Specify(b.Type(), b.Tag, b.String)
b := struct {
bool
Tag string
}{true, "bit~5"}
s := tppi.Specify("B", b.Tag, func() string {
if b.bool {
return "1"
}
return "0"
})
fmt.Printf("%#v transforms into %q\n", b, s)

m := myStruct{134 * time.Millisecond, 12.345, "sensor/12"}
sa := tppi.Specify(m.Type(), "myStruct", m.String)
fmt.Printf("%#v\n", m)
fmt.Println("transforms into")
fmt.Println(sa)
f := 12.3456
s2 := tppi.Specify("F", "", func() string {
return fmt.Sprintf("%2.5f", f)
})
fmt.Printf("%#v transforms into %q\n", f, s2)

// Output:
// tppi_test.myBool{bool:true, Tag:"bit~5"} transforms into "B~bit%7E5~1"
// tppi_test.myStruct{TimeStamp:134000000, Value:12.345, Topic:"sensor/12"}
// transforms into
// MSTR~myStruct~134000000 12.345000 "sensor/12"
// struct { bool; Tag string }{bool:true, Tag:"bit~5"} transforms into "B~bit%7E5~1"
// 12.3456 transforms into "F~12.34560"
}

0 comments on commit 40cd740

Please sign in to comment.