package relation import ( "github.com/gshopify/service-wrapper/model" "github.com/mailru/dbr" "gshopper.com/gshopify/shop/graphql/generated" "strings" ) type Location struct { Id string `db:"id"` Name string `db:"name"` Status string `db:"status"` Legacy bool `db:"legacy"` Address1 dbr.NullString `db:"address_1"` Address2 dbr.NullString `db:"address_2"` City dbr.NullString `db:"city"` Country dbr.NullString `db:"country"` CountryCode dbr.NullString `db:"country_code"` Latitude dbr.NullFloat64 `db:"latitude"` Longitude dbr.NullFloat64 `db:"longitude"` Phone dbr.NullString `db:"phone"` Province dbr.NullString `db:"province"` ProvinceCode dbr.NullString `db:"province_code"` Zip dbr.NullString `db:"zip"` LocalizedCountryName dbr.NullString `db:"localized_country_name"` LocalizedProvinceName dbr.NullString `db:"localized_province_name"` } func (l *Location) As() *generated.Location { var ( fmtProvince = strings.Builder{} loc = &generated.Location{ Address: &generated.LocationAddress{}, ID: model.NewId(model.GidLocation, l.Id), Name: l.Name, } ) if l.Address1.Valid { loc.Address.Address1 = &l.Address1.String loc.Address.Formatted = append(loc.Address.Formatted, l.Address1.String) } if l.Address2.Valid { loc.Address.Address2 = &l.Address2.String loc.Address.Formatted = append(loc.Address.Formatted, l.Address2.String) } if l.City.Valid { loc.Address.City = &l.City.String loc.Address.Formatted = append(loc.Address.Formatted, l.City.String) } if l.ProvinceCode.Valid { loc.Address.ProvinceCode = &l.ProvinceCode.String fmtProvince.WriteString(l.ProvinceCode.String) } if l.Province.Valid { loc.Address.Province = &l.Province.String if fmtProvince.Len() == 0 { fmtProvince.WriteString(l.Province.String) } } if l.Zip.Valid { loc.Address.Zip = &l.Zip.String if fmtProvince.Len() == 0 { fmtProvince.WriteString(" ") } fmtProvince.WriteString(l.Zip.String) } if fmtProvince.Len() > 0 { loc.Address.Formatted = append(loc.Address.Formatted, fmtProvince.String()) } if l.Country.Valid { loc.Address.Country = &l.Country.String loc.Address.Formatted = append(loc.Address.Formatted, l.Country.String) } if code := model.CountryCode(l.CountryCode.String); code.IsValid() { s := code.String() loc.Address.CountryCode = &s } if l.Latitude.Valid { loc.Address.Latitude = &l.Latitude.Float64 } if l.Longitude.Valid { loc.Address.Longitude = &l.Longitude.Float64 } if l.Phone.Valid { loc.Address.Phone = &l.Phone.String } return loc }