Skip to content

Commit

Permalink
Merge pull request #48 from kintone-labs/SSR-1903
Browse files Browse the repository at this point in the history
SSR-1903: add index to form field API (SSR-2363)
  • Loading branch information
josh-vo authored Jun 20, 2022
2 parents 0072fa3 + 452e9ec commit 14114b2
Showing 1 changed file with 74 additions and 3 deletions.
77 changes: 74 additions & 3 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,7 @@ type FieldInfo struct {
Medium string `json:"protocol"` // "WEB", "CALL", or "MAIL"
Format string `json:"format"` // "NUMBER", "NUMBER_DIGIT", "DATETIME", "DATE", "TIME", "HOUR_MINUTE", "DAY_HOUR_MINUTE"
Fields []FieldInfo `json:"fields"` // Field list of this subtable
Index int `json:"index"`
}

// Work around code to handle "true"/"false" strings as booleans...
Expand All @@ -1008,6 +1009,8 @@ func (fi *FieldInfo) UnmarshalJSON(data []byte) error {
Medium string `json:"protocol"`
Format string `json:"format"`
Fields []FieldInfo `json:"fields"`
Index int `json:"index"`

}
err := json.Unmarshal(data, &t)
if err != nil {
Expand All @@ -1021,7 +1024,7 @@ func (fi *FieldInfo) UnmarshalJSON(data []byte) error {
t.MaxValue, t.MinValue, t.MaxLength, t.MinLength,
t.Default, t.DefaultTime, t.Options, t.Expression,
(t.Separator == "true"),
t.Medium, t.Format, t.Fields,
t.Medium, t.Format, t.Fields,t.Index,
}
return nil
}
Expand Down Expand Up @@ -1105,13 +1108,18 @@ func (app *App) Fields() (map[string]*FieldInfo, error) {
data, _ := json.Marshal(request_body{app.AppId})
req, err := app.newRequest("GET", "app/form/fields", bytes.NewReader(data))
if err != nil {
return nil, err
return nil, err
}
resp, err := app.do(req)
resp, err := app.do(req)
if err != nil {
return nil, err
}
body, err := parseResponse(resp)
fieldCodes := getFieldCodeFromFormField(body)
if err != nil {
fmt.Println("error:", err)
}

if err != nil {
return nil, err
}
Expand All @@ -1124,9 +1132,72 @@ func (app *App) Fields() (map[string]*FieldInfo, error) {

ret := make(map[string]*FieldInfo)
decodeFieldInfo(t, ret)
updateIndexToField(fieldCodes, ret)
return ret, nil
}

func getFieldCodeFromFormField(formField []byte) []string {
result := []string{}
var tempByteString= "";
var fieldCode = "";
fieldCodeIgnore := []string{"GROUP"}
for _,byteField:= range formField {
hasField := hasFieldCodeIgnore(byteField , tempByteString, fieldCodeIgnore)
if strings.Contains(tempByteString, `"code":`) {
if string(byteField) == "," {
if hasField {
tempByteString = "";
fieldCode = "";
hasField= false;
continue;
}
tempByteString = "";
result = append(result, fieldCode);
fieldCode = "";
} else if string(byteField) != `"` {
fieldCode += string(byteField);
}
continue;
}
tempByteString += string(byteField);
}
return result;
}

func hasFieldCodeIgnore(byteField byte,tempFormFieldString string,fieldCodeIgnore []string) bool {
hasField := false
if strings.Contains(tempFormFieldString, `"type":`) {
if string(byteField) == "," {
for _, type_1 := range fieldCodeIgnore {
if strings.Contains(tempFormFieldString, type_1) {
hasField= true
}
}
}
}
return hasField
}

func updateIndexToField(fieldCode []string, fieldInfo map[string]*FieldInfo) {
i:=0
for _,initFieldCode:= range fieldCode {
for _,sortedFieldCode:= range fieldInfo {
if initFieldCode == sortedFieldCode.Code {
sortedFieldCode.Index = i;
i+=1
}

for k,subField := range sortedFieldCode.Fields {
if initFieldCode == subField.Code {
sortedFieldCode.Fields[k].Index = i;
i+=1
}

}
}
}
}

// CreateCursor return the meta data of the Cursor in this application
func (app *App) CreateCursor(fields []string, query string, size uint64) (*Cursor, error) {
type cursor struct {
Expand Down

0 comments on commit 14114b2

Please sign in to comment.