改进 webservice 编码规范
diff --git a/message.go b/message.go
index 4036ad9..302fe6a 100644
--- a/message.go
+++ b/message.go
@@ -1,4 +1,3 @@
-// message.go
package swservice
import (
@@ -9,6 +8,7 @@
"strconv"
)
+// MessageWriter writer
type MessageWriter struct {
FuncNo int `json:"funcno"`
Attributes map[string]interface{}
@@ -18,6 +18,7 @@
Row map[string]interface{}
}
+// NewMessageReader reader
func NewMessageWriter(funcno int) *MessageWriter {
return &MessageWriter{
FuncNo: funcno,
@@ -26,9 +27,13 @@
ColumnDescs: make([]string, 0),
RowData: make([]map[string]interface{}, 0)}
}
+
+// SetAttr set attribute
func (m *MessageWriter) SetAttr(name string, value interface{}) {
m.Attributes[name] = value
}
+
+// AddCol add column data
func (m *MessageWriter) AddCol(name string, value interface{}) {
if m.Row == nil {
m.Row = make(map[string]interface{})
@@ -36,7 +41,7 @@
m.Row[name] = value
}
-func is_contains(k string, slice []string) bool {
+func isContains(k string, slice []string) bool {
for _, v := range slice {
if k == v {
return true
@@ -45,6 +50,7 @@
return false
}
+// AddRow add row
func (m *MessageWriter) AddRow() {
if m.Row == nil {
return
@@ -75,11 +81,11 @@
m.Row = nil
}
-func (m *MessageWriter) serialize_rowdata() {
+func (m *MessageWriter) serializeRowData() {
m.ColumnNames = make([]string, 0)
for _, row := range m.RowData {
for k, _ := range row {
- if !is_contains(k, m.ColumnNames) {
+ if !isContains(k, m.ColumnNames) {
m.ColumnNames = append(m.ColumnNames, k)
}
}
@@ -100,9 +106,10 @@
m.Attributes["rowcnt"] = len(rows)
}
+// Serialize 序列化
func (m *MessageWriter) Serialize() string {
m.Attributes["funcno"] = m.FuncNo
- m.serialize_rowdata()
+ m.serializeRowData()
m.Attributes["colname"] = m.ColumnNames
m.Attributes["coldesc"] = m.ColumnNames
m.Attributes["colcnt"] = len(m.ColumnNames)
@@ -111,6 +118,8 @@
}
//////////////////////////////////////////////////////////////////////
+
+// MessageReader message reader
type MessageReader struct {
FuncNo int
RetCode int
@@ -124,7 +133,7 @@
RowIndex int
}
-func get_value_as_int(value interface{}) int {
+func getValueAsInt(value interface{}) int {
// vtype := reflect.TypeOf(value)
switch value.(type) {
case int:
@@ -139,7 +148,7 @@
}
}
-func get_column_names(data interface{}) (result []string) {
+func getColumnNames(data interface{}) (result []string) {
if data == nil {
return nil
}
@@ -151,7 +160,7 @@
return
}
-func convert_to_int(value interface{}) int {
+func convertToInt(value interface{}) int {
if value == nil {
return 0
}
@@ -168,13 +177,14 @@
}
}
-func convert_to_string(value interface{}) string {
+func convertToString(value interface{}) string {
if value == nil {
return ""
}
return fmt.Sprintf("%v", value)
}
+// NewMessageReader new MessageReader
func NewMessageReader(data []byte) *MessageReader {
var s interface{}
err := json.Unmarshal(data, &s)
@@ -185,14 +195,14 @@
m := &MessageReader{Attributes: make(map[string]interface{}),
RowData: make([]map[string]interface{}, 0)}
- m.FuncNo = convert_to_int(obj["funcno"])
- m.RetCode = convert_to_int(obj["retcode"])
- m.RetMsg = convert_to_string(obj["retmsg"])
- m.DBMsg = convert_to_string(obj["dbmsg"])
- m.ErrName = convert_to_string(obj["errname"])
+ m.FuncNo = convertToInt(obj["funcno"])
+ m.RetCode = convertToInt(obj["retcode"])
+ m.RetMsg = convertToString(obj["retmsg"])
+ m.DBMsg = convertToString(obj["dbmsg"])
+ m.ErrName = convertToString(obj["errname"])
- m.ColumnNames = get_column_names(obj["colname"])
- m.ColumnDescs = get_column_names(obj["coldesc"])
+ m.ColumnNames = getColumnNames(obj["colname"])
+ m.ColumnDescs = getColumnNames(obj["coldesc"])
if rowdata, err := obj["rowdata"]; err {
if rowdata != nil {
@@ -227,14 +237,17 @@
return m
}
+// RowCount row count
func (m *MessageReader) RowCount() int {
return len(m.RowData)
}
+// HasMore has more row
func (m *MessageReader) HasMore() bool {
return m.RowIndex < m.RowCount()
}
+// NextRow next row
func (m *MessageReader) NextRow() error {
m.RowIndex++
if m.RowIndex > m.RowCount() {
@@ -243,15 +256,17 @@
return nil
}
+// GetCol get column
func (m *MessageReader) GetCol(name string) interface{} {
idx := m.RowIndex - 1
- if v, ok := m.RowData[idx][name]; !ok {
+ v, ok := m.RowData[idx][name]
+ if !ok {
return nil
- } else {
- return v
}
+ return v
}
+// GetColAsInt get column data as int
func (m *MessageReader) GetColAsInt(name string) int {
v := m.GetCol(name)
switch v.(type) {
@@ -269,11 +284,13 @@
}
}
+// GetColAsString get column data as string
func (m *MessageReader) GetColAsString(name string) string {
v := m.GetCol(name)
return fmt.Sprintf("%v", v)
}
+// GetColAsDouble get column data as double
func (m *MessageReader) GetColAsDouble(name string) float64 {
v := m.GetCol(name)
switch v.(type) {
@@ -291,10 +308,11 @@
}
}
+// GetAttr get attribute value
func (m *MessageReader) GetAttr(name string) interface{} {
- if v, ok := m.Attributes[name]; !ok {
+ v, ok := m.Attributes[name]
+ if !ok {
return nil
- } else {
- return v
}
+ return v
}