Last active
August 29, 2015 14:05
-
-
Save sumitasok/488c5017f5b62cdda53d to your computer and use it in GitHub Desktop.
A Display of interface and Functional Programming playing hand in hand with OOP - with Golang
This file contains hidden or 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
package reqresp | |
type ReqResp interface { | |
HashableString() string | |
IgnoreList() map[string]string | |
} | |
type Response struct { | |
Type Type | |
Details DataTypes | |
} | |
func (r *Response) IgnoreList() map[string]string{ | |
return map[string]string{{"key1": "val1"}, "key2": "val2"} | |
} | |
func (r *Response) HashableString() { | |
dict := Actions.Dict(r) | |
return fmt.Sprintf("%s|%s", dict["amount"], dict["salt"]) | |
} | |
type Request struct { | |
Type Type | |
Details DataTypes | |
} | |
func (r *Request) HashableString() { | |
dict := Actions.Dict(r) | |
return fmt.Sprintf("%s|%s", dict["key"], dict["salt"]) | |
} | |
func (r *Request) IgnoreList() map[string]string{ | |
return map[string]string{{"keyo": "valo"}, "keyu": "valu"} | |
} | |
// (FP) | |
type Actions struct { | |
} | |
func (a Actions) JSON(req ReqResp []byte{ | |
safe_dict = remove(req.IgnoreList(), dict) | |
return JSON.marshal(safe_dict) | |
} | |
func (a Actions) Dict(req ReqResp) map[string]string { | |
var dict map[string]string | |
for i, detail := req.Details { | |
dict = merge(dict, detail.Dict()) | |
} | |
return dict | |
} | |
// ||| level:(OOP) interface and struct | |
type DataTypes interface { | |
Validate() | |
Dict() map[string]string | |
} | |
type UserDetails struct { | |
Name string | |
} | |
func (ud *UserDetails) Validate() { | |
} | |
func (ud *UserDetails) Dict() map[string]string { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment