pq: malformed array literal: - postgresql

I want to insert json array request like
{
"name": "Sales Commission",
"commission_type":"ZCo6QCHOBth7MD-8sohHQ",
"time_period_from": "2022-05-13 12:00:00",
"time_period_to": "2022-05-18 12:00:00",
"service_id": [
{
"service_id": "1",
"service_type": "1",
"commission_type": "1",
"commission_rate": "1.50"
}
],
"package_id": [
{
"package_id": "1",
"package_type": "1",
"commission_type": "1",
"commission_rate": "1.50"
}
],
"product_id": [
{
"product_id": "1",
"product_type": "1",
"commission_type": "1",
"commission_rate": "1.50"
}
],
"employee_id": [
{
"employee_id": "1",
"commission_type": "1",
"commission_rate": "10.50"
}
],
"status": "active"
}
I am using Golang with Postgres. I had made struct of the Model which is below:
type AutoGenerated struct {
Name string `json:"name"`
CommissionType string `json:"commission_type"`
TimePeriodFrom string `json:"time_period_from"`
TimePeriodTo string `json:"time_period_to"`
Services []Services `json:"service_id"`
Packages []Packages `json:"package_id"`
Products []Products `json:"product_id"`
Employees []Employees `json:"employee_id"`
Status string `json:"status"`
}
type Services struct {
ServiceID string `json:"service_id"`
ServiceType string `json:"service_type"`
CommissionType string `json:"commission_type"`
CommissionRate string `json:"commission_rate"`
}
type Packages struct {
PackageID string `json:"package_id"`
PackageType string `json:"package_type"`
CommissionType string `json:"commission_type"`
CommissionRate string `json:"commission_rate"`
}
type Products struct {
ProductID string `json:"product_id"`
ProductType string `json:"product_type"`
CommissionType string `json:"commission_type"`
CommissionRate string `json:"commission_rate"`
}
type Employees struct {
EmployeeID string `json:"employee_id"`
CommissionType string `json:"commission_type"`
CommissionRate string `json:"commission_rate"`
}
Request which is being decoded
var ReqMapData model.AutoGenerated
err := json.NewDecoder(r.Body).Decode(&ReqMapData)
if err != nil {
log.Println(err)
}
While performing data insertion I am facing this issue like "pq: malformed array literal: "[{"service_id":"1","service_type":"1","commission_type":"1","commission_rate":"1.50"}]"""
Code which inserts data
serJson, errJson := json.Marshal(reqMap.Services)
if errJson != nil {
util.CheckErrorLog(r, errJson)
return false
}
pkgJson, errJson1 := json.Marshal(reqMap.Packages)
if errJson1 != nil {
util.CheckErrorLog(r, errJson1)
return false
}
proJson, errJson2 := json.Marshal(reqMap.Products)
if errJson2 != nil {
util.CheckErrorLog(r, errJson2)
return false
}
empJson, errJson3 := json.Marshal(reqMap.Employees)
if errJson3 != nil {
util.CheckErrorLog(r, errJson3)
return false
}
Qry.WriteString("INSERT INTO commission_template(id, name, commission_type, time_period_from, time_period_to, service_id, package_id, product_id, employee_id, status) ")
Qry.WriteString(" VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);")
err := ExecuteNonQuery(Qry.String(), nanoid, reqMap.Name, reqMap.CommissionType, reqMap.TimePeriodFrom, reqMap.TimePeriodTo, string(serJson), string(pkgJson), string(proJson), string(empJson), reqMap.Status)
if err != nil {
util.CheckErrorLog(r, err)
return false
}
// ExecuteNonQuery - Update, Insert Query
func ExecuteNonQuery(SQLQry string, params ...interface{}) error {
_, err := VDB.Exec(SQLQry, params...)
return err
}

Related

How to handle null value in golang?

User model
type UserExample struct {
Id primitive.ObjectID `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Location string `json:"location,omitempty"`
Title string `json:"title,omitempty"`
}
Update User
func UpdateUserExample() gin.HandlerFunc {
return func(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
userId := c.Param("userId")
var user models.UserExample
defer cancel()
objId, _ := primitive.ObjectIDFromHex(userId)
//Validate the request body
if err := c.BindJSON(&user); err != nil {
c.JSON(http.StatusBadRequest, responses.UserResponseExample{
Status: http.StatusBadRequest,
Message: "Error",
Data: map[string]interface{}{
"data": err.Error()},
})
}
update := bson.M{
"name": user.Name,
"location": user.Location,
"title": user.Title,
}
result, err := userCollectionExample.UpdateOne(ctx, bson.M{
"id": objId,
}, bson.M{
"$set": update,
})
if err != nil {
c.JSON(http.StatusInternalServerError, responses.UserResponseExample{
Status: http.StatusInternalServerError,
Message: "Error",
Data: map[string]interface{}{
"data": err.Error(),
}})
return
}
//Get Update UserExample Detail
var updateUser models.UserExample
if result.MatchedCount == 1 {
err := userCollectionExample.FindOne(ctx, bson.M{
"id": objId,
}).Decode(&updateUser)
if err != nil {
c.JSON(http.StatusInternalServerError, responses.UserResponseExample{
Status: http.StatusInternalServerError,
Message: "Error",
Data: map[string]interface{}{
"data": err.Error(),
}})
return
}
}
c.JSON(http.StatusOK, responses.UserResponseExample{
Status: http.StatusOK,
Message: "Success",
Data: map[string]interface{}{
"data": updateUser,
},
})
}
}
i have try update data via postman, but if value == null will be delete from collection
In this case, i want Update Title of the User, before update all data already exist
Postman
{
"title": "User One"
}
its working to change title in collection. but, for other data (name and location)has gone
"data": {
"id": "63d2ac86aeb9d78d3d5daf21",
"title": "User One",
}
so, how to handle null value from request body?
i just want change title for this case
Usually, such partial updates are handled using a structure that looks like this:
type UserUpdateRequest struct {
Id primitive.ObjectId `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
Location *string `json:"location,omitempty"`
Title *string `json:"title,omitempty"`
}
Note the pointers. This way, the API caller can send non-nil values for the field it wants to update. It can also use an empty string to set the field values to empty.
Then on the database side, you have to create an update statement:
updateFields:=bson.M{}
if request.Name!=nil {
updateFields["name"]=*request.Name
}
if request.Location!=nil {
updateFields["location"]=*request.Location
}
// etc.
update:=bson.M{"$set":updateFields}
Then use the update to update the database record.

Monogo DB Update Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters

So I am using my Express server to try update an array on my MongoDB.
My data looks like this:
{
"ID": "1",
"Name": "John",
"Image URL": "https://www.bnl.gov/today/body_pics/2017/06/stephanhruszkewycz-hr.jpg",
"Email": "",
"userSkillIds": ["1","2"]
},
{
"ID": "2",
"Name": "Sarah",
"Image URL": "https://www.venmond.com/demo/vendroid/img/avatar/big.jpg",
"Email": "",
"userSkillIds": ["3"]
},
I am trying to insert just a single String number in to the userSkillIds array using this code
router.put('/task/:id',function(req,res,next) {
var obj = req.body;
let skillId = obj.skillId;
if(obj == null || obj == {} ) {
res.status(400);
res.json({
"error": "Bad Data"
});
}else {
db.tasks.update(
{ _id: mongojs.ObjectID(req.params.id)},
{ $push: { userSkillIds: { 1: skillId } } }
)
res.json();
}
});
However I continuously get the error Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters.
Any help would be greatly appreciated!
May be your syntax is incorrect, use push operator as follows
db.tasks.update( { _id: 1 }, { $push: { userSkillIds: 89 } } )

Golang GraphQL MongoDB Struggling to get date and id out of the Database

I am new to Golang and Graphql so I probably messed a lot of the configuration up but I am struggling to get values returned from my database using the GraphQL API I created. Whenever I query my database using the GraphQL API I created in Golang It throws the error cannot decode UTC datetime into a string type and struggles to get the id out.
Here is my GrapqhQL schema:
type User {
_id: ID!
username: String!
passwordHash: String!
email: String!
userInfo: userStats
profileStats: profileInfo
}
type userStats {
firstName: String
lastName: String
birthday: String
dateCreated: String!
nativeLanguage: String
currentlyLearning: String
location: Location
}
type Location {
city: String
state: String
zipcode: Int
country: String
}
type profileInfo {
level: Int
xp: Int
xpTillNextLevel: Int
posts: Int
}
input NewUser {
id: ID!
username: String!
passwordHash: String!
email: String!
userStats: String
profileInfo: String
}
type Mutation {
createUser(input: NewUser!): User!
}
type Query {
users: [User!]!
user(id: ID!): User!
}
Here is my code that executes when a query is provided:
func (u *UserRepo) GetUsers() ([]*model.User, error) {
var users []*model.User
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
usersCollection := u.DB.Collection(u.KEYS["collection"].(string))
cursor, err := usersCollection.Find(ctx, bson.M{})
if err != nil {
fmt.Println(err)
return nil, err
}
if err = cursor.All(ctx, &users); err != nil {
fmt.Println(err)
return nil, err
}
fmt.Println(users[0])
return users, nil
}
func (u *UserRepo) GetUserById(id string) (*model.User, error) {
var user model.User
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
usersCollection := u.DB.Collection(u.KEYS["collection"].(string))
userID, err := primitive.ObjectIDFromHex(id)
if err != nil {
fmt.Println("Invalid ObjectID")
}
err = usersCollection.FindOne(ctx, bson.M{"_id": userID}).Decode(&user)
if err != nil {
fmt.Println("error retrieving user userid : " + id)
fmt.Printf("error: %d", err)
//return nil, err
}
fmt.Println(err)
fmt.Println(user)
return &user, nil
}
If I uncomment the return nil,err on the bottom query for selecting one user by the id, it will just return the error of the date and no information so I am leaving it commented out for testing purposes.
my query and result
query:
query getUsers {
user(id: "5ea75c4c67f9266c89dfb659") {
_id
username
email
passwordHash
userInfo{
lastName
dateCreated
location{
state
}
}
profileStats{
level
}
}
}
result:
{
"data": {
"user": {
"_id": "",
"username": "Aerith",
"email": "Aerith#LanguageLearning.com",
"passwordHash": "testingpassword",
"userInfo": {
"lastName": "Gainsborough",
"dateCreated": "",
"location": null
},
"profileStats": null
}
}
}
and here is example dataset that I made for testing in my MongoDB database
db.users.findOne({_id: ObjectId("5ea75c4c67f9266c89dfb659")})
{
"_id" : ObjectId("5ea75c4c67f9266c89dfb659"),
"username" : "Aerith",
"passwordHash" : "testingpassword",
"email" : "Aerith#LanguageLearning.com",
"userInfo" : {
"firstName" : "Aerith",
"lastName" : "Gainsborough",
"birthday" : ISODate("1985-02-07T00:00:00Z"),
"dateCreated" : ISODate("2020-04-27T22:27:24.650Z"),
"nativeLanguage" : "English",
"currentlyLearning" : "Japanese",
"location" : {
"city" : "Sector 5",
"state" : "Midgar",
"zipcode" : 77777,
"country" : "FF7"
}
},
"profileStats" : {
"level" : 1,
"xp" : 0,
"xpTillNextLevel" : 1000,
"comments" : 0,
"posts" : 0
}
}
Also the location and profile stats are just coming back empty and null and I have no clue why.
Sorry for the long amount of code but I am trying to provide the most information possible to assist with finding the answer. Hopefully, this helps and I can get some assurance on how to fix this issue. Thank you for all your help in advance.
edit: after some testing in the userStats type I can get the firstName and lastName but it fails and the cursor crashes because of the data error when it hits birthday. This is why everything is null under birthday. So the issues is how do I decode the mongo date so I can put in the userStates. I am tempted to just pull everything as bson and convert it to correct model structs but that seems like to much extra work and I really do not want to resort to this.
Some BSON types doesn't have direct mapping with Go primitive types, so you need types with custom unmarshalling, either your own made or already done on bson/primitive package
Try defining your user stats struct that way:
import "go.mongodb.org/mongo-driver/mongo/primitive"
type UserStats {
...
BirthDay primitive.DateTime `bson:"birthday"`
//OR BirthDay primitive.Timestamp `bson:"birthday"`
...
}
https://pkg.go.dev/go.mongodb.org/mongo-driver/bson#v1.3.3?tab=doc#hdr-Native_Go_Types
https://pkg.go.dev/go.mongodb.org/mongo-driver/bson/primitive
https://pkg.go.dev/go.mongodb.org/mongo-driver/bson/primitive?tab=doc#DateTime
https://pkg.go.dev/go.mongodb.org/mongo-driver/bson/primitive?tab=doc#Timestamp

How can i sort multidimension array with specific name in swift? [duplicate]

This question already has answers here:
Sorting a Swift array by ordering from another array
(10 answers)
Closed 3 years ago.
In my json result, there is odds array. And inside of it, there will be 8 types of bet. And i want to sort the array by bet type as i want to show. Eg. first "name": "3Way Result", second another one, third "over/under" etc..
Here is my json result from server.
{
"success": true,
"result": [
{
"league_id": 5,
"localTeam": {"data": {}},
"visitorTeam": {"data": {}},
"scores": {},
"time": {"starting_at": {},},
"league": {"data": {"coverage": {}}},
"odds": [
{
"id": 12,
"name": "Over/Under",
"suspended": false,
"bookmaker": {
"data": [
{
"id": 2,
"name": "bet365",
"odds": {
"data": [
{
"label": "Over",
"value": "2.00",
"extra": null,
"probability": "50%",
"dp3": "2.000",
"american": 100,
"handicap": null,
"total": "2.5",
"winning": null,
"stop": false,
"bookmaker_event_id": 84922729,
},
{
"label": "Under",
"value": "1.80",
"probability": "55.56%",
"dp3": "1.800",
}
]
}
}
]
}
},
{
"id": 1,
"name": "3Way Result",
"suspended": false,
"bookmaker": {
"data": [
{
"id": 2,
"name": "bet365",
"odds": {
"data": [
{
"label": "1",
"value": "2.10",
"extra": null,
"probability": "47.62%",
"dp3": "2.100",
"american": 110,
},
{
"label": "X",
"value": "3.30",
"extra": null,
"probability": "30.3%",
"dp3": "3.300",
"american": 229,
},
{
"label": "2",
"value": "3.60",
}
]
}
}
]
}
},
{
"id": 975909,
"name": "Correct Score",
"suspended": false,
"bookmaker": {
"data": [
{
"id": 2,
"name": "bet365",
"odds": {
"data": [
{
"label": "1:0",
"value": "7.50",
"extra": null,
"probability": "13.33%",
"dp3": "7.500",
"american": 650,
"factional": null,
"handicap": null,
"total": null,
"winning": null,
"stop": false,
"bookmaker_event_id": 84922729,
"last_update": {
"date": "2020-02-20 10:59:06.746514",
"timezone_type": 3,
"timezone": "UTC"
}
},
{
"label": "2:0",
"value": "10.00",
},
]
}
}
]
}
},
],
"tipsters": 2
}
]
}
so it is not alphabetically, datetime or anytype i could access. How could i do ?
Update: I have added model.
struct BetterMatchResults: APIModel, Codable {
var success: Bool?
var result: [BetterMatch]?
}
struct BetterMatch: APIModel, Codable {
var id,_id: String?
var localTeam, visitorTeam: BetterTeam?
var spId, league_id, seasonID: Int?
var winningOddsCalculated: Bool?
var time: BetterTime?
var league: BetterLeague?
var createdAt, updatedAt: String?
var odds: [BetterOdd]!
var resultID: String?
var tipsters: Int?
var stats_url: String?
}
struct BetterLeague : APIModel, Codable {
var data: LeagueData?
}
struct LeagueData : APIModel, Codable{
var id: Int?
var active: Bool?
//var legacyID, countryID: Int?
var logo_path: String?
var name: String?
//var isCup: Bool?
}
struct BetterOdd : APIModel, Codable {
var id: Int?
var name: String?
var suspended: Bool?
var bookmaker: BetterBookmaker?
}
// MARK: - Bookmaker
struct BetterBookmaker : APIModel, Codable {
var data: [BetterBookmakerDatum]?
}
// MARK: - BookmakerDatum
struct BetterBookmakerDatum : APIModel, Codable {
var id: Int?
var name: String?
var odds: BetterOdds?
}
// MARK: - Odds
struct BetterOdds : APIModel, Codable {
var data: [BetterOddsDatum]?
}
class BetterOddsDatum: APIModel , Codable {
var label: String?
//var extra: NSNull?
//var probability, dp3: String?
var american: Int?
//var factional, handicap: NSNull?
var total: String?
var winning: Bool?
var stop: Bool?
var bookmakerEventID: Int?
//private var odd: Double
public var value: String?
init() {
}
}
If I understand your question correctly you want to be able sort the data based on betting type but the value of betting types are not sortable if used as a String variable. The solution would be to converting them into enum types with raw values and then sorting the array based on those raw values. Here is an example:
// Create a BetType for your datas
enum BetType: Int {
case overUnder = 0
case threeWayResult = 1 // 3WayResult
...
}
// Update your BetterOdd
struct BetterOdd : APIModel, Codable {
var id: Int?
var name: String?
var betType: BetType = .overUnder // or put your default value here
var suspended: Bool?
var bookmaker: BetterBookmaker?
}
// Loop your BetterMatch property's odds value after fetching datas.
for i in 0..<betterMatch.odds {
if betterMatch.odds[i].name == "over/under" {
betterMatch.odds[i].betType = .overUnder
}
... // Do the same for other types as well in else if blocks
}
Another alternative would be to add a function for getting the type in BetterOdd
struct BetterOdd ... {
... // Your properties
func getBetType() -> BetType {
if name == "over/under" {
return .overUnder
} else if name == "3WayResult" {
return .threeWayResult
}
... // Other cases
}
}
Finnaly for sorting you can do:
let result = betterMatch.odds.sorted({ $0.betType.rawValue > $1.betType.rawValue })
// or if you used the function solution
let result = betterMatch.odds.sorted({ $0.getBetType().rawValue > $1.getBetType().rawValue })
Since you are using a Codable approach you might need to loop the array and set the betType values based on name values.
i changed model
struct BetterOdd : APIModel, Codable {
var id: Int?
var name: String?
var suspended: Bool?
var bookmaker: BetterBookmaker?
//var betType: BetType = .threeWayResult
enum BetType:Int, APIModel, Codable {
case threeWayResult = 7
case overUnder = 6
case doubleChance = 5
case bothTeamsToScore = 4
case threeWayResultFirstHalf = 3
case threeWayResultSecondHalf = 2
case correctScore = 1
case hTfTdouble = 0
}
//
func getBetType() -> BetType {
if name == "3Way Result" {
return .threeWayResult
} else if name == "Over/Under" {
return .overUnder
} else if name == "Double Chance" {
return .doubleChance
} else if name == "Both Teams To Score" {
return .bothTeamsToScore
} else if name == "3Way Result 1st Half" {
return .threeWayResultFirstHalf
} else if name == "3Way Result 2nd Half"{
return .threeWayResultSecondHalf
} else if name == "Correct Score"{
return .correctScore
} else if name == "HF/FT Double" {
return .hTfTdouble
} else {
return .correctScore
}
}
}
and then :
let matchOddsArray = match.odds!
let result = matchOddsArray.sorted(by: { $0.betType.rawValue > $1.betType.rawValue})
let bet = result[indexPath.row]
works perfect.
First of all, mark result in BetterMatchResults and odds in BetterMatch as var, i.e.
struct BetterMatchResults: Codable {
let success: Bool
var result: [BetterMatch] //here....
}
struct BetterMatch: Codable {
var odds: [BetterOdd] //here....
//rest of the parameters...
}
Next, while parsing your JSON data, use map(_:) and sorted(by:) to modify the result and odds in the betterMatchResults, i.e.
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
var betterMatchResults = try JSONDecoder().decode(BetterMatchResults.self, from: data)
betterMatchResults.result = betterMatchResults.result.map {(betterMatch) in
var match = betterMatch
match.odds = match.odds.sorted(by: { $0.name < $1.name })
return match
}
//Test the response...
print(betterMatchResults.result.map({ $0.odds.map({ $0.name }) })) //[["3Way Result", "Correct Score", "Over/Under"]]
} catch {
print(error)
}

Golang find a value from a map of nested json data from MongoDB

I am trying to receive data from my MongoDB using MGO in a map of type []map[string]interface{}
My JSON looks like this -
{
"_id":"string",
"brandId":123,
"category":{
"television":[
{
"cat":"T1",
"subCategory":[
{
"subCat":"T1A TV",
"warrantyPeriod":6
}
],
"warrantyPeriod":12
},
{
"cat":"T2",
"subCategory":[
{
"subCat":"T2A",
"warrantyPeriod":18
},
{
"subCat":"T2B",
"warrantyPeriod":9
}
],
"warrantyPeriod":15
},
{
"cat":"T3",
"subCategory":[
{
"subCat":"T3A",
"warrantyPeriod":3
},
{
"subCat":"T3B",
"warrantyPeriod":5
},
{
"subCat":"T3C",
"warrantyPeriod":7
},
{
"subCat":"T3D",
"warrantyPeriod":11
}
],
"warrantyPeriod":4
}
],
"television_warrantyPeriod":24
},
"title":"BrandName"
}
I would ideally pass in the category name i.e. 'television' and cat and subCat values which could be optional.
For e.g. something like this -
{
"categorySlug": "television",
"brandId": "123",
"model": "T2"
}
In which case I would expect to find '15' which is the warrantyPeriod value for T2 if there are no T2A or T2B specified.
My query functions look like this -
var data map[string]string
err := json.NewDecoder(r.Body).Decode(&data)
log.Println(err)
var buffer bytes.Buffer
buffer.WriteString("category.")
buffer.WriteString(data["categorySlug"])
brandId, _ := strconv.Atoi(data["brandId"])
concernedbrandandcategory := database.GetMappedFields("Brands", bson.M{"brandId": brandId, buffer.String(): bson.M{"$exists": true}}, bson.M{buffer.String(): 1})
categorymap := concernedbrandandcategory[0]
log.Println(categorymap["category"]["television"], reflect.TypeOf(categorymap))
My GetMappedFields function looks like this -
func GetMappedFields(collectionName string, query interface{}, selector interface{}) (result []map[string]interface{}) {
MgoSession.DB(Dbname).C(collectionName).Find(query).Select(selector).All(&result)
return
}
I'm just not able to wrap my head around this nested structure which sometimes returns a map and sometimes an interface!
Any help would be highly appreciated!
you can do something like this
majorCat := body["categorySlug"]
category := body["category"]
subCategory := body["subCategory"]
brandId, err := strconv.Atoi(body["brandId"])
if err != nil {
log.Println(err)
}
result := database.GetMappedFields("Brands", bson.M{"brandId": brandId}, bson.M{"category": 1, "_id": 0})
internalObj := result[0]["category"].(map[string]interface{})
finalValue := internalObj["television_warrantyPeriod"]
if category != "" {
for _, v := range internalObj[majorCat].([]interface{}) {
subObj := v.(map[string]interface{})
if subObj["cat"] == category {
finalValue = subObj["warrantyPeriod"]
if subCategory != "" {
minorObj := subObj["subCategory"].([]interface{})
for _, iter := range minorObj {
kevVal := iter.(map[string]interface{})
if kevVal["subCat"] == subCategory {
finalValue = kevVal["warrantyPeriod"]
}
}
}
}
}
}
Hopefully this will do dynamically or you can create a struct so that it can directly be decoded into that cheers