0x4a52466c696e74 hace 2 semanas
padre
commit
afe97c4e64
Se han modificado 2 ficheros con 45 adiciones y 7 borrados
  1. 27 7
      rest_gorm/request_list.go
  2. 18 0
      z_test.go

+ 27 - 7
rest_gorm/request_list.go

@@ -4,6 +4,7 @@ import (
 	"fmt"
 	"reflect"
 
+	"git.ali33.ru/fcg-xvii/go-tools/json"
 	"git.ali33.ru/fcg-xvii/rest"
 	"gorm.io/gorm"
 	"gorm.io/gorm/clause"
@@ -100,13 +101,6 @@ func toAnySlice(slice any) []any {
 	return result
 }
 
-type ResultList struct {
-	Items  []any `json:"Items" rest:"fixed"`
-	Offset int   `json:"Offset" rest:"fixed"`
-	Limit  int   `json:"Limit" rest:"fixed"`
-	Count  int64 `json:"Count" rest:"fixed"`
-}
-
 func (s *List) ResultAnswer(pg *gorm.DB, fields rest.FieldNamesList, res any, offset, limit int) (*ResultList, rest.IErrorArgs) {
 	count, err := s.Result(pg, fields, res)
 	if err != nil {
@@ -120,3 +114,29 @@ func (s *List) ResultAnswer(pg *gorm.DB, fields rest.FieldNamesList, res any, of
 	}
 	return rList, nil
 }
+
+type ResultList struct {
+	Items  []any `json:"Items" rest:"fixed"`
+	Offset int   `json:"Offset" rest:"fixed"`
+	Limit  int   `json:"Limit" rest:"fixed"`
+	Count  int64 `json:"Count" rest:"fixed"`
+}
+
+func (s *ResultList) Map(req rest.IRequestIn) (json.Map, rest.IErrorArgs) {
+	fields := req.Fields()
+	ml := make([]json.Map, 0, len(s.Items))
+	for _, item := range s.Items {
+		it, ierr := rest.Fields(item, nil, fields)
+		if ierr != nil {
+			return nil, ierr
+		}
+		ml = append(ml, it)
+	}
+	res := json.Map{
+		"Items":  ml,
+		"Offset": s.Offset,
+		"Limit":  s.Limit,
+		"Count":  s.Count,
+	}
+	return res, nil
+}

+ 18 - 0
z_test.go

@@ -467,3 +467,21 @@ func TestFielderMap(t *testing.T) {
 	}
 	rm.LogPretty()
 }
+
+type RFielder struct {
+	ID    int `rest:"default"`
+	Name  string
+	Count int
+}
+
+func TestFielderObject(t *testing.T) {
+	sl := []*RFielder{
+		{ID: 1, Name: "one"},
+		{ID: 2, Name: "two"},
+	}
+	rm, err := Fields(sl, nil, nil)
+	if err != nil {
+		t.Fatal(err)
+	}
+	rm.LogPretty()
+}