feat(mapping): 建立码表映射基线(T-102)
This commit is contained in:
+244
@@ -0,0 +1,244 @@
|
||||
package mapping
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Direction string
|
||||
|
||||
const (
|
||||
DirectionPHISToOSI Direction = "phis_to_osi"
|
||||
DirectionOSIToPHIS Direction = "osi_to_phis"
|
||||
)
|
||||
|
||||
type ValidationError struct {
|
||||
Field string
|
||||
DictName string
|
||||
Direction Direction
|
||||
Code string
|
||||
}
|
||||
|
||||
func (e ValidationError) Error() string {
|
||||
return fmt.Sprintf("mapping %s %s unknown code %q for %s", e.DictName, e.Direction, e.Code, e.Field)
|
||||
}
|
||||
|
||||
type Entry struct {
|
||||
PHIS string
|
||||
OSI string
|
||||
Name string
|
||||
}
|
||||
|
||||
type Dict struct {
|
||||
Name string
|
||||
Field string
|
||||
Entries []Entry
|
||||
}
|
||||
|
||||
func (d Dict) PHISToOSI(code string) (string, error) {
|
||||
for _, entry := range d.Entries {
|
||||
if entry.PHIS == code {
|
||||
return entry.OSI, nil
|
||||
}
|
||||
}
|
||||
return "", ValidationError{Field: d.Field, DictName: d.Name, Direction: DirectionPHISToOSI, Code: code}
|
||||
}
|
||||
|
||||
func (d Dict) OSIToPHIS(code string) (string, error) {
|
||||
for _, entry := range d.Entries {
|
||||
if entry.OSI == code {
|
||||
return entry.PHIS, nil
|
||||
}
|
||||
}
|
||||
return "", ValidationError{Field: d.Field, DictName: d.Name, Direction: DirectionOSIToPHIS, Code: code}
|
||||
}
|
||||
|
||||
func (d Dict) EntryByOSI(code string) (Entry, bool) {
|
||||
for _, entry := range d.Entries {
|
||||
if entry.OSI == code {
|
||||
return entry, true
|
||||
}
|
||||
}
|
||||
return Entry{}, false
|
||||
}
|
||||
|
||||
func (d Dict) Len() int {
|
||||
return len(d.Entries)
|
||||
}
|
||||
|
||||
func (d Dict) Validate() error {
|
||||
phisSeen := make(map[string]bool, len(d.Entries))
|
||||
osiSeen := make(map[string]bool, len(d.Entries))
|
||||
for _, entry := range d.Entries {
|
||||
if entry.PHIS == "" || entry.OSI == "" {
|
||||
return fmt.Errorf("%s contains empty code: %#v", d.Name, entry)
|
||||
}
|
||||
if phisSeen[entry.PHIS] {
|
||||
return fmt.Errorf("%s duplicate PHIS code %q", d.Name, entry.PHIS)
|
||||
}
|
||||
if osiSeen[entry.OSI] {
|
||||
return fmt.Errorf("%s duplicate OSI code %q", d.Name, entry.OSI)
|
||||
}
|
||||
phisSeen[entry.PHIS] = true
|
||||
osiSeen[entry.OSI] = true
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func sameCodeDict(name, field string, entries ...Entry) Dict {
|
||||
for i := range entries {
|
||||
if entries[i].PHIS == "" {
|
||||
entries[i].PHIS = entries[i].OSI
|
||||
}
|
||||
}
|
||||
return Dict{Name: name, Field: field, Entries: entries}
|
||||
}
|
||||
|
||||
var SexDict = sameCodeDict("sex", "sexCode",
|
||||
Entry{OSI: "0", Name: "未知"},
|
||||
Entry{OSI: "1", Name: "男"},
|
||||
Entry{OSI: "2", Name: "女"},
|
||||
Entry{OSI: "9", Name: "未说明"},
|
||||
)
|
||||
|
||||
var BloodTypeDict = sameCodeDict("bloodType", "bloodTypeCode",
|
||||
Entry{OSI: "1", Name: "A"},
|
||||
Entry{OSI: "2", Name: "B"},
|
||||
Entry{OSI: "3", Name: "O"},
|
||||
Entry{OSI: "4", Name: "AB"},
|
||||
Entry{OSI: "5", Name: "不详"},
|
||||
)
|
||||
|
||||
var RhBloodDict = sameCodeDict("rhBlood", "rhBloodCode",
|
||||
Entry{OSI: "1", Name: "阳性"},
|
||||
Entry{OSI: "2", Name: "阴性"},
|
||||
Entry{OSI: "3", Name: "不详"},
|
||||
)
|
||||
|
||||
var EducationDict = sameCodeDict("education", "educationCode",
|
||||
Entry{OSI: "10", Name: "研究生"},
|
||||
Entry{OSI: "20", Name: "本科"},
|
||||
Entry{OSI: "31", Name: "大专"},
|
||||
Entry{OSI: "41", Name: "中专"},
|
||||
Entry{OSI: "47", Name: "技校"},
|
||||
Entry{OSI: "60", Name: "高中"},
|
||||
Entry{OSI: "70", Name: "初中"},
|
||||
Entry{OSI: "80", Name: "小学"},
|
||||
Entry{OSI: "90", Name: "文盲"},
|
||||
Entry{OSI: "91", Name: "不详"},
|
||||
)
|
||||
|
||||
var WorkDict = sameCodeDict("work", "workCode",
|
||||
Entry{OSI: "0", Name: "负责人"},
|
||||
Entry{OSI: "1", Name: "专业技术人员"},
|
||||
Entry{OSI: "2", Name: "专业技术人员"},
|
||||
Entry{OSI: "3", Name: "办事人员"},
|
||||
Entry{OSI: "4", Name: "商业服务业人员"},
|
||||
Entry{OSI: "5", Name: "农林牧渔人员"},
|
||||
Entry{OSI: "9", Name: "生产运输人员"},
|
||||
Entry{OSI: "X", Name: "军人"},
|
||||
Entry{OSI: "Y", Name: "其他"},
|
||||
Entry{OSI: "8", Name: "无职业"},
|
||||
)
|
||||
|
||||
var MaritalStatusDict = sameCodeDict("maritalStatus", "maritalStatusCode",
|
||||
Entry{OSI: "10", Name: "未婚"},
|
||||
Entry{OSI: "20", Name: "已婚"},
|
||||
Entry{OSI: "30", Name: "丧偶"},
|
||||
Entry{OSI: "40", Name: "离婚"},
|
||||
Entry{OSI: "90", Name: "未说明"},
|
||||
)
|
||||
|
||||
var InsuranceDict = sameCodeDict("insurance", "insuranceCode",
|
||||
Entry{OSI: "01", Name: "城镇职工"},
|
||||
Entry{OSI: "02", Name: "城乡居民"},
|
||||
Entry{OSI: "04", Name: "贫困救助"},
|
||||
Entry{OSI: "05", Name: "商业"},
|
||||
Entry{OSI: "06", Name: "全公费"},
|
||||
Entry{OSI: "07", Name: "全自费"},
|
||||
Entry{OSI: "99", Name: "其他"},
|
||||
)
|
||||
|
||||
var PersonSignDict = sameCodeDict("personSign", "personSign",
|
||||
Entry{OSI: "PU", Name: "普通"},
|
||||
Entry{OSI: "GRQY", Name: "已签约"},
|
||||
Entry{OSI: "LAO", Name: "老年"},
|
||||
Entry{OSI: "GAO", Name: "高血压"},
|
||||
Entry{OSI: "TANG", Name: "糖尿病"},
|
||||
Entry{OSI: "FU", Name: "孕产妇"},
|
||||
Entry{OSI: "ER", Name: "儿童"},
|
||||
Entry{OSI: "FEI", Name: "肺结核"},
|
||||
Entry{OSI: "JING", Name: "精神障碍"},
|
||||
Entry{OSI: "CAN", Name: "残疾"},
|
||||
)
|
||||
|
||||
var NationDict = sameCodeDict("nation", "nationCode",
|
||||
Entry{OSI: "01", Name: "汉族"},
|
||||
Entry{OSI: "02", Name: "蒙古族"},
|
||||
Entry{OSI: "03", Name: "回族"},
|
||||
Entry{OSI: "04", Name: "藏族"},
|
||||
Entry{OSI: "05", Name: "维吾尔族"},
|
||||
Entry{OSI: "06", Name: "苗族"},
|
||||
Entry{OSI: "07", Name: "彝族"},
|
||||
Entry{OSI: "08", Name: "壮族"},
|
||||
Entry{OSI: "09", Name: "布依族"},
|
||||
Entry{OSI: "10", Name: "朝鲜族"},
|
||||
Entry{OSI: "11", Name: "满族"},
|
||||
Entry{OSI: "12", Name: "侗族"},
|
||||
Entry{OSI: "13", Name: "瑶族"},
|
||||
Entry{OSI: "14", Name: "白族"},
|
||||
Entry{OSI: "15", Name: "土家族"},
|
||||
Entry{OSI: "16", Name: "哈尼族"},
|
||||
Entry{OSI: "17", Name: "哈萨克族"},
|
||||
Entry{OSI: "18", Name: "傣族"},
|
||||
Entry{OSI: "19", Name: "黎族"},
|
||||
Entry{OSI: "20", Name: "傈僳族"},
|
||||
Entry{OSI: "21", Name: "佤族"},
|
||||
Entry{OSI: "22", Name: "畲族"},
|
||||
Entry{OSI: "23", Name: "高山族"},
|
||||
Entry{OSI: "24", Name: "拉祜族"},
|
||||
Entry{OSI: "25", Name: "水族"},
|
||||
Entry{OSI: "26", Name: "东乡族"},
|
||||
Entry{OSI: "27", Name: "纳西族"},
|
||||
Entry{OSI: "28", Name: "景颇族"},
|
||||
Entry{OSI: "29", Name: "柯尔克孜族"},
|
||||
Entry{OSI: "30", Name: "土族"},
|
||||
Entry{OSI: "31", Name: "达斡尔族"},
|
||||
Entry{OSI: "32", Name: "仫佬族"},
|
||||
Entry{OSI: "33", Name: "羌族"},
|
||||
Entry{OSI: "34", Name: "布朗族"},
|
||||
Entry{OSI: "35", Name: "撒拉族"},
|
||||
Entry{OSI: "36", Name: "毛南族"},
|
||||
Entry{OSI: "37", Name: "仡佬族"},
|
||||
Entry{OSI: "38", Name: "锡伯族"},
|
||||
Entry{OSI: "39", Name: "阿昌族"},
|
||||
Entry{OSI: "40", Name: "普米族"},
|
||||
Entry{OSI: "41", Name: "塔吉克族"},
|
||||
Entry{OSI: "42", Name: "怒族"},
|
||||
Entry{OSI: "43", Name: "乌孜别克族"},
|
||||
Entry{OSI: "44", Name: "俄罗斯族"},
|
||||
Entry{OSI: "45", Name: "鄂温克族"},
|
||||
Entry{OSI: "46", Name: "德昂族"},
|
||||
Entry{OSI: "47", Name: "保安族"},
|
||||
Entry{OSI: "48", Name: "裕固族"},
|
||||
Entry{OSI: "49", Name: "京族"},
|
||||
Entry{OSI: "50", Name: "塔塔尔族"},
|
||||
Entry{OSI: "51", Name: "独龙族"},
|
||||
Entry{OSI: "52", Name: "鄂伦春族"},
|
||||
Entry{OSI: "53", Name: "赫哲族"},
|
||||
Entry{OSI: "54", Name: "门巴族"},
|
||||
Entry{OSI: "55", Name: "珞巴族"},
|
||||
Entry{OSI: "56", Name: "基诺族"},
|
||||
Entry{OSI: "99", Name: "其他"},
|
||||
)
|
||||
|
||||
func AllDicts() []Dict {
|
||||
return []Dict{
|
||||
SexDict,
|
||||
BloodTypeDict,
|
||||
RhBloodDict,
|
||||
EducationDict,
|
||||
WorkDict,
|
||||
MaritalStatusDict,
|
||||
InsuranceDict,
|
||||
NationDict,
|
||||
PersonSignDict,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package mapping
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDictTranslatesPHISToOSIAndOSIToPHIS(t *testing.T) {
|
||||
got, err := SexDict.PHISToOSI("1")
|
||||
if err != nil {
|
||||
t.Fatalf("PHISToOSI: %v", err)
|
||||
}
|
||||
if got != "1" {
|
||||
t.Fatalf("PHISToOSI = %q, want 1", got)
|
||||
}
|
||||
|
||||
got, err = SexDict.OSIToPHIS("2")
|
||||
if err != nil {
|
||||
t.Fatalf("OSIToPHIS: %v", err)
|
||||
}
|
||||
if got != "2" {
|
||||
t.Fatalf("OSIToPHIS = %q, want 2", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDictReturnsValidationErrorForUnknownCode(t *testing.T) {
|
||||
_, err := SexDict.PHISToOSI("bad")
|
||||
if err == nil {
|
||||
t.Fatal("PHISToOSI accepted unknown code")
|
||||
}
|
||||
var validationErr ValidationError
|
||||
if !errors.As(err, &validationErr) {
|
||||
t.Fatalf("error type = %T, want ValidationError", err)
|
||||
}
|
||||
if validationErr.Field != "sexCode" || validationErr.Code != "bad" || validationErr.Direction != DirectionPHISToOSI {
|
||||
t.Fatalf("validation error = %#v", validationErr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNationDictContains56EthnicGroupsAndOther(t *testing.T) {
|
||||
if got := NationDict.Len(); got != 57 {
|
||||
t.Fatalf("NationDict.Len = %d, want 57 including 99 other", got)
|
||||
}
|
||||
checks := map[string]string{
|
||||
"01": "汉族",
|
||||
"02": "蒙古族",
|
||||
"10": "朝鲜族",
|
||||
"56": "基诺族",
|
||||
"99": "其他",
|
||||
}
|
||||
for code, wantName := range checks {
|
||||
entry, ok := NationDict.EntryByOSI(code)
|
||||
if !ok {
|
||||
t.Fatalf("NationDict missing code %s", code)
|
||||
}
|
||||
if entry.Name != wantName {
|
||||
t.Fatalf("NationDict[%s].Name = %q, want %q", code, entry.Name, wantName)
|
||||
}
|
||||
got, err := NationDict.PHISToOSI(code)
|
||||
if err != nil {
|
||||
t.Fatalf("Nation PHISToOSI(%s): %v", code, err)
|
||||
}
|
||||
if got != code {
|
||||
t.Fatalf("Nation PHISToOSI(%s) = %q", code, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNationDictContainsAllCodesFrom01To56(t *testing.T) {
|
||||
for i := 1; i <= 56; i++ {
|
||||
code := fmt.Sprintf("%02d", i)
|
||||
if _, ok := NationDict.EntryByOSI(code); !ok {
|
||||
t.Fatalf("NationDict missing code %s", code)
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestCoreDictsContainDocumentedValues(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
dict Dict
|
||||
code string
|
||||
want string
|
||||
}{
|
||||
{name: "blood", dict: BloodTypeDict, code: "4", want: "AB"},
|
||||
{name: "rh", dict: RhBloodDict, code: "2", want: "阴性"},
|
||||
{name: "education", dict: EducationDict, code: "31", want: "大专"},
|
||||
{name: "work", dict: WorkDict, code: "Y", want: "其他"},
|
||||
{name: "marital", dict: MaritalStatusDict, code: "40", want: "离婚"},
|
||||
{name: "insurance", dict: InsuranceDict, code: "99", want: "其他"},
|
||||
{name: "personSign", dict: PersonSignDict, code: "GRQY", want: "已签约"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
entry, ok := tt.dict.EntryByOSI(tt.code)
|
||||
if !ok {
|
||||
t.Fatalf("missing code %s", tt.code)
|
||||
}
|
||||
if entry.Name != tt.want {
|
||||
t.Fatalf("name = %q, want %q", entry.Name, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllDictsRejectDuplicateCodes(t *testing.T) {
|
||||
for _, dict := range AllDicts() {
|
||||
if err := dict.Validate(); err != nil {
|
||||
t.Fatalf("%s Validate: %v", dict.Name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user