Metastatic.Semantic.OpKind
(Metastatic v0.10.4)
View Source
Semantic operation kind metadata for MetaAST nodes.
OpKind captures the semantic meaning of function calls and operations, enabling analyzers to reason about code at a higher level than raw syntax.
Structure
An OpKind is a keyword list with the following fields:
:domain- The operation domain (required):operation- The specific operation type (required):target- The entity/resource being operated on (optional):async- Whether this is an async operation (optional, default: false):framework- The framework/library this pattern comes from (optional)
Domains
Currently supported domains:
:db- Database operations (CRUD, transactions, queries)
Future domains (not yet implemented):
:http- HTTP client operations:auth- Authentication/authorization:cache- Cache operations:queue- Message queue operations:file- File I/O operations:external_api- External API calls
Database Operations (:db)
| Operation | Description |
|---|---|
:retrieve | Fetch single record by primary key |
:retrieve_all | Fetch multiple/all records |
:query | Complex query with conditions |
:create | Insert new record |
:update | Update existing record |
:delete | Delete record |
:transaction | Transaction boundary |
:preload | Eager load associations |
:aggregate | Aggregation operations (count, sum, etc.) |
Examples
# Elixir/Ecto: Repo.get(User, 1)
[domain: :db, operation: :retrieve, target: "User", framework: :ecto]
# Python/Django: User.objects.filter(active=True)
[domain: :db, operation: :query, target: "User", framework: :django]
# JavaScript/Sequelize: User.findAll()
[domain: :db, operation: :retrieve_all, target: "User", framework: :sequelize]Usage in MetaAST
OpKind is stored in the metadata of :function_call nodes:
{:function_call, [
name: "Repo.get",
line: 42,
op_kind: [domain: :db, operation: :retrieve, target: "User"]
], [args...]}Analyzers can check for the presence of op_kind:
def analyze({:function_call, meta, _args} = node, _context) do
case Keyword.get(meta, :op_kind) do
nil -> use_heuristics(node)
op_kind -> analyze_with_semantic(node, op_kind)
end
end
Summary
Types
Auth operation types
Cache operation types
Database operation types
Supported operation domains
External API operation types
File operation types
Known framework identifiers
HTTP operation types
All operation types (union of domain-specific operations)
Queue operation types
OpKind as a keyword list
Functions
Checks if this is an auth operation.
Returns all supported auth operations.
Checks if this is a cache operation.
Returns all supported cache operations.
Checks if this is a database operation.
Returns all supported database operations.
Gets the domain from an OpKind.
Returns all supported domains.
Checks if this is an external API operation.
Returns all supported external API operations.
Checks if this is a file operation.
Returns all supported file operations.
Checks if this is an HTTP operation.
Returns all supported HTTP operations.
Creates a new OpKind keyword list.
Gets the operation from an OpKind.
Checks if this is a queue operation.
Returns all supported queue operations.
Checks if this is a read operation (retrieve, retrieve_all, query).
Gets the target from an OpKind.
Checks if a value is a valid OpKind.
Checks if this is a write operation (create, update, delete).
Types
@type auth_operation() ::
:login
| :logout
| :authenticate
| :register
| :verify_token
| :generate_token
| :refresh_token
| :hash_password
| :verify_password
| :authorize
| :oauth
| :session
Auth operation types
@type cache_operation() ::
:get
| :set
| :delete
| :clear
| :invalidate
| :expire
| :exists
| :increment
| :decrement
| :ttl
| :fetch
Cache operation types
@type db_operation() ::
:retrieve
| :retrieve_all
| :query
| :create
| :update
| :delete
| :transaction
| :preload
| :aggregate
Database operation types
@type domain() :: :db | :http | :auth | :cache | :queue | :file | :external_api
Supported operation domains
@type external_api_operation() ::
:call | :upload | :download | :send | :charge | :webhook | :search | :sync
External API operation types
@type file_operation() ::
:read
| :write
| :append
| :delete
| :copy
| :move
| :exists
| :stat
| :mkdir
| :rmdir
| :list
| :open
| :close
File operation types
@type framework() ::
:ecto
| :amnesia
| :sqlalchemy
| :django
| :peewee
| :sequelize
| :typeorm
| :prisma
| :mongoose
| :activerecord
| :httpoison
| :req
| :tesla
| :finch
| :requests
| :httpx
| :aiohttp
| :nethttp
| :httparty
| :faraday
| :restclient
| :fetch
| :axios
| :got
| :superagent
| :guardian
| :pow
| :bcrypt_elixir
| :argon2_elixir
| :pbkdf2_elixir
| :comeonin
| :ueberauth
| :flask_login
| :pyjwt
| :jose
| :passlib
| :devise
| :warden
| :bcrypt_ruby
| :passport
| :jsonwebtoken
| :bcryptjs
| :auth0
| :cachex
| :concache
| :nebulex
| :ets
| :redis_py
| :django_cache
| :flask_cache
| :rails_cache
| :dalli
| :redis_rb
| :node_cache
| :ioredis
| :broadway
| :oban
| :genstage
| :amqp
| :celery
| :rq
| :kombu
| :sidekiq
| :activejob
| :resque
| :bullmq
| :amqplib
| :agenda
| :elixir_file
| :elixir_io
| :python_builtin
| :python_os
| :pathlib
| :shutil
| :ruby_file
| :fileutils
| :nodejs_fs
| :nodejs_fs_promises
| :ex_aws
| :boto3
| :aws_sdk
| :aws_sdk_js
| :stripe
| :twilio
| :sendgrid
| :unknown
Known framework identifiers
@type http_operation() ::
:get | :post | :put | :patch | :delete | :head | :options | :request | :stream
HTTP operation types
@type operation() :: db_operation() | http_operation() | auth_operation() | cache_operation() | queue_operation() | file_operation() | external_api_operation()
All operation types (union of domain-specific operations)
@type queue_operation() ::
:publish
| :consume
| :subscribe
| :acknowledge
| :reject
| :enqueue
| :dequeue
| :schedule
| :retry
| :process
Queue operation types
@type t() :: [ domain: domain(), operation: operation(), target: String.t() | nil, async: boolean(), framework: framework() | nil ]
OpKind as a keyword list
Functions
Checks if this is an auth operation.
Examples
iex> op_kind = [domain: :auth, operation: :login]
iex> Metastatic.Semantic.OpKind.auth?(op_kind)
true
iex> op_kind = [domain: :db, operation: :retrieve]
iex> Metastatic.Semantic.OpKind.auth?(op_kind)
false
@spec auth_operations() :: [auth_operation()]
Returns all supported auth operations.
Examples
iex> Metastatic.Semantic.OpKind.auth_operations()
[:login, :logout, :authenticate, :register, :verify_token, :generate_token, :refresh_token, :hash_password, :verify_password, :authorize, :oauth, :session]
Checks if this is a cache operation.
Examples
iex> op_kind = [domain: :cache, operation: :get]
iex> Metastatic.Semantic.OpKind.cache?(op_kind)
true
iex> op_kind = [domain: :db, operation: :retrieve]
iex> Metastatic.Semantic.OpKind.cache?(op_kind)
false
@spec cache_operations() :: [cache_operation()]
Returns all supported cache operations.
Examples
iex> Metastatic.Semantic.OpKind.cache_operations()
[:get, :set, :delete, :clear, :invalidate, :expire, :exists, :increment, :decrement, :ttl, :fetch]
Checks if this is a database operation.
Examples
iex> op_kind = [domain: :db, operation: :retrieve]
iex> Metastatic.Semantic.OpKind.db?(op_kind)
true
iex> op_kind = [domain: :http, operation: :get]
iex> Metastatic.Semantic.OpKind.db?(op_kind)
false
@spec db_operations() :: [db_operation()]
Returns all supported database operations.
Examples
iex> Metastatic.Semantic.OpKind.db_operations()
[:retrieve, :retrieve_all, :query, :create, :update, :delete, :transaction, :preload, :aggregate]
Gets the domain from an OpKind.
Examples
iex> op_kind = [domain: :db, operation: :retrieve]
iex> Metastatic.Semantic.OpKind.domain(op_kind)
:db
@spec domains() :: [domain()]
Returns all supported domains.
Examples
iex> Metastatic.Semantic.OpKind.domains()
[:db, :http, :auth, :cache, :queue, :file, :external_api]
Checks if this is an external API operation.
Examples
iex> op_kind = [domain: :external_api, operation: :call]
iex> Metastatic.Semantic.OpKind.external_api?(op_kind)
true
iex> op_kind = [domain: :db, operation: :retrieve]
iex> Metastatic.Semantic.OpKind.external_api?(op_kind)
false
@spec external_api_operations() :: [external_api_operation()]
Returns all supported external API operations.
Examples
iex> Metastatic.Semantic.OpKind.external_api_operations()
[:call, :upload, :download, :send, :charge, :webhook, :search, :sync]
Checks if this is a file operation.
Examples
iex> op_kind = [domain: :file, operation: :read]
iex> Metastatic.Semantic.OpKind.file?(op_kind)
true
iex> op_kind = [domain: :db, operation: :retrieve]
iex> Metastatic.Semantic.OpKind.file?(op_kind)
false
@spec file_operations() :: [file_operation()]
Returns all supported file operations.
Examples
iex> Metastatic.Semantic.OpKind.file_operations()
[:read, :write, :append, :delete, :copy, :move, :exists, :stat, :mkdir, :rmdir, :list, :open, :close]
Checks if this is an HTTP operation.
Examples
iex> op_kind = [domain: :http, operation: :get]
iex> Metastatic.Semantic.OpKind.http?(op_kind)
true
iex> op_kind = [domain: :db, operation: :retrieve]
iex> Metastatic.Semantic.OpKind.http?(op_kind)
false
@spec http_operations() :: [http_operation()]
Returns all supported HTTP operations.
Examples
iex> Metastatic.Semantic.OpKind.http_operations()
[:get, :post, :put, :patch, :delete, :head, :options, :request, :stream]
Creates a new OpKind keyword list.
Examples
iex> Metastatic.Semantic.OpKind.new(:db, :retrieve)
[domain: :db, operation: :retrieve, target: nil, async: false, framework: nil]
iex> Metastatic.Semantic.OpKind.new(:db, :retrieve, target: "User", framework: :ecto)
[domain: :db, operation: :retrieve, target: "User", async: false, framework: :ecto]
Gets the operation from an OpKind.
Examples
iex> op_kind = [domain: :db, operation: :retrieve]
iex> Metastatic.Semantic.OpKind.operation(op_kind)
:retrieve
Checks if this is a queue operation.
Examples
iex> op_kind = [domain: :queue, operation: :enqueue]
iex> Metastatic.Semantic.OpKind.queue?(op_kind)
true
iex> op_kind = [domain: :db, operation: :retrieve]
iex> Metastatic.Semantic.OpKind.queue?(op_kind)
false
@spec queue_operations() :: [queue_operation()]
Returns all supported queue operations.
Examples
iex> Metastatic.Semantic.OpKind.queue_operations()
[:publish, :consume, :subscribe, :acknowledge, :reject, :enqueue, :dequeue, :schedule, :retry, :process]
Checks if this is a read operation (retrieve, retrieve_all, query).
Examples
iex> op_kind = [domain: :db, operation: :retrieve]
iex> Metastatic.Semantic.OpKind.read?(op_kind)
true
iex> op_kind = [domain: :db, operation: :create]
iex> Metastatic.Semantic.OpKind.read?(op_kind)
false
Gets the target from an OpKind.
Examples
iex> op_kind = [domain: :db, operation: :retrieve, target: "User"]
iex> Metastatic.Semantic.OpKind.target(op_kind)
"User"
Checks if a value is a valid OpKind.
Examples
iex> Metastatic.Semantic.OpKind.valid?([domain: :db, operation: :retrieve])
true
iex> Metastatic.Semantic.OpKind.valid?([domain: :invalid, operation: :foo])
false
iex> Metastatic.Semantic.OpKind.valid?("not a keyword list")
false
Checks if this is a write operation (create, update, delete).
Examples
iex> op_kind = [domain: :db, operation: :create]
iex> Metastatic.Semantic.OpKind.write?(op_kind)
true
iex> op_kind = [domain: :db, operation: :retrieve]
iex> Metastatic.Semantic.OpKind.write?(op_kind)
false