Best K6 code snippet using types.UnmarshalJSON
activity.go
Source:activity.go
...49 Data json.RawMessage `json:"data"`50 } `json:"actor"`51 } `json:"relationships"`52}53// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.54func (a *Activity) UnmarshalJSON(b []byte) error {55 var helper activityUnmarshalHelper56 helper.Attributes = &helper.activity57 if err := json.Unmarshal(b, &helper); err != nil {58 return err59 }60 *a = Activity(helper.activity)61 a.Attachments = helper.Relationships.Attachments.Data62 a.RawActor = helper.Relationships.RawActor.Data63 a.rawData = b64 return nil65}66// Actor returns returns the parsed actor. For recognized actor types, a value of the corresponding struct type will be returned.67func (a *Activity) Actor() (actor interface{}) {68 var obj unknownResource69 if err := json.Unmarshal(a.RawActor, &obj); err != nil {70 panic(err.Error())71 }72 switch *obj.Type {73 case UserType:74 actor = &User{}75 case ProgramType:76 actor = &Program{}77 }78 if err := json.Unmarshal(a.RawActor, &actor); err != nil {79 panic(err.Error())80 }81 return actor82}83// Activity returns the parsed activity. For recognized activity types, a value of the corresponding struct type will be returned.84func (a *Activity) Activity() (activity interface{}) {85 switch *a.Type {86 case ActivityBountyAwardedType:87 activity = &ActivityBountyAwarded{}88 case ActivityBountySuggestedType:89 activity = &ActivityBountySuggested{}90 case ActivityBugClonedType:91 activity = &ActivityBugCloned{}92 case ActivityExternalUserInvitationCancelledType:93 activity = &ActivityExternalUserInvitationCancelled{}94 case ActivityExternalUserInvitedType:95 activity = &ActivityExternalUserInvited{}96 case ActivityExternalUserJoinedType:97 activity = &ActivityExternalUserJoined{}98 case ActivityExternalUserRemovedType:99 activity = &ActivityExternalUserRemoved{}100 case ActivityGroupAssignedToBugType:101 activity = &ActivityGroupAssignedToBug{}102 case ActivityReferenceIDAddedType:103 activity = &ActivityReferenceIDAdded{}104 case ActivityReportTitleUpdatedType:105 activity = &ActivityReportTitleUpdated{}106 case ActivityReportVulnerabilityTypesUpdatedType:107 activity = &ActivityReportVulnerabilityTypesUpdated{}108 case ActivitySwagAwardedType:109 activity = &ActivitySwagAwarded{}110 case ActivityUserAssignedToBugType:111 activity = &ActivityUserAssignedToBug{}112 case ActivityUserBannedFromProgramType:113 activity = &ActivityUserBannedFromProgram{}114 default:115 return nil116 }117 if err := json.Unmarshal(a.rawData, activity); err != nil {118 panic(err.Error())119 }120 return activity121}122// Report returns the report this activity is a child of123func (a *Activity) Report() *Report {124 return a.report125}126// ActivityBountyAwarded occurs when a bounty is awarded.127//128// HackerOne API docs:https://api.hackerone.com/docs/v1#activity-bounty-awarded129type ActivityBountyAwarded struct {130 BountyAmount *string `json:"bounty_amount"`131 BonusAmount *string `json:"bonus_amount"`132}133// Helper types for JSONUnmarshal134type activityBountyAwarded ActivityBountyAwarded // Used to avoid recursion of JSONUnmarshal135type activityBountyAwardedUnmarshalHelper struct {136 Attributes activityBountyAwarded `json:"attributes"`137}138// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.139func (a *ActivityBountyAwarded) UnmarshalJSON(b []byte) error {140 var helper activityBountyAwardedUnmarshalHelper141 if err := json.Unmarshal(b, &helper); err != nil {142 return err143 }144 *a = ActivityBountyAwarded(helper.Attributes)145 return nil146}147// ActivityBountySuggested occurs when a bounty is suggested.148//149// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-bounty-suggested150type ActivityBountySuggested struct {151 BountyAmount *string `json:"bounty_amount"`152 BonusAmount *string `json:"bonus_amount"`153}154// Helper types for JSONUnmarshal155type activityBountySuggested ActivityBountySuggested // Used to avoid recursion of JSONUnmarshal156type activityBountySuggestedUnmarshalHelper struct {157 Attributes activityBountySuggested `json:"attributes"`158}159// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.160func (a *ActivityBountySuggested) UnmarshalJSON(b []byte) error {161 var helper activityBountySuggestedUnmarshalHelper162 if err := json.Unmarshal(b, &helper); err != nil {163 return err164 }165 *a = ActivityBountySuggested(helper.Attributes)166 return nil167}168// ActivityBugCloned occurs when a bug is cloned.169//170// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-bug-cloned171type ActivityBugCloned struct {172 OriginalReportID *int `json:"original_report_id"`173}174// Helper types for JSONUnmarshal175type activityBugCloned ActivityBugCloned // Used to avoid recursion of JSONUnmarshal176type activityBugClonedUnmarshalHelper struct {177 Attributes activityBugCloned `json:"attributes"`178}179// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.180func (a *ActivityBugCloned) UnmarshalJSON(b []byte) error {181 var helper activityBugClonedUnmarshalHelper182 if err := json.Unmarshal(b, &helper); err != nil {183 return err184 }185 *a = ActivityBugCloned(helper.Attributes)186 return nil187}188// ActivityExternalUserInvitationCancelled occurs when a external user's invitiation is cancelled.189//190// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-external-user-invitation-cancelled191type ActivityExternalUserInvitationCancelled struct {192 Email *string `json:"email"`193}194// Helper types for JSONUnmarshal195type activityExternalUserInvitationCancelled ActivityExternalUserInvitationCancelled // Used to avoid recursion of JSONUnmarshal196type activityExternalUserInvitationCancelledSuggestedUnmarshalHelper struct {197 Attributes activityExternalUserInvitationCancelled `json:"attributes"`198}199// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.200func (a *ActivityExternalUserInvitationCancelled) UnmarshalJSON(b []byte) error {201 var helper activityExternalUserInvitationCancelledSuggestedUnmarshalHelper202 if err := json.Unmarshal(b, &helper); err != nil {203 return err204 }205 *a = ActivityExternalUserInvitationCancelled(helper.Attributes)206 return nil207}208// ActivityExternalUserInvited occurs when a external user is invited.209//210// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-external-user-invited211type ActivityExternalUserInvited struct {212 Email *string `json:"email"`213}214// Helper types for JSONUnmarshal215type activityExternalUserInvited ActivityExternalUserInvited // Used to avoid recursion of JSONUnmarshal216type activityExternalUserInvitedUnmarshalHelper struct {217 Attributes activityExternalUserInvited `json:"attributes"`218}219// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.220func (a *ActivityExternalUserInvited) UnmarshalJSON(b []byte) error {221 var helper activityExternalUserInvitedUnmarshalHelper222 if err := json.Unmarshal(b, &helper); err != nil {223 return err224 }225 *a = ActivityExternalUserInvited(helper.Attributes)226 return nil227}228// ActivityExternalUserJoined occurs when a external user joins.229//230// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-external-user-joined231type ActivityExternalUserJoined struct {232 DuplicateReportID *int `json:"duplicate_report_id"`233}234// Helper types for JSONUnmarshal235type activityExternalUserJoined ActivityExternalUserJoined // Used to avoid recursion of JSONUnmarshal236type activityExternalUserJoinedUnmarshalHelper struct {237 Attributes activityExternalUserJoined `json:"attributes"`238}239// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.240func (a *ActivityExternalUserJoined) UnmarshalJSON(b []byte) error {241 var helper activityExternalUserJoinedUnmarshalHelper242 if err := json.Unmarshal(b, &helper); err != nil {243 return err244 }245 *a = ActivityExternalUserJoined(helper.Attributes)246 return nil247}248// ActivityExternalUserRemoved occurs when a external user is removed249//250// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-external-user-removed251type ActivityExternalUserRemoved struct {252 RemovedUser *User `json:"removed_user"`253}254// Helper types for JSONUnmarshal255type activityExternalUserRemoved ActivityExternalUserRemoved // Used to avoid recursion of JSONUnmarshal256type activityExternalUserRemovedUnmarshalHelper struct {257 Relationships struct {258 RemovedUser struct {259 Data *User `json:"data"`260 } `json:"removed_user"`261 } `json:"relationships"`262}263// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.264func (a *ActivityExternalUserRemoved) UnmarshalJSON(b []byte) error {265 var helper activityExternalUserRemovedUnmarshalHelper266 if err := json.Unmarshal(b, &helper); err != nil {267 return err268 }269 a.RemovedUser = helper.Relationships.RemovedUser.Data270 return nil271}272// ActivityGroupAssignedToBug occurs when a group is assigned to a report.273//274// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-group-assigned-to-bug275type ActivityGroupAssignedToBug struct {276 Group *Group `json:"group"`277}278// Helper types for JSONUnmarshal279type activityGroupAssignedToBug ActivityGroupAssignedToBug // Used to avoid recursion of JSONUnmarshal280type activityGroupAssignedToBugUnmarshalHelper struct {281 Relationships struct {282 Group struct {283 Data *Group `json:"data"`284 } `json:"group"`285 } `json:"relationships"`286}287// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.288func (a *ActivityGroupAssignedToBug) UnmarshalJSON(b []byte) error {289 var helper activityGroupAssignedToBugUnmarshalHelper290 if err := json.Unmarshal(b, &helper); err != nil {291 return err292 }293 a.Group = helper.Relationships.Group.Data294 return nil295}296// ActivityReferenceIDAdded occurs when a reference id/url is added to a report.297//298// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-reference-id-added299type ActivityReferenceIDAdded struct {300 Reference *string `json:"reference"`301 ReferenceURL *string `json:"reference_url"`302}303// Helper types for JSONUnmarshal304type activityReferenceIDAdded ActivityReferenceIDAdded // Used to avoid recursion of JSONUnmarshal305type activityReferenceIDAddedUnmarshalHelper struct {306 Attributes activityReferenceIDAdded `json:"attributes"`307}308// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.309func (a *ActivityReferenceIDAdded) UnmarshalJSON(b []byte) error {310 var helper activityReferenceIDAddedUnmarshalHelper311 if err := json.Unmarshal(b, &helper); err != nil {312 return err313 }314 *a = ActivityReferenceIDAdded(helper.Attributes)315 return nil316}317// ActivityReportTitleUpdated occurs when report title is updated318//319// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-report-title-updated320type ActivityReportTitleUpdated struct {321 OldTitle *string `json:"old_title"`322 NewTitle *string `json:"new_title"`323}324// Helper types for JSONUnmarshal325type activityReportTitleUpdated ActivityReportTitleUpdated // Used to avoid recursion of JSONUnmarshal326type activityReportTitleUpdatedUnmarshalHelper struct {327 Attributes activityReportTitleUpdated `json:"attributes"`328}329// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.330func (a *ActivityReportTitleUpdated) UnmarshalJSON(b []byte) error {331 var helper activityReportTitleUpdatedUnmarshalHelper332 if err := json.Unmarshal(b, &helper); err != nil {333 return err334 }335 *a = ActivityReportTitleUpdated(helper.Attributes)336 return nil337}338// ActivityReportVulnerabilityTypesUpdated occurs when vulnerability types for a report are updated.339//340// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-report-vulnerability-types-updated341type ActivityReportVulnerabilityTypesUpdated struct {342 OldVulnerabilityTypes []VulnerabilityType `json:"old_vulnerability_types"`343 NewVulnerabilityTypes []VulnerabilityType `json:"new_vulnerability_types"`344}345// Helper types for JSONUnmarshal346type activityReportVulnerabilityTypesUpdated ActivityReportVulnerabilityTypesUpdated // Used to avoid recursion of JSONUnmarshal347type activityReportVulnerabilityTypesUpdatedUnmarshalHelper struct {348 Relationships struct {349 OldVulnerabilityTypes struct {350 Data []VulnerabilityType `json:"data"`351 } `json:"old_vulnerability_types"`352 NewVulnerabilityTypes struct {353 Data []VulnerabilityType `json:"data"`354 } `json:"new_vulnerability_types"`355 } `json:"relationships"`356}357// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.358func (a *ActivityReportVulnerabilityTypesUpdated) UnmarshalJSON(b []byte) error {359 var helper activityReportVulnerabilityTypesUpdatedUnmarshalHelper360 if err := json.Unmarshal(b, &helper); err != nil {361 return err362 }363 a.OldVulnerabilityTypes = helper.Relationships.OldVulnerabilityTypes.Data364 a.NewVulnerabilityTypes = helper.Relationships.NewVulnerabilityTypes.Data365 return nil366}367// ActivitySwagAwarded occurs when swag is awarded368//369// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-swag-awarded370type ActivitySwagAwarded struct {371 Swag *Swag `json:"swag"`372}373// Helper types for JSONUnmarshal374type activitySwagAwarded ActivitySwagAwarded // Used to avoid recursion of JSONUnmarshal375type activitySwagAwardedUnmarshalHelper struct {376 Relationships struct {377 Swag struct {378 Data *Swag `json:"data"`379 } `json:"swag"`380 } `json:"relationships"`381}382// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.383func (a *ActivitySwagAwarded) UnmarshalJSON(b []byte) error {384 var helper activitySwagAwardedUnmarshalHelper385 if err := json.Unmarshal(b, &helper); err != nil {386 return err387 }388 a.Swag = helper.Relationships.Swag.Data389 return nil390}391// ActivityUserAssignedToBug occurs when a user is assigned to a report.392//393// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-user-assigned-to-bug394type ActivityUserAssignedToBug struct {395 AssignedUser *User `json:"assigned_user"`396}397// Helper types for JSONUnmarshal398type activityUserAssignedToBug ActivityUserAssignedToBug // Used to avoid recursion of JSONUnmarshal399type activityUserAssignedToBugUnmarshalHelper struct {400 Relationships struct {401 AssignedUser struct {402 Data *User `json:"data"`403 } `json:"assigned_user"`404 } `json:"relationships"`405}406// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.407func (a *ActivityUserAssignedToBug) UnmarshalJSON(b []byte) error {408 var helper activityUserAssignedToBugUnmarshalHelper409 if err := json.Unmarshal(b, &helper); err != nil {410 return err411 }412 a.AssignedUser = helper.Relationships.AssignedUser.Data413 return nil414}415// ActivityUserBannedFromProgram occurs when a user is banned from a program.416//417// HackerOne API docs: https://api.hackerone.com/docs/v1#activity-user-banned-from-program418type ActivityUserBannedFromProgram struct {419 RemovedUser *User `json:"removed_user"`420}421// Helper types for JSONUnmarshal422type activityUserBannedFromProgram ActivityUserBannedFromProgram // Used to avoid recursion of JSONUnmarshal423type activityUserBannedFromProgramUnmarshalHelper struct {424 Relationships struct {425 RemovedUser struct {426 Data *User `json:"data"`427 } `json:"removed_user"`428 } `json:"relationships"`429}430// UnmarshalJSON allows JSONAPI attributes and relationships to unmarshal cleanly.431func (a *ActivityUserBannedFromProgram) UnmarshalJSON(b []byte) error {432 var helper activityUserBannedFromProgramUnmarshalHelper433 if err := json.Unmarshal(b, &helper); err != nil {434 return err435 }436 a.RemovedUser = helper.Relationships.RemovedUser.Data437 return nil438}...
encoding.go
Source:encoding.go
...29 EncDurationInTraffic *internal.Duration `json:"duration_in_traffic"`30 EncArrivalTime *internal.DateTime `json:"arrival_time"`31 EncDepartureTime *internal.DateTime `json:"departure_time"`32}33// UnmarshalJSON implements json.Unmarshaler for Leg. This decodes the API34// representation into types useful for Go developers.35func (leg *Leg) UnmarshalJSON(data []byte) error {36 x := encodedLeg{}37 err := json.Unmarshal(data, &x)38 if err != nil {39 return err40 }41 *leg = Leg(x.safeLeg)42 leg.Duration = x.EncDuration.Duration()43 leg.DurationInTraffic = x.EncDurationInTraffic.Duration()44 leg.ArrivalTime = x.EncArrivalTime.Time()45 leg.DepartureTime = x.EncDepartureTime.Time()46 return nil47}48// MarshalJSON implements json.Marshaler for Leg. This encodes Go types back to49// the API representation.50func (leg *Leg) MarshalJSON() ([]byte, error) {51 x := encodedLeg{}52 x.safeLeg = safeLeg(*leg)53 x.EncDuration = internal.NewDuration(leg.Duration)54 x.EncDurationInTraffic = internal.NewDuration(leg.DurationInTraffic)55 x.EncArrivalTime = internal.NewDateTime(leg.ArrivalTime)56 x.EncDepartureTime = internal.NewDateTime(leg.DepartureTime)57 return json.Marshal(x)58}59// safeStep is a raw version of Step that does not have custom encoding or60// decoding methods applied.61type safeStep Step62// encodedStep is the actual encoded version of Step as per the Maps APIs.63type encodedStep struct {64 safeStep65 EncDuration *internal.Duration `json:"duration"`66}67// UnmarshalJSON implements json.Unmarshaler for Step. This decodes the API68// representation into types useful for Go developers.69func (step *Step) UnmarshalJSON(data []byte) error {70 x := encodedStep{}71 err := json.Unmarshal(data, &x)72 if err != nil {73 return err74 }75 *step = Step(x.safeStep)76 step.Duration = x.EncDuration.Duration()77 return nil78}79// MarshalJSON implements json.Marshaler for Step. This encodes Go types back to80// the API representation.81func (step *Step) MarshalJSON() ([]byte, error) {82 x := encodedStep{}83 x.safeStep = safeStep(*step)84 x.EncDuration = internal.NewDuration(step.Duration)85 return json.Marshal(x)86}87// safeTransitDetails is a raw version of TransitDetails that does not have88// custom encoding or decoding methods applied.89type safeTransitDetails TransitDetails90// encodedTransitDetails is the actual encoded version of TransitDetails as per91// the Maps APIs92type encodedTransitDetails struct {93 safeTransitDetails94 EncArrivalTime *internal.DateTime `json:"arrival_time"`95 EncDepartureTime *internal.DateTime `json:"departure_time"`96}97// UnmarshalJSON implements json.Unmarshaler for TransitDetails. This decodes98// the API representation into types useful for Go developers.99func (transitDetails *TransitDetails) UnmarshalJSON(data []byte) error {100 x := encodedTransitDetails{}101 err := json.Unmarshal(data, &x)102 if err != nil {103 return err104 }105 *transitDetails = TransitDetails(x.safeTransitDetails)106 transitDetails.ArrivalTime = x.EncArrivalTime.Time()107 transitDetails.DepartureTime = x.EncDepartureTime.Time()108 return nil109}110// MarshalJSON implements json.Marshaler for TransitDetails. This encodes Go111// types back to the API representation.112func (transitDetails *TransitDetails) MarshalJSON() ([]byte, error) {113 x := encodedTransitDetails{}114 x.safeTransitDetails = safeTransitDetails(*transitDetails)115 x.EncArrivalTime = internal.NewDateTime(transitDetails.ArrivalTime)116 x.EncDepartureTime = internal.NewDateTime(transitDetails.DepartureTime)117 return json.Marshal(x)118}119// safeTransitLine is the raw version of TransitLine that does not have custom120// encoding or decoding methods applied.121type safeTransitLine TransitLine122// encodedTransitLine is the actual encoded version of TransitLine as per the123// Maps APIs124type encodedTransitLine struct {125 safeTransitLine126 EncURL string `json:"url"`127 EncIcon string `json:"icon"`128}129// UnmarshalJSON imlpements json.Unmarshaler for TransitLine. This decodes the130// API representation into types useful for Go developers.131func (transitLine *TransitLine) UnmarshalJSON(data []byte) error {132 x := encodedTransitLine{}133 err := json.Unmarshal(data, &x)134 if err != nil {135 return err136 }137 *transitLine = TransitLine(x.safeTransitLine)138 transitLine.URL, err = url.Parse(x.EncURL)139 if err != nil {140 return err141 }142 transitLine.Icon, err = url.Parse(x.EncIcon)143 if err != nil {144 return err145 }146 return nil147}148// MarshalJSON implements json.Marshaler for TransitLine. This encodes Go149// types back to the API representation.150func (transitLine *TransitLine) MarshalJSON() ([]byte, error) {151 x := encodedTransitLine{}152 x.safeTransitLine = safeTransitLine(*transitLine)153 x.EncURL = transitLine.URL.String()154 x.EncIcon = transitLine.Icon.String()155 return json.Marshal(x)156}157// safeTransitAgency is the raw version of TransitAgency that does not have158// custom encoding or decoding methods applied.159type safeTransitAgency TransitAgency160// encodedTransitAgency is the actual encoded version of TransitAgency as per the161// Maps APIs162type encodedTransitAgency struct {163 safeTransitAgency164 EncURL string `json:"url"`165}166// UnmarshalJSON imlpements json.Unmarshaler for TransitAgency. This decodes the167// API representation into types useful for Go developers.168func (transitAgency *TransitAgency) UnmarshalJSON(data []byte) error {169 x := encodedTransitAgency{}170 err := json.Unmarshal(data, &x)171 if err != nil {172 return err173 }174 *transitAgency = TransitAgency(x.safeTransitAgency)175 transitAgency.URL, err = url.Parse(x.EncURL)176 if err != nil {177 return err178 }179 return nil180}181// MarshalJSON implements json.Marshaler for TransitAgency. This encodes Go182// types back to the API representation.183func (transitAgency *TransitAgency) MarshalJSON() ([]byte, error) {184 x := encodedTransitAgency{}185 x.safeTransitAgency = safeTransitAgency(*transitAgency)186 x.EncURL = transitAgency.URL.String()187 return json.Marshal(x)188}189// safeTransitLineVehicle is the raw version of TransitLineVehicle that does not190// have custom encoding or decoding methods applied.191type safeTransitLineVehicle TransitLineVehicle192// encodedTransitLineVehicle is the actual encoded version of TransitLineVehicle193// as per the Maps APIs194type encodedTransitLineVehicle struct {195 safeTransitLineVehicle196 EncIcon string `json:"icon"`197}198// UnmarshalJSON imlpements json.Unmarshaler for TransitLineVehicle. This199// decodes the API representation into types useful for Go developers.200func (transitLineVehicle *TransitLineVehicle) UnmarshalJSON(data []byte) error {201 x := encodedTransitLineVehicle{}202 err := json.Unmarshal(data, &x)203 if err != nil {204 return err205 }206 *transitLineVehicle = TransitLineVehicle(x.safeTransitLineVehicle)207 transitLineVehicle.Icon, err = url.Parse(x.EncIcon)208 if err != nil {209 return err210 }211 return nil212}213// MarshalJSON implements json.Marshaler for TransitLineVehicle. This encodes214// Go types back to the API representation.215func (transitLineVehicle *TransitLineVehicle) MarshalJSON() ([]byte, error) {216 x := encodedTransitLineVehicle{}217 x.safeTransitLineVehicle = safeTransitLineVehicle(*transitLineVehicle)218 x.EncIcon = transitLineVehicle.Icon.String()219 return json.Marshal(x)220}221// safeDistanceMatrixElement is a raw version of DistanceMatrixElement that222// does not have custom encoding or decoding methods applied.223type safeDistanceMatrixElement DistanceMatrixElement224// encodedDistanceMatrixElement is the actual encoded version of225// DistanceMatrixElement as per the Maps APIs.226type encodedDistanceMatrixElement struct {227 safeDistanceMatrixElement228 EncDuration *internal.Duration `json:"duration"`229 EncDurationInTraffic *internal.Duration `json:"duration_in_traffic"`230}231// UnmarshalJSON implements json.Unmarshaler for DistanceMatrixElement. This232// decodes the API representation into types useful for Go developers.233func (dme *DistanceMatrixElement) UnmarshalJSON(data []byte) error {234 x := encodedDistanceMatrixElement{}235 err := json.Unmarshal(data, &x)236 if err != nil {237 return err238 }239 *dme = DistanceMatrixElement(x.safeDistanceMatrixElement)240 dme.Duration = x.EncDuration.Duration()241 dme.DurationInTraffic = x.EncDurationInTraffic.Duration()242 return nil243}244// MarshalJSON implements json.Marshaler for DistanceMatrixElement. This encodes245// Go types back to the API representation.246func (dme *DistanceMatrixElement) MarshalJSON() ([]byte, error) {247 x := encodedDistanceMatrixElement{}248 x.safeDistanceMatrixElement = safeDistanceMatrixElement(*dme)249 x.EncDuration = internal.NewDuration(dme.Duration)250 return json.Marshal(x)251}252// safeSnappedPoint is a raw version of SnappedPoint that does not have custom253// encoding or decoding methods applied.254type safeSnappedPoint SnappedPoint255// encodedSnappedPoint is the actual encoded version of SnappedPoint as per the256// Roads API.257type encodedSnappedPoint struct {258 safeSnappedPoint259 EncLocation internal.Location `json:"location"`260}261// UnmarshalJSON implements json.Unmarshaler for SnappedPoint. This decode the262// API representation into types useful for Go developers.263func (sp *SnappedPoint) UnmarshalJSON(data []byte) error {264 x := encodedSnappedPoint{}265 err := json.Unmarshal(data, &x)266 if err != nil {267 return err268 }269 *sp = SnappedPoint(x.safeSnappedPoint)270 sp.Location.Lat = x.EncLocation.Latitude271 sp.Location.Lng = x.EncLocation.Longitude272 return nil273}274// MarshalJSON implements json.Marshaler for SnappedPoint. This encodes Go275// types back to the API representation.276func (sp *SnappedPoint) MarshalJSON() ([]byte, error) {277 x := encodedSnappedPoint{}...
types_test.go
Source:types_test.go
...4 "github.com/stretchr/testify/assert"5 "testing"6 "time"7)8func Test_Common_Types_CustomFloat64_UnmarshalJSONStringSuccess(t *testing.T) {9 c := CustomFloat64{}10 _ = c.UnmarshalJSON([]byte(`"10.10"`))11 assert.Equal(t, 10.10, c.Value())12}13func Test_Common_Types_CustomFloat64_UnmarshalJSONStringError(t *testing.T) {14 c := CustomFloat64{}15 err := c.UnmarshalJSON([]byte(`"qwerty"`))16 assert.Error(t, err)17 assert.Equal(t, "CustomFloat64@UnmarshalJSON: invalid character 'q' looking for beginning of value", err.Error())18}19func Test_Common_Types_CustomFloat64_UnmarshalJSONFloatSuccess(t *testing.T) {20 c := CustomFloat64{}21 _ = c.UnmarshalJSON([]byte(`10.10`))22 assert.Equal(t, 10.10, c.Value())23}24func Test_Common_Types_CustomFloat64_UnmarshalJSONFloatError(t *testing.T) {25 c := CustomFloat64{}26 err := c.UnmarshalJSON([]byte(`qwerty`))27 assert.Error(t, err)28 assert.Equal(t, "CustomFloat64@UnmarshalJSON: invalid character 'q' looking for beginning of value", err.Error())29}30func Test_Common_Types_CustomFloat64_MarshalJSONSuccess(t *testing.T) {31 c := CustomFloat64{}32 c.Float64 = 10.1033 result, err := c.MarshalJSON()34 expected, _ := json.Marshal(c.Float64)35 assert.Equal(t, expected, result)36 assert.Nil(t, err)37}38func Test_Common_Types_CustomFloat64_MarshalJSONEmpty(t *testing.T) {39 c := CustomFloat64{}40 result, err := c.MarshalJSON()41 expected, _ := json.Marshal(c.Float64)42 assert.Equal(t, expected, result)43 assert.Nil(t, err)44}45func Test_Common_Types_CustomTimestamp_UnmarshalJSONSuccess(t *testing.T) {46 c := CustomTimestamp{}47 format := "2006-01-02 15:04:05"48 str := `"2020-09-10 15:15:15"`49 _ = c.UnmarshalJSON([]byte(str))50 assert.Equal(t, "2020-09-10 15:15:15", c.Value().Format(format))51}52func Test_Common_Types_CustomTimestamp_UnmarshalJSONError(t *testing.T) {53 c := CustomTimestamp{}54 str := `123456`55 err := c.UnmarshalJSON([]byte(str))56 assert.Error(t, err)57 assert.Equal(t, "CustomTimestamp@UnmarshalJSON: json: cannot unmarshal number into Go value of type string", err.Error())58}59func Test_Common_Types_CustomTimestamp_UnmarshalJSONErrorTimeParse(t *testing.T) {60 c := CustomTimestamp{}61 str := `"2006/01/02"`62 err := c.UnmarshalJSON([]byte(str))63 assert.Error(t, err)64 assert.Equal(t, `CustomTimestamp@UnmarshalJSON ParseTime: parsing time "2006/01/02" as "2006-01-02 15:04:05": cannot parse "/01/02" as "-"`, err.Error())65}66func Test_Common_Types_CustomTimestamp_MarshalJSONSuccess(t *testing.T) {67 c := CustomTimestamp{}68 c.Timestamp = time.Date(2020, time.Month(1), 20, 0, 0, 0, 0, time.UTC)69 result, err := c.MarshalJSON()70 assert.Equal(t, []byte(`"2020-01-20 00:00:00"`), result)71 assert.Nil(t, err)72}73func Test_Common_Types_CustomTimestamp_MarshalJSONEmpty(t *testing.T) {74 c := CustomTimestamp{}75 result, err := c.MarshalJSON()76 assert.Equal(t, []byte(`""`), result)77 assert.Nil(t, err)78}79//func Test_Common_Types_CustomTimestamp_MarshalJSONError(t *testing.T) {80// c := CustomTimestamp{}81// result, err := c.MarshalJSON()82// assert.Equal(t, []byte(``), result)83// assert.Nil(t, err)84//}85func Test_Common_Types_CustomDate_UnmarshalJSONSuccess(t *testing.T) {86 c := CustomDate{}87 format := "2006-01-02"88 str := `"2020-09-10"`89 _ = c.UnmarshalJSON([]byte(str))90 assert.Equal(t, "2020-09-10", c.Value().Format(format))91}92func Test_Common_Types_CustomDate_UnmarshalJSONError(t *testing.T) {93 c := CustomDate{}94 str := `123456`95 err := c.UnmarshalJSON([]byte(str))96 assert.Error(t, err)97 assert.Equal(t, "CustomDate@UnmarshalJSON: json: cannot unmarshal number into Go value of type string", err.Error())98}99func Test_Common_Types_CustomDate_UnmarshalJSONErrorTimeParse(t *testing.T) {100 c := CustomDate{}101 str := `"2006/01/02"`102 err := c.UnmarshalJSON([]byte(str))103 assert.Error(t, err)104 assert.Equal(t, `CustomDate@UnmarshalJSON ParseTime: parsing time "2006/01/02" as "2006-01-02": cannot parse "/01/02" as "-"`, err.Error())105}106func Test_Common_Types_CustomDate_MarshalJSONSuccess(t *testing.T) {107 c := CustomDate{}108 c.Date = time.Date(2020, time.Month(1), 20, 0, 0, 0, 0, time.UTC)109 result, err := c.MarshalJSON()110 assert.Equal(t, []byte(`"2020-01-20"`), result)111 assert.Nil(t, err)112}113func Test_Common_Types_CustomDate_MarshalJSONEmpty(t *testing.T) {114 c := CustomDate{}115 result, err := c.MarshalJSON()116 assert.Equal(t, []byte(`""`), result)117 assert.Nil(t, err)118}...
UnmarshalJSON
Using AI Code Generation
1import (2type Person struct {3}4func (p Person) UnmarshalJSON(b []byte) error {5 fmt.Println("UnmarshalJSON method called")6}7func main() {8 json.Unmarshal([]byte(`{"name":"John","age":30}`), &p)9 fmt.Println(p)10}11{John 0}12import (13type Person struct {14}15func (p Person) MarshalJSON() ([]byte, error) {16 fmt.Println("MarshalJSON method called")17}18func main() {19 p := Person{Name: "John", Age: 30}20 json.Marshal(p)21}22import (23type Person struct {24}25func (p Person) MarshalJSON() ([]byte, error) {26 fmt.Println("MarshalJSON method called")27}28func main() {29 p := Person{Name: "John", Age: 30}30 json.Marshal(p)31 fmt.Println(json.Marshal(p))32}
UnmarshalJSON
Using AI Code Generation
1func main() {2 err := json.Unmarshal([]byte(`{"a":1,"b":2,"c":3}`), &t)3 if err != nil {4 fmt.Println("error:", err)5 }6 fmt.Printf("%#v7}8func main() {9 t := types{A: 1, B: 2, C: 3}10 b, err := json.Marshal(t)11 if err != nil {12 fmt.Println("error:", err)13 }14 fmt.Println(string(b))15}16func main() {17 err := json.Unmarshal([]byte(`{"a":1,"b":2,"c":3}`), &t)18 if err != nil {19 fmt.Println("error:", err)20 }21 fmt.Printf("%#v22}23func main() {24 t := types{A: 1, B: 2, C: 3}25 b, err := json.Marshal(t)26 if err != nil {27 fmt.Println("error:", err)28 }29 fmt.Println(string(b))30}31func main() {32 err := json.Unmarshal([]byte(`{"a":1,"b":2,"c":3}`), &t)33 if err != nil {34 fmt.Println("error:", err)35 }36 fmt.Printf("%#v37}38func main() {39 t := types{A: 1, B: 2, C: 3}40 b, err := json.Marshal(t)41 if err != nil {42 fmt.Println("error:", err)43 }44 fmt.Println(string(b))45}46func main() {47 err := json.Unmarshal([]byte(`{"a":1,"b":2,"c":3}`), &t)
UnmarshalJSON
Using AI Code Generation
1func main() {2 var jsonBlob = []byte(`[{"Name": "Platypus", "Order": "Monotremata"}, {"Name": "Quoll", "Order": "Dasyuromorphia"}]`)3 err := json.Unmarshal(jsonBlob, &animals)4 if err != nil {5 fmt.Println("error:", err)6 }7 fmt.Printf("%+v", animals)8}9[{Name:Platypus Order:Monotremata} {Name:Quoll Order:Dasyuromorphia}]
UnmarshalJSON
Using AI Code Generation
1import (2type Person struct {3}4func main() {5 bs := []byte(`{"First":"James","Last":"Bond","Age":20}`)6 json.Unmarshal(bs, &p1)7 fmt.Println(p1.First)8 fmt.Println(p1.Last)9 fmt.Println(p1.Age)10}
UnmarshalJSON
Using AI Code Generation
1func main() {2 err := json.Unmarshal([]byte(`{"foo": "bar"}`), &t)3 if err != nil {4 log.Fatalln(err)5 }6 fmt.Println(t)7}8func main() {9 t := types{10 }11 err := json.NewEncoder(os.Stdout).Encode(t)12 if err != nil {13 log.Fatalln(err)14 }15}16func main() {17 t := types{18 }19 err := json.NewEncoder(os.Stdout).Encode(t)20 if err != nil {21 log.Fatalln(err)22 }23}24func main() {25 t := types{26 }27 err := json.NewEncoder(os.Stdout).Encode(t)28 if err != nil {29 log.Fatalln(err)30 }31}32func main() {33 t := types{34 }35 err := json.NewEncoder(os.Stdout).Encode(t)36 if err != nil {37 log.Fatalln(err)38 }39}40func main() {41 t := types{42 }43 err := json.NewEncoder(os.Stdout).Encode(t)44 if err != nil {45 log.Fatalln(err)46 }47}48func main() {49 t := types{50 }51 err := json.NewEncoder(os.Stdout).Encode(t)52 if err != nil {53 log.Fatalln(err)54 }55}56func main() {57 t := types{58 }59 err := json.NewEncoder(os.Stdout).Encode(t)60 if err != nil {61 log.Fatalln(err)62 }63}64func main() {65 t := types{
UnmarshalJSON
Using AI Code Generation
1func main() {2 json.Unmarshal([]byte(`{"a":1, "b":2, "c":3}`), &t)3 fmt.Printf("%+v4}5func main() {6 t := types{A: 1, B: 2, C: 3}7 b, _ := json.Marshal(t)8 fmt.Println(string(b))9}10func main() {11 t := types{A: 1, B: 2, C: 3}12 b, _ := json.Marshal(t)13 fmt.Println(string(b))14}15func main() {16 t := types{A: 1, B: 2, C: 3}17 b, _ := json.Marshal(t)18 fmt.Println(string(b))19}20func main() {21 t := types{A: 1, B: 2, C: 3}22 b, _ := json.Marshal(t)23 fmt.Println(string(b))24}25func main() {26 t := types{A: 1, B: 2, C: 3}27 b, _ := json.Marshal(t)28 fmt.Println(string(b))29}30func main() {31 t := types{A: 1, B: 2, C: 3}32 b, _ := json.Marshal(t)33 fmt.Println(string(b))34}35func main() {36 t := types{A: 1, B: 2, C: 3}37 b, _ := json.Marshal(t)38 fmt.Println(string(b))39}40func main() {41 t := types{A: 1, B: 2, C: 3}
UnmarshalJSON
Using AI Code Generation
1import (2type types struct {3}4func (t *types) UnmarshalJSON(b []byte) error {5 if err := json.Unmarshal(b, &s); err != nil {6 }7 *t = types{Types: s}8}9func main() {10 data := []byte(`["string", "int"]`)11 if err := json.Unmarshal(data, &t); err != nil {12 fmt.Println(err)13 }14 fmt.Println(t)15}16[{string int}]17import (18type types struct {19}20func (t *types) MarshalJSON() ([]byte, error) {21 return json.Marshal(t.Types)22}23func main() {24 t.Types = []string{"string", "int"}25 data, err := json.Marshal(t)26 if err != nil {27 fmt.Println(err)28 }29 fmt.Println(string(data))30}31import (32type types struct {33}34func (t *types) MarshalJSON() ([]byte, error) {35 return json.MarshalIndent(t.Types, "", "\t")36}37func main() {38 t.Types = []string{"string", "int"}39 data, err := json.MarshalIndent(t, "", "\t")40 if err != nil {41 fmt.Println(err)42 }43 fmt.Println(string(data))44}
UnmarshalJSON
Using AI Code Generation
1func main() {2 data := `{"name":"John","age":30,"cars":["Ford","BMW","Fiat"]}`3 json.Unmarshal([]byte(data), &d)4 fmt.Println(d)5 fmt.Println(d.Name)6 fmt.Println(d.Age)7 fmt.Println(d.Cars)8}9{John 30 [Ford BMW Fiat]}
UnmarshalJSON
Using AI Code Generation
1import (2type types struct {3}4func main() {5 data := []byte(`{"name":"John"}`)6 err := json.Unmarshal(data, &t)7 if err != nil {8 fmt.Println(err)9 }10 fmt.Println(t.Name)11}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!