由于swift中的泛型类型(静态)不支持静态存储属性,我想知道什么是好的替代方案.
我的具体用例是我想在swift中构建一个ORM.我有一个Entity
协议,它有一个主键的关联类型,因为一些实体将有一个整数作为他们id
,一些将有一个字符串等.所以这使Entity
协议通用.
现在我还有一个EntityCollection<T: Entity>
管理实体集合的类型,你可以看到它也是通用的.目标EntityCollection
是让它使用实体集合,就像它们是普通数组一样,而不必知道它背后有数据库.EntityCollection
将负责查询和缓存,并尽可能优化.
我想在它上面使用静态属性EntityCollection
来存储已经从数据库中提取的所有实体.因此,如果EntityCollection
要从数据库中获取同一实体的两个单独实例,则只会查询一次数据库.
你们有什么想法我能做到这一点吗?
所以我正在开发一个全新的项目,使用 Vue 3 和 Volar 作为 VSCode 的扩展。我正在使用一个组件库 CoreUI。所以在我的main.ts
我现在有这个代码。
import { createApp } from 'vue'
import App from './App.vue'
import CoreuiVue from '@coreui/vue'
const app = createApp(App);
app.use(CoreuiVue);
Run Code Online (Sandbox Code Playgroud)
现在这意味着在任何其他 SFC 中我可以使用任何 CoreUI 组件,例如<CAlert color="primary">A simple alert!</CAlert>
,而无需导入它。
它编译得很好。在编译过程中,我没有收到来自 TypeScript 或 ESLint 的关于没有正确导入组件的抱怨。因此,这些工具似乎知道这些组件现在在全球范围内可用。
然而,Volar 却不是这样,它是CAlert
这样描述的:
换句话说,它无法识别CAlert
也不能给我任何智能感知,比如它的属性和属性类型是什么。
我怎样才能让 Volar 明白所有 CoreUI 的组件都是全局可用的?
基本上,这就是我正在尝试(到目前为止尚未成功)实现的目标:
我希望 eslint 对我的.vue
文件和我的.ts
文件使用相同的 TypeScript 规则(我的每个组件都使用<script lang="ts" setup>
),并使用 mytsconfig.json
作为两者的基本 TS 配置。
现在我发现的困难是这样的。据我了解,当你为 eslint 添加 typescript 插件时,需要明确告诉它你在哪里tsconfig.json
。这里展示了如何做到这一点:
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
},
plugins: ['@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
],
};
Run Code Online (Sandbox Code Playgroud)
但问题是,我无法将打字稿解析器设置为主解析器。因为vue解析器需要为主解析器。
所以我把它设置如下:
module.exports = {
env: {
node: true,
},
root: true,
ignorePatterns: ["/.vscode/**/*", "/dist/**/*", "/public/**/*"],
extends: [
"eslint:recommended",
"plugin:import/recommended",
"plugin:import/typescript",
"plugin:vue/vue3-recommended",
"@vue/eslint-config-typescript",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"plugin:json/recommended",
"plugin:prettier/recommended",
],
plugins: …
Run Code Online (Sandbox Code Playgroud) 所以我正在编写一个名为Slade的php模板引擎,灵感来自Ruby Slim和laravel Blade.
现在很多人都建议我将它重写为词法分析器/解析器,而不是完全依赖正则表达式.所以我搜索了词法分析器和解析器并试图了解它们是如何工作的,虽然我认为我得到了一般的想法,但我仍然觉得很难开始写一个.
所以我希望有人能通过展示如何做一个例子来帮助我.我怎么会完全lex(甚至是一个动词?)并解析这个:
#wrapper.container.well first-attr="a literal attribute" second-attr=variable.name And here some text that will be the content of the div...
Run Code Online (Sandbox Code Playgroud)
进入这些节点:
[
'tagName' => 'div', // since there was no tagname, a div is assumed
'attributes' => [
'id' => 'wrapper',
'class' => 'container well',
'first-attr' => 'a literal attribute',
'second-attr' => 'the value of the variable',
],
'textContent' => 'And here some text that will be the content of the div...' …
Run Code Online (Sandbox Code Playgroud) 现在阅读文档我没有看到直接的功能,但我确实看到了使用monthly()
方法结合方法的选项when()
,所以我想,我可以这样做:
$schedule->command('send:reminders')->monthly()->when(function() {
return date('d') == '23';
});
Run Code Online (Sandbox Code Playgroud)
但现在我担心这样做不会起作用,因为据我所知,它when()
每个月只会尝试约束一次(可能不是我想要它的日期),然后当它失败时它会跳过那个月.至少那是我从阅读laravel的来源猜测的.
那么我迷路了,我怎么做到这一点?
所以我正在使用swift中的sql数据库抽象层,我希望尽可能安全地编写它.我的意思是,我宁愿让任何人都无法使用我的库在数据库中做任何非法行为.
我还无法完全解决的一件事是如何使从Swift类型到SQL数据类型的转换100%安全.所以我将解释我目前的实现及其缺陷:
所以在SQL中,许多数据类型都有参数.例如,当您要将列定义为a时VARCHAR
,需要为其指定一个表示最大长度的参数VARCHAR
,例如VARCHAR(255)
.您可以说这VARCHAR
是SQL的原始数据类型之一,并且VARCHAR(255)
是更具体的数据类型.为了在swift中以类型安全的方式表示,我创建了两个协议:
public protocol PrimitiveDataType {
associatedtype Options = Void
}
public protocol SpecifiedDataType {
associatedtype Primitive: PrimitiveDataType
static var options: Primitive.Options { get }
static func from(primitive: Primitive) -> Self
var primitive: Primitive { get }
}
Run Code Online (Sandbox Code Playgroud)
所以这是一个例子PrimitiveDataType
:
extension String: PrimitiveDataType {
public enum DatabaseStringType {
case char(length: Int), varchar(limit: Int), text
}
public typealias Options = DatabaseStringType
}
Run Code Online (Sandbox Code Playgroud)
这是一个例子SpecifiedDataType
: …
所以我有一个名为的表files
,其中包含一个文件列表,其中包含各自的名称、路径和文件类型。然后我还有一些其他表,可以附加文件。例如表user_profiles
。最后,我有一个数据透视表,用于文件和其他表之间的多对多多态关系。数据透视表被称为fileables
(想不出更好的名字)。现在,用户可能会在他们的个人资料中附加一些图片,可能还有一些视频,它们都来自文件。
通常,如果它只是图像,我会做这样的事情:
class UserProfile extends Model {
public function images()
{
return $this->morphToMany('App\File', 'fileable');
}
}
Run Code Online (Sandbox Code Playgroud)
但是,由于它是图像和视频,我想做这样的事情:
class UserProfile extends Model {
public function images()
{
return $this->morphToMany('App\File', 'fileable')->where('type', 'LIKE', 'image%');
}
public function videos()
{
return $this->morphToMany('App\File', 'fileable')->where('type', 'LIKE', 'video%');
}
}
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用。那么这样做的正确方法是什么?
我试过了:
import {Component, Template, bootstrap} from 'angular2/angular2';
import {Immutable} from 'immutable/immutable';
@Component({
selector: 'my-app'
})
@Template({
inline: '<h1>Hello {{ name }}</h1>'
})
class MyAppComponent {
constructor() {
this.name = 'Alice';
this.wishes = Immutable.List(['a dog', 'a balloon', 'and so much more']);
console.log(this.wishes);
}
}
bootstrap(MyAppComponent);
Run Code Online (Sandbox Code Playgroud)
但是然后Immutable最终被定义为未定义.然后我尝试了:
import {Component, Template, bootstrap} from 'angular2/angular2';
import {Immutable} from 'immutable/immutable';
@Component({
selector: 'my-app'
})
@Template({
inline: '<h1>Hello {{ name }}</h1>'
})
class MyAppComponent {
constructor(im: Immutable) {
this.name = 'Alice';
this.wishes = im.List(['a dog', 'a …
Run Code Online (Sandbox Code Playgroud) 因此,我第一次要做一个涉及地图和地图之上的图层的项目,地图上有很多点和很多多边形。
我倾向于为点和多边形创建单独的表,然后在它们和图层表之间创建多对多关系。如果我这样做,我最终会得到 5 个表:points
、polygons
、layers
和layers_points
。layers_polygons
但是,我看到 PostGIS 还提供了名为MULTIPOINT
和 的类型MULTIPOLYGON
。如果我使用这些类型,那么我可以将其全部放入表中layers
。我想这会让查询更快,因为我需要更少的连接。然而,我不确定以后我是否会后悔,如果这意味着处理单个点和多边形变得不可能。我什至还不确定是否有必要对各个点和多边形进行计算,但很高兴知道这两种方法是否可行。
所以基本上我要问的是,这些不同方法的优点和缺点是什么?
当我阅读 eslint 文档时,似乎我应该能够只用 1 或 2 行代码就可以让它工作。
你看,这里(https://eslint.org/docs/latest/user-guide/configuring/ignoring-code#ignorepatterns-in-config-files)他们说:
您可以告诉 ESLint 忽略
ignorePatterns
配置文件中使用的特定文件和目录。ignorePatterns
模式遵循与 相同的规则.eslintignore
。请参阅文件.eslintignore
文档以了解更多信息。
好吧,这些文档只是再往下一点(https://eslint.org/docs/latest/user-guide/configuring/ignoring-code#the-eslintignore-file),他们说:
前面有 ! 是重新包含被早期模式忽略的模式的否定模式。
所以,我当时想,应该有这个列表:
ignorePatterns: ["*", "!src/**/*"]
Run Code Online (Sandbox Code Playgroud)
但事实并非如此。我真的不明白为什么...... VSCode 停止对所有文件进行 linting,当我运行npm run lint
命令时,我收到以下消息:
您正在检查“src”,但所有与 glob 模式“src”匹配的文件都将被忽略。
哦,为了防止误解,我的命令不需要这个npm run lint
。因为在该命令中我已经可以将 eslint 配置为仅查看我的src
文件夹。我想让它们ignorePatterns
像我所描述的那样工作的原因是因为我希望 VSCode 停止尝试检查 src 文件夹之外的文件。
所以我一直在查找更多有关MVC设计模式的教程和文章,以加深我对它们的理解,我开始怀疑我是否一直在做错.所以,这些模式的目标是使代码可重用并最小化重复的代码,对吗?
我已经看到了解释这些设计模式的各种方法让我感到困惑.在过去,我认为它是控制器作为介体 - 模型和视图之间的方式,但现在我知道这是错误的,并且视图实际上不仅仅是模板.然后我也读到某个地方(我想在这里),在真正的MVC模式中,根据定义,只有一个模型,所有其他"模型"只是单个模型的不同方面.是这样的吗?这是分离代码并使其可重用的最佳方法吗?在代码中会是什么样子?而在我读到的其他地方,对于Web应用程序,最好坚持使用MVVM模式.
所以现在我有点困惑.什么是在Web应用程序中分离关注点并使代码可重用的最有效模式?我不仅希望看到这种模式的描述,还希望看到代码中的一个简短示例,以便我更好地理解它.
php ×3
eslint ×2
laravel ×2
swift ×2
vuejs3 ×2
angular ×1
blade ×1
core-ui ×1
cron ×1
doctrine-orm ×1
ecmascript-6 ×1
eloquent ×1
enums ×1
eslintrc ×1
generics ×1
immutable.js ×1
javascript ×1
laravel-5 ×1
laravel-5.1 ×1
lexer ×1
mvvm ×1
orm ×1
parsing ×1
postgis ×1
postgresql ×1
slim-lang ×1
type-safety ×1
typescript ×1
vue.js ×1