PATCH http request validation of single field - validation

I have a struct snippet which is something like this
type Talent struct {
Code string `json:"code" gorm:"type:varchar(10);not null"`
FirstName string `json:"firstName" example:"Ravi" gorm:"type:varchar(50)"`
LastName string `json:"lastName" example:"Sharma" gorm:"type:varchar(50)"`
Email string `json:"email" example:"john#doe.com" gorm:"type:varchar(100)"`
Contact string `json:"contact" example:"1234567890" gorm:"type:varchar(15)"`
AcademicYear *uint8 `json:"academicYear" example:"1" gorm:"type:tinyint(2)"`
Type *uint8 `json:"talentType" example:"4" gorm:"type:tinyint(2)"`
Resume *string `json:"resume" gorm:"type:varchar(200)"`
ExperienceInMonths *uint `json:"experienceInMonths"`
Image *string `json:"image" gorm:"type:varchar(200)"`
Gender *string `json:"gender" gorm:"type:varchar(50)"`
DateOfBirth *string `json:"dateOfBirth" gorm:"type:date"`
}
I want to update one field at a time, so I am sending only one field in json to the API as a PATCH http request and in backend(Golang) I am converting the json to struct where in only that field in json will have value in the struct... all other fields will be nil.
For example I am sending firstName, now when I convert it to struct only the firstName field is filled. Now I want to validate that field.. like firstName is a compulsary field so it will have two validations.. one for checking if its empty and one for the max number of characters.
Like wise all fields have some validations. Now how do I validate only that particular field. Having switch cases is what I had considered but is there any other optimal way to do this in golang??

Related

Microsoft calendar event response to Go struct

been trying to unmarshal a response from Microsoft's Graph API into a Go struct but I keep getting an error "json: cannot unmarshal object into Go struct field .value.start of type []struct { DateTime string "json:"dateTime""; TimeZone string "json:"timeZone"" }".
Below is my struct:
type MicrosoftCalendarEventsResponse struct {
Value []struct {
Etag string `json:"#odata.etag"`
Id string `json:"id"`
Subject string `json:"subject"`
Start []struct {
DateTime string `json:"dateTime"`
TimeZone string `json:"timeZone"`
} `json:"start"`
End []struct {
DateTime string `json:"dateTime"`
TimeZone string `json:"timeZone"`
} `json:"end"`
OriginalStartTimeZone string `json:"originalStartTimeZone"`
OriginalEndTimeZone string `json:"originalEndTimeZone"`
ICalUId string `json:"iCalUId"`
ReminderMinutesBeforeStart int `json:"reminderMinutesBeforeStart"`
IsReminderOn bool `json:"isReminderOn"`
} `json:"value"`
}
The response I received is this:
{"#odata.etag":"W/\"8COqS12xxxhwcMA==\"","id":"xxxxx","createdDateTime":"2019-12-05T17:09:41.018502Z","lastModifiedDateTime":"2019-12-05T17:09:41.8919929Z","changeKey":"xxxx","categories":[],"originalStartTimeZone":"W. Europe Standard Time","originalEndTimeZone":"W. Europe Standard Time","iCalUId":"xxx","reminderMinutesBeforeStart":15,"isReminderOn":true,"hasAttachments":false,"subject":"Something","bodyPreview":"","importance":"normal","sensitivity":"normal","isAllDay":false,"isCancelled":false,"isOrganizer":true,"responseRequested":true,"seriesMasterId":null,"showAs":"busy","type":"singleInstance","webLink":"xxx","onlineMeetingUrl":null,"isOnlineMeeting":false,"onlineMeetingProvider":"unknown","allowNewTimeProposals":true,"recurrence":null,"onlineMeeting":null,"responseStatus":{"response":"organizer","time":"0001-01-01T00:00:00Z"},"body":{"contentType":"html","content":""},"start":{"dateTime":"2019-12-17T17:00:00.0000000","timeZone":"UTC"},"end":{"dateTime":"2019-12-17T17:30:00.0000000","timeZone":"UTC"},"location":{"displayName":"","locationType":"default","uniqueIdType":"unknown","address":{},"coordinates":{}},"locations":[],"attendees":[],"organizer":{"emailAddress":{"name":"John Doe","address":"someone#somewhere.com"}}}
In which you can clearly see the part that is giving the error:
"start":{"dateTime":"2019-12-17T17:00:00.0000000","timeZone":"UTC"}
can anyone please tell me what I am doing wrong? been trying for hours without any progress and I really have no clue whats wrong.
The other stuff like Etag, Id, Subject and so on are working properly. Its only the nested []structs that do not work.
To capture calender events (plural) it makes sense that the top-level Values is a slice of structs.
However, within a particular Value, your Start and End fields are defined also as slices of structs which is probably not what you want.
Try just a plain struct:
Start struct {
DateTime string `json:"dateTime"`
TimeZone string `json:"timeZone"`
} `json:"start"`
End struct {
DateTime string `json:"dateTime"`
TimeZone string `json:"timeZone"`
} `json:"end"`

Golang How to template a nested struct?

I'm trying to template a JSON response onto a front end, my current struct is this:
type Response struct {
WhoisRecord struct {
CreatedDate time.Time `json:"createdDate"`
UpdatedDate time.Time `json:"updatedDate"`
ExpiresDate time.Time `json:"expiresDate"`
Registrant struct {
Name string `json:"name"`
Organization string `json:"organization"`
Street1 string `json:"street1"`
City string `json:"city"`
State string `json:"state"`
Country string `json:"country"`
CountryCode string `json:"countryCode"`
Email string `json:"email"`
Telephone string `json:"telephone"`
Fax string `json:"fax"`
} `json:"registrant"`
AdministrativeContact struct {
Name string `json:"name"`
Organization string `json:"organization"`
Street1 string `json:"street1"`
City string `json:"city"`
State string `json:"state"`
Country string `json:"country"`
CountryCode string `json:"countryCode"`
Email string `json:"email"`
Telephone string `json:"telephone"`
Fax string `json:"fax"`
} `json:"administrativeContact"`
TechnicalContact struct {
Name string `json:"name"`
Organization string `json:"organization"`
Street1 string `json:"street1"`
City string `json:"city"`
State string `json:"state"`
Country string `json:"country"`
CountryCode string `json:"countryCode"`
Email string `json:"email"`
Telephone string `json:"telephone"`
Fax string `json:"fax"`
} `json:"technicalContact"`
DomainName string `json:"domainName"`
NameServers struct {
HostNames []string `json:"hostNames"`
} `json:"nameServers"`
RegistrarName string `json:"registrarName"`
Ips []string `json:"ips"`
} `json:"WhoisRecord"`
}
I then unmarshal this json response, and pass it onto the front end (I'm using GIN)
(Response is redeclared as res)
c.HTML(200,"homework.html", gin.H{
"whois": res,
})
But this is where i run into problems, the code works, but i'm not sure how to template it since it's nested.
For example, i want to show Registrant, Administrative, and Technical contact details (all fields returned) into separate tables. Along with displaying the created, updated, and expired dates. Then Finalizing by showing the The registrar, the ips, and the nameservers (in this case the hostnames field under the NameServers )
How would i go about serving this in my homework.html file? I've tried everything. Usually i would just do something like:
To Display IPs:
{{ range .Ips }}
<div>
<p>IP</p>
<h6>{{ . }}</h6>
</div>
{{end}}
To Display Register Data:
<div>
<p>Name</p>
<h6>{{ .Name }}</h6>
<p>Email</p>
<h6>{{ .Email }}</h6>
//etc
</div>
To display the Registrar:
<div>
<p>Registrar</p>
<h6>{{ .RegistrarName }}</h6>
</div>
But none of this is working (How do i display registrant name in one field, then display technical name in another? i'm obviously messing up the templates big time and my understanding of it is a bit skewed). I've read everything i could, i tried to divide the structs and serve as separate structs, etc. Nothing is working. Can someone point me in the right direction and give examples?
Thanks!
Templates fields are evaluated relative to the current context {{.}}. The following evaluates the template using a map containing "whois" as key, and res as the value:
in.H{ "whois": res})
The value of {{.whois}} is your struct, and you can access the struc field from there. So you can do:
{{.whois.Registrant.Name}}

How to set unique at struct Beego

How to set unique at the struct specific columns. first name
type User struct {
ID int64 `orm:"size(100)", pk`
Lastname string `orm:"size(100)"`
Firstname string `orm:"size(100)"`
Role string `orm:"size(100)"`
Created time.Time `orm:"size(100)"`
Updated time.Time `orm:"size(100)"`
}
I'm using "github.com/astaxie/beego/orm"
According to the documentation, you just add the word "unique" to the tag:
Add unique key for one field
Name string `orm:"unique"`
To combine tags, you must use a semicolon as documented here. For example:
Firstname string orm:"unique;size(100)"

Golang Decode a BSON with special character Keys to a struct

I have a Golang struct called Person where all the properties have to be exported:
type Person struct {
Id string
Name string
}
Now I need to encode my MongoDB BSON response to this Person struct. The BSON looks like:
{
"_id": "ajshJSH78N",
"Name": "Athavan Kanapuli"
}
The Golang code to encode the BSON is:
mongoRecord := Person{}
c := response.session.DB("mydb").C("users")
err := c.Find(bson.M{"username": Credentials.Username, "password": Credentials.Password}).One(&mongoRecord)
The Problem:
_id is not getting encoded into Id
If I change the Person property into _Id, then it won't be exported.
How can I solve this problem?
Define your struct with json tag-
type Person struct {
Id string `json:"_id"`
Name string // this field match with json, so mapping not need
}
I tried to put a json tag like ,
type Person struct {
Id string `json:"_id"`
Name string // this field match with json, so mapping not need
}
But still it didn't work. Because the Mongodb returns '_id' which is of type bson.ObjectId . Hence changing the Struct tag to bson:"_id" and the type of the Person struct has been changed from string to bson.ObjectId. The changes done are as follows ,
type Person struct {
Id bson.ObjectId `bson:"_id"`
Name string
UserName string
IsAdmin bool
IsApprover bool
}
And It works!

structs with multiple mapping notations

I have these two structs which represent the same entities (one comes from a Json file and the other from a DB)
type DriverJson struct {
ID int `json:"id"`
Name string `json:"name"`
}
type DriverOrm struct {
ID int `orm:"column(id);auto"`
Name string `orm:"column(name);size(255);null"`
}
I want to merge them into one Driver struct, how do I merge the mapping notations (orm:, json:)?
Thank you
As mentioned in the documentation of reflect.StructTag, by convention the value of a tag string is a space-separated key:"value" pairs, so simply:
type DriverJson struct {
ID int `json:"id" orm:"column(id);auto"`
Name string `json:"name" orm:"column(name);size(255);null`
}
For details, see What are the use(s) for tags in Go?

Resources