This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: calculate and store BUMP in DB
- Loading branch information
1 parent
9540f06
commit 15eb374
Showing
6 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package bux | ||
|
||
import ( | ||
"bytes" | ||
"database/sql/driver" | ||
"encoding/json" | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
// BUMP represents BUMP format | ||
type BUMP struct { | ||
BlockHeight uint64 `json:"blockHeight,string"` | ||
Path []BUMPPathMap `json:"path"` | ||
} | ||
|
||
// BUMPPathMap represents map with pathes | ||
type BUMPPathMap map[string]BUMPPathElement | ||
|
||
// BUMPPathElement represents each BUMP path element | ||
type BUMPPathElement struct { | ||
Hash string `json:"hash,omitempty"` | ||
TxId bool `json:"txid,omitempty"` | ||
Duplicate bool `json:"duplicate,omitempty"` | ||
} | ||
|
||
// Scan scan value into Json, implements sql.Scanner interface | ||
func (m *BUMP) Scan(value interface{}) error { | ||
if value == nil { | ||
return nil | ||
} | ||
|
||
xType := fmt.Sprintf("%T", value) | ||
var byteValue []byte | ||
if xType == ValueTypeString { | ||
byteValue = []byte(value.(string)) | ||
} else { | ||
byteValue = value.([]byte) | ||
} | ||
if bytes.Equal(byteValue, []byte("")) || bytes.Equal(byteValue, []byte("\"\"")) { | ||
return nil | ||
} | ||
|
||
return json.Unmarshal(byteValue, &m) | ||
} | ||
|
||
// Value return json value, implement driver.Valuer interface | ||
func (m BUMP) Value() (driver.Value, error) { | ||
if reflect.DeepEqual(m, BUMP{}) { | ||
return nil, nil | ||
} | ||
marshal, err := json.Marshal(m) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return string(marshal), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters